Skip to content

Commit

Permalink
Added translation to the download files. (#120)
Browse files Browse the repository at this point in the history
* Correcting mistake

* Adding translation

* Adding translation
  • Loading branch information
mrutkowski-aero committed Apr 15, 2024
1 parent 5e92a15 commit ddd846d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
67 changes: 42 additions & 25 deletions code/info2/nonlin.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
#ifndef __NONLIN_H__
#define __NONLIN_H__

// 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"

// 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);

#endif // __NONLIN_H__
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

double bisec( double xa, double xb, double (*pf)(double), double eps, int *i_iter )
{
int i;
double fa, fb, xc, fc;

fa = pf(xa);
fb = pf(xb);

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;
}

14 changes: 13 additions & 1 deletion code/info2/nonlin.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
// jesli warunek jest spelniony
// wartosc zwracana = liczba iteracji uzytych dla znalezienia pierwiastka
// x = poszukiwany pierwiastek okreslony z dokladnoscia "eps"

// 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);

#endif __NONLIN_H__
#endif __NONLIN_H__

0 comments on commit ddd846d

Please sign in to comment.