Sequence of Bisect implementations
Bisect3_lambda.cpp
Go to the documentation of this file.
1 // Bisect3_lambda.cpp
2 // Recursive function
3 // Example: Find the point of zero by bisection
4 
5 // Version 3: func(x) and epsilon as parameters
6 // lambda: pass lambda function to Bisect
7 
8 #include <cassert> // assert()
9 #include <cmath>
10 #include <functional> // function; C++11
11 #include <iostream>
12 #include <vector>
13 using namespace std;
14 
21 double f(const double x) // declaration and
22 {
23  return sin(x) - 0.5 * x ; // definition of function f(x)
24 }
25 
32 double g(const double x) // declaration and
33 {
34  return -(x - 1.234567) * (x + 0.987654) ; // definition of function g(x)
35 }
36 
37 // declaration of Bisect3
52 double Bisect3(const std::function<double(double)>& func,
53  const double a, const double b, const double eps = 1e-6);
54 
63 double eval(vector<double> const &a, double x);
64 
65 int main()
66 {
67  // ...
68  const double EPS = 1e-6; // accuracy constant
69 
70  cout << endl;
71  cout << " Determine point of zero in [a,b] by bisection " << endl;
72 
73  double a, b;
74  cout << "f(a) > 0, a : ";
75  cin >> a;
76  cout << "f(b) < 0, b : ";
77  cin >> b;
78  cout << endl;
79 
80  //double x0 = Bisect3(f, a, b, EPS); // call recursive function with f
81  //double x0 = Bisect3(g,a,b,EPS); // call recursive function with g
82 
83  // Lamda 1
84  //double x0 = Bisect3([](const double x){return 2-x*x;},
85  //a, b, EPS);
86 
87  // Lambda 2
88  vector<double> koeff{-6.0, 1.0 , 1.0}; // x^2 + x -6 : {-3,2}
89 
90  double x0 = Bisect3([&koeff](const double x){return eval(koeff,x);},
91  a, b, EPS);
92 
93 
94  cout << endl << " point of zero = " << x0 << endl;
95  cout << endl;
96 
97  return 0;
98 }
99 
100 // ---------------------------------------------------------------
101 // Recursive function Bisect3
102 // ---------------------------------------------------------------
103 // definition of Bisect3
104 
105 double Bisect3(const std::function<double(double)>& func, const double a, const double b,
106  const double eps)
107 {
108  double x0;
109  double c = (a + b) / 2; // center of interval
110  double fc = func(c); // function value in center
111 
112  if ( std::abs(fc) < eps ) { // end of recursion
113  x0 = c;
114  }
115  else if ( fc > 0.0 ) {
116  x0 = Bisect3(func, c, b, eps); // search in the right intervall
117  }
118  else { // i.e., fc < 0.0
119  x0 = Bisect3(func, a, c, eps); // search in the left intervall
120  }
121 
122  return x0; // return the solution
123 }
124 
125 
126 
127 double eval(vector<double> const &a, double x)
128 {
129  assert(!a.empty());
130  double p=a[0];
131  for (size_t k=1; k<a.size(); ++k)
132  {
133  p += a[k]*pow(x,k);
134  }
135  return p;
136 }
137 
138 
139 
140 
141 
const double EPS
Definition: Bisect2.cpp:13
double Bisect3(const std::function< double(double)> &func, const double a, const double b, const double eps=1e-6)
Returns one solution for the equation with .
double f(const double x)
Calculates function .
double eval(vector< double > const &a, double x)
Evaluates the polynom at point x.
double g(const double x)
Calculates function .
int main()
b
Definition: bisect.m:12
define interval a
Definition: bisect.m:11