Write the program for finding aprroxiamate root of a function by Bisection method
The method is applicable for numerically solving the equation f(x) = 0 for the real variable x, where f is a continuous function defined on an interval [a, b] and where f(a) and f(b) have opposite signs. In this case a and b are said to bracket a root since, by the intermediate value theorem, the continuous function f must have at least one root in the interval (a, b).
At each step the method divides the interval in two by computing the midpoint c = (a+b) / 2 of the interval and the value of the function f(c) at that point. Unless c is itself a root (which is very unlikely, but possible) there are now only two possibilities: either f(a) and f(c) have opposite signs and bracket a root, or f(c) and f(b) have opposite signs and bracket a root.[5]
The method selects the subinterval that is guaranteed to be a bracket as the new interval to be used in the next step. In this way an interval that contains a zero of f is reduced in width by 50% at each step. The process is continued until the interval is sufficiently small.
Explicitly, if f(a) and f(c) have opposite signs, then the method sets c as the new value for b, and if f(b) and f(c) have opposite signs then the method sets c as the new a. (If f(c)=0 then c may be taken as the solution and the process stops.) In both cases, the new f(a) and f(b) have opposite signs, so the method is applicable to this smaller interval.
#include<iostream>
#include<math.h>
using namespace std;
float func(float x) //User Define Funtion
{ float m= x*x*x-x-11;
return m; }
int main()
{ float a, b,c;
a = 2;
b = 3;
cout<<"Funtion is X^3-X-11"<<endl;
cout <<"initialization by two numbers "<<endl;
cout<<"First initialization number = 2"<<endl;
cout<<"secound initialization number = 3"<<endl;
if( func(a)*func(b)>=0 )
{ cout<<"Roots are not present between initialization"<<endl;
return 0;
}
for(int i=1;i<10;i++)
{ c= (a+b)/2.0;
if (func(c)*func(b)<0)
{ a=c; }
else { b=c; }
if(func(c)==0)
{break;}
}
cout<<"\nApprox root of this function between 2 and 3 by using bisected method is "<<c;
return 0;
}
- click on below link and run this program
- https://onlinegdb.com/SyYBY9gr_
https://onlinegdb.com/Bkdv4ceHd
Comments
Post a Comment
If you have any doubt ,let me know