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

Method Of false Position (Regula-Falsi method)

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

cout<<"Enter max No of iterations: ";
cin>>max;
cout<<setprecision(6)<<"Iterations\tRoot\n";
x2=regula(x0,x1);
while(i<max)
{
cout<<i<<"\t\t"<<x2<<endl;
if(f(x0)*f(x2)<0)
x1=x2;
else
x0=x2;
x3=regula(x0,x1);
i++;
if(fabs(x3-x2)<err)
{
cout<<"\nRoot after "<<i<<" iterations="<<setprecision(5)<<regula(x0,x1)<<endl;
return 0;
}
x2=x3;
}
cout<<"\nSolution Does not cover: "<<i<<" iteration not sufficient";
return 1;
}

No comments:

Post a Comment