#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
float f=pow(x,3)-18;
return(f);
}
void main()
{ clrscr();
cout<<"the given equation is x^3-18:"<<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*f(x2)-x2*f(x1))/(f(x2)-f(x1));
c=f(x1)*f(x0);
if(c<0)
x1=x0;
else if(c>0)
x2=x0;
n++;
if(c==0)
break;
xm=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
}
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^3-18:
enter the interval(a,b)
enter x1=2
enter x2=3
the value of f(2):-10
the value of f(3):9
the value of f(9.490434e-41):-18root of the given equation on given tolerance is
2.620708
no. of iteration4
Comments :
Post a Comment