The bisection method is used to find the roots of a polynomial equation. It separates the interval and subdivides the interval in which the root of the equation lies. The principle behind this method is the intermediate theorem for continuous functions. It works by narrowing the gap between the positive and negative intervals until it closes in on the correct answer. This method narrows the gap by taking the average of the positive and negative intervals. For any continuous function f(x), i. Find TWO (2) points, say a and b such that a < b and f(a)* f(b) < 0 ii. Find the midpoint of a and b, say “t” iii. t is the root of the given function if f(t) = 0; else follow the next step iv. Divide the interval [a, b] v. If f(t)*f(b) <0, let a = t vi. Else if f(t) *f(a), let b = t vii. Repeat above three steps until f(t) = 0.
#include <iostream>
using namespace std;
float f(float x){
return x * x * x - x * x + 2;
}
int main(){
//using an example of f(x) = x^3 - x^2 + 2
float a = -500, b = 100, t;
if(a < b && f(a) * f(b) < 0){
while(f(a) * f(b) < 0){
t = (a + b)/2;
if(f(t) == 0.0) break;
else if(f(t) * f(b) < 0) a = t;
else if(f(t) * f(a) < 0) b = t;
}
cout<<"Root of x^3 - x^2 + 2: "<<t;
}
else cout<<"f(a) * f(b) > 0";
return 0;
}
Comments
Leave a comment