BISECTION METHOD-
if you have an equation like f(x)=x*e^x-1then to find it's one root we will use bisection method
choose a & b such that f(a)=-ve & f(b)=+ve let a=0 &b=1
make a table
n a b x=(a+b)/2 f(x)
0 0 1 0.5 -ve
1 0.5 1 0.75 find f(x)
if f(x) is +ve then x=b if -ve , x=a .if 0 then x will be a root.
solve further up to 12 iterations means till n=12
and then whatever will be the value of x will be a approximate root.
code is given below
#include<iostream.h>
#include<conio.h>
#include<math.h>
float e=2.718;
float f(float x)
{
float f=x*pow(e,x)-1;
return(f);
}
void main()
{ clrscr();
cout<<"the given equation is x*e^x-1:"<<endl;
float x1,x2,x0,c,xm,n;
cout<<"enter the interval(a,b)"<<endl;
cout<<"\n enter x1=";
cin>>x1;
cout<<"\n enter x2=";
cin>>x2;
cout<<"\n the value of f("<<x1<<"):"<<f(x1);
cout<<"\n the value of f("<<x2<<"):"<<f(x2);
cout<<"\n the value of f("<<x0<<"):"<<f(x0);
if (f(x1)*f(x2)<0)
{do
{x0=(x1+x2)/2;
c=f(x1)*f(x0);
if(c<0)
x2=x0;
else if(c>0)
x1=x0;
n++;
if(c==0)
break;
xm=(x1+x2)/2;
}
while(fabs(xm-x0)>=0.0001);
cout<<"root of the given equation on given tolerance is"<<x0<<endl;
cout<<"no. of iteration"<<n<<endl;
}
else
cout<<"can not found in the given inter val";
getch();
}
OUTPUT-
the given equation is x*e^x-1:
enter the interval(a,b)
enter x1=0
enter x2=1
the value of f(0):-1
the value of f(1):1.718
the value of f(9.490434e-41):-1root of the given equation on given tolerance is
0.567261
no. of iteration13
Comments :
Post a Comment