Menu Close

Regula Falsi Method C++

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

float f(float x)
{
return cos(x)-x*exp(x);
}

void regula(float *x,float x0,float x1,float fx0,float fx1,int *itr)
{
*x=x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
cout<<"Iteration no."<<setw(3)<<*itr;
cout<<"X= "<<setw(7)<<setprecision(5);
cout<<*x<<endl;
}
int main()
{
int itr=0,maxitr;
float x0,x1,x2,x3,aerr;
cout<<"Enter the value for x0,x1,";
cout<<"allowed error, maximum iterations"<<endl;
cin>>x0>>x1>>aerr>>maxitr;
regula(&x2,x0,x1,f(x0),f(x1),&itr);
cout<<fixed;

do
{
if(f(x0)*f(x2)<0)
x1=x2;
else
x0=x2;
regula(&x3,x0,x1,f(x0),f(x1),&itr);

if(fabs(x3-x2)<aerr)
{
cout<<"After "<<itr<<" iterations,";
cout<<"root="<<setw(6)<<setprecision(4);
cout<<x3<<endl;
return 0;
}
x2=x3;
}while(itr<maxitr);
cout<<"Solution does not converge,";
cout<<"iterations not sufficient"<<endl;
return 1;
}

Output

Enter the value for x0,x1,allowed error, maximum iterations
0 1 0.0001 15
Iteration no. 1X= 0.31467
Iteration no. 2X= 0.44673
Iteration no. 3X= 0.49402
Iteration no. 4X= 0.50995
Iteration no. 5X= 0.51520
Iteration no. 6X= 0.51692
Iteration no. 7X= 0.51748
Iteration no. 8X= 0.51767
Iteration no. 9X= 0.51773
After 9 iterations,root=0.5177

More Related Stuff