All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Friday, 13 February 2015

Secant Method of Solving Transcidental Equation

#include<iostream>
#include<math.h>
#include<iomanip>
#define err 0.0001
//Correction upto three places
using namespace std;
float f(float x)
{
return x*x*x-2*x-5; //Place your Function here
}
float secant(float x0,float x1)
{
return x1-((x1-x0)/(f(x1)-f(x0))*f(x1));
}
int main()
{
int i=2;
float x0,x1,x2,x3;
cout<<"Enter two initial approximate roots x0,x1 : ";
cin>>x0>>x1;

cout<<setprecision(6)<<"Iterations\tRoot\n";
x2=secant(x0,x1);
while(fabs(x2-x1)>err)
{
cout<<"X"<<i<<"\t\t"<<x2<<endl;
x3=secant(x1,x2);
x1=x2;
x2=x3;
i++;
}
cout<<"\nAfter "<<i-1<<" iterations Root ="<<x2;
return 0;
}

No comments:

Post a Comment