-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
bisection_method.cpp
75 lines (65 loc) · 2 KB
/
bisection_method.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* \file
* \brief Solve the equation \f$f(x)=0\f$ using [bisection
* method](https://en.wikipedia.org/wiki/Bisection_method)
*
* Given two points \f$a\f$ and \f$b\f$ such that \f$f(a)<0\f$ and
* \f$f(b)>0\f$, then the \f$(i+1)^\text{th}\f$ approximation is given by: \f[
* x_{i+1} = \frac{a_i+b_i}{2}
* \f]
* For the next iteration, the interval is selected
* as: \f$[a,x]\f$ if \f$x>0\f$ or \f$[x,b]\f$ if \f$x<0\f$. The Process is
* continued till a close enough approximation is achieved.
*
* \see newton_raphson_method.cpp, false_position.cpp, secant_method.cpp
*/
#include <cmath>
#include <iostream>
#include <limits>
#define EPSILON \
1e-6 // std::numeric_limits<double>::epsilon() ///< system accuracy limit
#define MAX_ITERATIONS 50000 ///< Maximum number of iterations to check
/** define \f$f(x)\f$ to find root for
*/
static double eq(double i) {
return (std::pow(i, 3) - (4 * i) - 9); // original equation
}
/** get the sign of any given number */
template <typename T>
int sgn(T val) {
return (T(0) < val) - (val < T(0));
}
/** main function */
int main() {
double a = -1, b = 1, x, z;
int i;
// loop to find initial intervals a, b
for (int i = 0; i < MAX_ITERATIONS; i++) {
z = eq(a);
x = eq(b);
if (sgn(z) == sgn(x)) { // same signs, increase interval
b++;
a--;
} else { // if opposite signs, we got our interval
break;
}
}
std::cout << "\nFirst initial: " << a;
std::cout << "\nSecond initial: " << b;
// start iterations
for (i = 0; i < MAX_ITERATIONS; i++) {
x = (a + b) / 2;
z = eq(x);
std::cout << "\n\nz: " << z << "\t[" << a << " , " << b
<< " | Bisect: " << x << "]";
if (z < 0) {
a = x;
} else {
b = x;
}
if (std::abs(z) < EPSILON) // stoping criteria
break;
}
std::cout << "\n\nRoot: " << x << "\t\tSteps: " << i << std::endl;
return 0;
}