diff --git a/code/info2/gauss.cpp b/code/info2/gauss.cpp index 5bad04d6a..0a8726356 100644 --- a/code/info2/gauss.cpp +++ b/code/info2/gauss.cpp @@ -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; diff --git a/code/info2/gauss.h b/code/info2/gauss.h index 44f059f0e..d5cfe6539 100644 --- a/code/info2/gauss.h +++ b/code/info2/gauss.h @@ -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 diff --git a/code/info2/inter.cpp b/code/info2/inter.cpp index 997127631..21e48971b 100644 --- a/code/info2/inter.cpp +++ b/code/info2/inter.cpp @@ -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; diff --git a/code/info2/inter.h b/code/info2/inter.h index 8dd0bdd4e..c112cf860 100644 --- a/code/info2/inter.h +++ b/code/info2/inter.h @@ -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 diff --git a/code/info2/kwad.cpp b/code/info2/kwad.cpp index fc396fc71..38294e74c 100644 --- a/code/info2/kwad.cpp +++ b/code/info2/kwad.cpp @@ -1,13 +1,16 @@ +#include +#include #include // 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); @@ -15,17 +18,18 @@ double trapez(double a, double b, double (*pf)(double), int n ) 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); diff --git a/code/info2/kwad.h b/code/info2/kwad.h index 32ddf6f46..fd0e392e2 100644 --- a/code/info2/kwad.h +++ b/code/info2/kwad.h @@ -1,15 +1,25 @@ #ifndef __KWAD_H__ #define __KWAD_H__ + ////////////////////////////////////////////////////////////////// // oblicza metoda trapezow calke funkcji pf w przedziale // w oparciu o n punktow (n >= 2) + +// computes definite integral on the range 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 // w oparciu o n punktow (n >= 2) + +// computes definite integral on the range 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); diff --git a/code/info2/nonlin.cpp b/code/info2/nonlin.cpp index e95e425ce..8be71742c 100644 --- a/code/info2/nonlin.cpp +++ b/code/info2/nonlin.cpp @@ -1,39 +1,25 @@ -#include +#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; -} \ No newline at end of file +#endif // __NONLIN_H__ diff --git a/code/info2/rk4.cpp b/code/info2/rk4.cpp index fee11fd77..65616c79c 100644 --- a/code/info2/rk4.cpp +++ b/code/info2/rk4.cpp @@ -1,6 +1,8 @@ #include -#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, @@ -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; @@ -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: @@ -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; diff --git a/code/info2/rk4.h b/code/info2/rk4.h index 23bc7d06c..73bc8ee04 100644 --- a/code/info2/rk4.h +++ b/code/info2/rk4.h @@ -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: @@ -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