Loading...
Searching...
No Matches
Solve the equation \(f(x)=0\) using bisection method More...
#include <cmath>
#include <iostream>
#include <limits>
Go to the source code of this file.
static double eq (double i) template<typename T> int sgn (T val) int main ()Solve the equation \(f(x)=0\) using bisection method
Given two points \(a\) and \(b\) such that \(f(a)<0\) and \(f(b)>0\), then the \((i+1)^\text{th}\) approximation is given by:
\[x_{i+1} = \frac{a_i+b_i}{2} \]
For the next iteration, the interval is selected as: \([a,x]\) if \(x>0\) or \([x,b]\) if \(x<0\). The Process is continued till a close enough approximation is achieved.
Definition in file bisection_method.cpp.
◆ EPSILON ◆ MAX_ITERATIONS #define MAX_ITERATIONS 50000 ◆ eq()define \(f(x)\) to find root for
Definition at line 26 of file bisection_method.cpp.
26 {
27 return (std::pow(i, 3) - (4 * i) - 9);
28}
◆ main()main function
Definition at line 37 of file bisection_method.cpp.
37 {
38 double a = -1, b = 1, x, z;
39 int i;
40
41
46 b++;
47 a--;
48 } else {
49 break;
50 }
51 }
52
53 std::cout << "\nFirst initial: " << a;
54 std::cout << "\nSecond initial: " << b;
55
56
58 x = (a + b) / 2;
60 std::cout << "\n\nz: " << z << "\t[" << a << " , " << b
61 << " | Bisect: " << x << "]";
62
63 if (z < 0) {
64 a = x;
65 } else {
66 b = x;
67 }
68
69 if (std::abs(z) < EPSILON)
70 break;
71 }
72
73 std::cout << "\n\nRoot: " << x << "\t\tSteps: " << i << std::endl;
74 return 0;
75}
#define MAX_ITERATIONS
Maximum number of iterations to check.
static double eq(double i)
◆ sgn()get the sign of any given number
Definition at line 32 of file bisection_method.cpp.
32 {
33 return (T(0) < val) - (val < T(0));
34}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4