Skip to content

Commit

Permalink
Updating files (#119)
Browse files Browse the repository at this point in the history
* Update gauss.cpp

Added english translation

* Update gauss.h

Added translation

* Update inter.cpp

Added translation

* Update inter.h

English translation

* Translation and corrected h calculation

* Translation and corrected h calculation

* Translation added

* Translation added

* Added translation

* Translation added
  • Loading branch information
mrutkowski-aero committed Apr 15, 2024
1 parent f73013e commit 5e92a15
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 49 deletions.
13 changes: 12 additions & 1 deletion code/info2/gauss.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ metoda eliminacji Gaussa.
b - wskaznik na tablice prawych stron ukladu rownan.
*/

#include "gauss.h"
/*
Function solves a system of linear equations
A * x = b
using the Gauss elimination method.
Functions arguments:
N - number of equations,
A - pointer to two-dimensional array,
x - pointer to an array storing solution of the system of equations,
b - pointer to an array storing the right hand side vector b.
*/
//#include "gauss.h"

void gauss(int N, double **A, double *x, double *b) {
double sum, wsp;
Expand Down
10 changes: 10 additions & 0 deletions code/info2/gauss.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@ metoda eliminacji Gaussa.
x - wskaznik na tablice przechowujaca rozwiazanie ukladu rownan,
b - wskaznik na tablice prawych stron ukladu rownan.
*/
/*
Function solves a system of linear equations
A * x = b
using the Gauss elimination method.
Functions arguments:
N - number of equations,
A - pointer to two-dimensional array,
x - pointer to an array storing solution of the system of equations,
b - pointer to an array storing the right hand side vector b.
*/
void gauss(int N, double **A, double *x, double *b);

#endif
Expand Down
5 changes: 5 additions & 0 deletions code/info2/inter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
// tablice *x i *y zawieraja wspolrzedne wezlow interpolacji
// n liczba wezlow interpolacji
// xx wartosc dla ktorej liczy sie wielomian

// function computing the value of Lagrange interpolation polynomial
// arrays *x and *y contain information about interpolation nodes
// n defines number of interpolation nodes
// xx value for which the value of polynomial is evaluated
double lagrange( double *x, double *y, int n, double xx )
{
int i, j;
Expand Down
5 changes: 5 additions & 0 deletions code/info2/inter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
// tablice x[] i y[] zawieraja wspolrzedne wezlow interpolacji
// n liczba wezlow interpolacji
// xx wartosc dla ktorej liczy sie wielomian

// function computing values of Lagrange interpolation polynomial
// arrays x[] i y[] contain coordinates of predefined interpolation nodes
// n is number of interpolation nodes
// xx value of independent variable for which the value of polynomial is computed
double lagrange( double *x, double *y, int n, double xx );

#endif
18 changes: 11 additions & 7 deletions code/info2/kwad.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

// oblicza calke metoda trapezow
// computes definite integral using complex the trapezoidal rule
double trapez(double a, double b, double (*pf)(double), int n )
{
double h = (b - a) / (n - 1);
double h = (b - a) / n;
double suma = 0.5 * (pf(a) + pf(b));
double x = a;

for (int i = 1; i < n-1; i++)
for (int i = 0; i < n-1; i++)
{
x += h;
suma += pf(x);
}
return suma * h;
}


// oblicza calke metoda simpsona
// computes definite integral using complex the Simpson rule
double simpson( double a, double b, double (*pf)(double), int n )
{
double x = a;
int nc = abs((n - 2) / 2 + 1) * 2 + 1; // poprawione n tak by bylo nieparzyste i >= n_old
double h = (b - a) / (nc - 1);
double h = (b - a) / (2*n);
double h2 = h * 2;
double suma = pf(a) + 4. *pf(a + h) + pf(b);
double x1 = a + h;

double suma = pf(a) + 4. *pf(x1) + pf(b);

for (int i = 3; i < nc; i += 2)
for (int i = 0; i < n-1; i += 1)
{
x += h2;
suma += 2. * pf(x) + 4. * pf(x + h);
Expand Down
10 changes: 10 additions & 0 deletions code/info2/kwad.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
#ifndef __KWAD_H__
#define __KWAD_H__


//////////////////////////////////////////////////////////////////
// oblicza metoda trapezow calke funkcji pf w przedziale <a,b>
// w oparciu o n punktow (n >= 2)

// computes definite integral on the range <a,b> with complex
// trapezoidal rule on (n >= 1) subdomains
// (*pf)(double) is a pointer to a function with one argument
//////////////////////////////////////////////////////////////////
double trapez( double a, double b, double (*pf)(double), int n);


//////////////////////////////////////////////////////////////////
// oblicza metoda simpsona calke funkcji pf w przedziale <a,b>
// w oparciu o n punktow (n >= 2)

// computes definite integral on the range <a,b> with complex
// Simpson rule on (n >= 1) subdomains
// (*pf)(double) is a pointer to a function with one argument
//////////////////////////////////////////////////////////////////
double simpson( double a, double b, double (*pf)(double), int n);

Expand Down
58 changes: 22 additions & 36 deletions code/info2/nonlin.cpp
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
#include <math.h>
#ifndef __NONLIN_H__
#define __NONLIN_H__

double bisec( double xa, double xb, double (*pf)(double), double eps, int *i_iter )
{
int i;
double fa, fb, xc, fc;
// rozwiazuje rownanie pf(x)=0 metoda bisekcji
// a,b to granice przedzialu, w ktorym poszukuje sie pierwiastka
// UWAGA: musi byc spelniony warunek:
// pf(a)*pf(b) <= 0 (jesli nie jest spelniony zwracana jest wartosc 0, liczba iteracji wynosi -1)
// jesli warunek jest spelniony
// wartosc zwracana = liczba iteracji uzytych dla znalezienia pierwiastka
// x = poszukiwany pierwiastek okreslony z dokladnoscia "eps"

fa = pf(xa);
fb = pf(xb);
// solves equation pf(x) = 0 using bisection method
// a,b are the limits of the range in which the root is sought
// (*pf)(double) is pointer to function of type double with a one argument
// eps is accuracy used to stop an iteration process (eg. eps=1.e-3)
// *i_iter pointer to a variable storing the number of iterations
// WARNING:
// IF condition pf(a)*pf(b) <= 0 is satisfied the function returns
// the root approximation and the number of iteraions *i_iter for given eps
// ELSE
// it returns 0, and the number of iterations is set to -1
//
double bisec( double a, double b, double (*pf)(double), double eps, int *i_iter);

if ( fa * fb > 0.0)
{
*i_iter = -1;
return 0;
}

for ( i = 1; i <= 1000; i++ )
{
xc = ( xa + xb ) / 2.;
fc = pf( xc );

if( fa * fc < 0. )
{
xb = xc;
fb = fc;
}
else
{
xa = xc;
fa = fc;
}

if ( fabs(fc) < eps && fabs(xb-xa) < eps)
break;
}

*i_iter = i;
return xc;
}
#endif // __NONLIN_H__
42 changes: 39 additions & 3 deletions code/info2/rk4.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <math.h>

#define MAXN 100 // maksymalna liczba rownan
// maksymalna liczba rownan
// maksimum number of the first order ODE's
#define MAXN 100

// --------------------------------------------------------------------------
// Funkcja wykonuje, metoda Rungego-Kutty IV-ego rzedu,
Expand All @@ -14,7 +16,21 @@
// h - krok calkowania
// fun(x,y) - nazwa funkcji obliczajacej prawe strony
// y1 - obliczona wartosc zmiennej zaleznej w punkcie x0+h

// --------------------------------------------------------------------------
// Using IV-th order Runge-Kutta method, function performs
// one integration/time step during solution of the one first-order ODE
// (initial value problem):
//
// dy/dx = fun(x,y), y(x0)=y0
//
// Parameters:
// x0 - initial value of independent variable (time)
// y0 - initial value of dependent variable (unknown function y())
// h - integration/time step size
// fun(x,y) - name of the function computing the right hand side of the equation
//
// rk4() returns y1 - value of dependent variable at point/time x0+h
//
double rk4(double x0, double y0, double h, double (*fun)(double, double))
{
double y1;
Expand All @@ -27,6 +43,7 @@ double rk4(double x0, double y0, double h, double (*fun)(double, double))
return y1;
}


// --------------------------------------------------------------------------
// Funkcja wykonuje, metoda Rungego-Kutty IV-tego rzedu,
// jeden krok calkowania wektorowego rownania rozniczkowego zwyczjanego:
Expand All @@ -41,7 +58,26 @@ double rk4(double x0, double y0, double h, double (*fun)(double, double))
// fun(x,y,prawastr) - nazwa funkcji obliczajacej prawe strony
// y1 - obliczona wartosc zmiennej zaleznej w punkcie x0+h
// (tablica n-elementowa)

// ------------------------------------------------------------------------------
// Using IV-th order Runge-Kutta method, function performs
// one integration/time step during solution of the set of
// the n first-order ODE's:
//
// dY/dx = Fun(x,Y), Y(x0)=y0[] where Y,Fun,y0 are vectors
//
// Parameters:
// x0 - initial value of independent variable (time)
// y0[] - initial values of dependent variables (n-element array)
// h - integration/time step size
// n - number of the first-order ODE's (size of arrays y0[] and y1[])
//
// fun(x0,y0,k) - name of the function computing the right hand sides
// of the set of n equations, it takes x0, y0[] and returns
// vector k[] containing evaluated right hand sides
//
// vrk4() result is y1[] array with values of dependent variables
// at point/time x0+h
//
void vrk4(double x0, double y0[], double h, int n, void (*fun)(double, double*, double*), double y1[])
{
int i;
Expand Down
38 changes: 36 additions & 2 deletions code/info2/rk4.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@
// fun(x,y) - nazwa funkcji obliczajacej prawe strony
// y1 - obliczona wartosc zmiennej zaleznej w punkcie x0+h

// --------------------------------------------------------------------------
// Using IV-th order Runge-Kutta method, function performs
// one integration/time step during solution of the one first-order ODE
// (initial value problem):
//
// dy/dx = fun(x,y), y(x0)=y0
//
// Parameters:
// x0 - initial value of independent variable (time)
// y0 - initial value of dependent variable (unknown function y())
// h - integration/time step size
// fun(x,y) - name of the function computing the right hand side of the equation
//
// rk4() returns y1 - value of dependent variable at point/time x0+h
//
double rk4(double x0, double y0, double h, double (*fun)(double, double));


// --------------------------------------------------------------------------
// Funkcja wykonuje, metoda Rungego-Kutty IV-tego rzedu,
// jeden krok calkowania wektorowego rownania rozniczkowego zwyczjanego:
Expand All @@ -27,11 +41,31 @@ double rk4(double x0, double y0, double h, double (*fun)(double, double));
// x0 - wartosc startowa zm. niezaleznej
// y0 - wartosc startowa zm. zaleznej (tablica n-elementowa)
// h - krok calkowania
// n - liczba rownań
// n - liczba rownań
// fun(x,y,prawastr) - nazwa funkcji obliczajacej prawe strony
// y1 - obliczona wartosc zmiennej zaleznej w punkcie x0+h
// (tablica n-elementowa)

// ------------------------------------------------------------------------------
// Using IV-th order Runge-Kutta method, function performs
// one integration/time step during solution of the set of
// the n first-order ODE's:
//
// dY/dx = Fun(x,Y), Y(x0)=y0[] where Y,Fun,y0 are vectors
//
// Parameters:
// x0 - initial value of independent variable (time)
// y0[] - initial values of dependent variables (n-element array)
// h - integration/time step size
// n - number of the first-order ODE's (size of arrays y0[] and y1[])
//
// fun(x0,y0,k) - name of the function computing the right hand sides
// of the set of n equations, it takes x0, y0[] and returns
// vector k[] containing evaluated right hand sides
//
// vrk4() result is y1[] array with values of dependent variables
// at point/time x0+h
//
void vrk4(double x0, double y0[], double h, int n, void (*fun)(double, double*, double*), double y1[]);

#endif

0 comments on commit 5e92a15

Please sign in to comment.