2. False position method(regula falsi method)
//equation x^3-x-1
#include<bits/stdc++.h>
using namespace std;
#define maxval 1000000
double val(double x)
{
return x*x*x - x - 1;
}
void false_position(double a,double b)
{
if(val(a)*val(b)>0)
{
cout<<"Wrong input\n";
return ;
}
double root;
root=a;
for(int i=0;i<maxval;i++)
{
root=((a*val(b))-(b*val(a)))/(val(b)-val(a));
// cout<<root<<endl;
if(val(root)==0)break;
else if(val(root)*val(a)<0)b=root;
else a=root;
}
cout<<"The root : "<<root<<endl;
}
int main()
{
cout<<"Enter two val : ";
double x,y;
cin>>x>>y;
false_position(x,y);
return 0;
}
0 Comments