Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bisection Method for the function is x^3 - x -1
- #include<bits/stdc++.h>
- using namespace std;
- double func(double x)
- {
- return x*x*x - x-1;
- }
- void bisection(double a, double b)
- {
- if (func(a) * func(b) >= 0)
- {
- cout << "You didn't took right a and b"<<endl;
- return;
- }
- double c;
- while (abs(b-a) >= 0.01)
- {
- // Find middle point
- c = (a+b)/2;
- if (func(c)< 0)
- b = c;
- else
- a = c;
- }
- cout << "The value of root is : " << c<<endl;
- }
- int main()
- {
- // Initial values for a and b
- double b =1, a = 2;
- bisection(a, b);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment