-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added translation to the download files. (#120)
* Correcting mistake * Adding translation * Adding translation
- Loading branch information
1 parent
5e92a15
commit ddd846d
Showing
2 changed files
with
55 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters