All rights are reserved by Niteesh Kumar.. Theme images by Storman. Powered by Blogger.

Program for solving equation in numerical techniques by secant method using c++.

#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
float f=pow(x,3)+x-1;
return(f);
}
void main()
{ clrscr();
cout<<"the given equation is x^3+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*f(x2)-x2*f(x1))/(f(x2)-f(x1));
c=f(x1)*f(x0);
x1=x2;
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
getch();
}
OUTPUT-
the given equation is x^3+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
the value of f(9.490434e-41):-1root of the given equation on given tolerance is
0.682326

no. of iteration5