VERB_code_2.2  2
 All Classes Namespaces Files Functions Variables Typedefs Macros Pages
bisection.cpp
Go to the documentation of this file.
1 /**
2  * Bisection method of root finding code
3  *
4  * \file bisection.cpp
5  *
6  * \author Unknown (Numeric recepies?)
7  */
8 
9 
10 #include <assert.h>
11 #include <math.h>
12 #include "bisection.h"
13 
14 /**
15  * \brief Looking for roots of function f for given Alpha on the interval x0-x1
16  * \param (*f), - Function for root finding. Only x-roots are finded, Alpha is constant
17  * \param Alpha, - constant for the f function
18  * \param x0 = -1E+7, - left boundary of the interval
19  * \param x1 = 1E+7, - right boundary of the interval
20  * \param eps = max_error); - max error
21  */
22 double bisection (double (*f)(double x, double Alpha), double Alpha, double x0, double x1, double eps) {
23 
24  // assert... some sort of checking and notifiyng
25  assert(x0 < x1);
26 
27  double x;
28  do {
29 
30  x = (x0 + x1) / 2;
31  double y0 = f(x0, Alpha), yy = f(x, Alpha);
32 
33  if (yy * y0 < 0) x1 = x;
34  else
35  if (yy * y0 > 0) x0 = x;
36  else
37  return x;
38 
39  } while (fabs(x1 - x0) > eps);
40 
41  return x;
42 
43 }