Home » Numerical Techniques Using C++ » Regula Falsi Method with C++ Program | Example & Algorithm

Regula Falsi Method with C++ Program | Example & Algorithm

Method of False Position

Regula-Falsi method also called the Method of False Position, closely resembles the Bisection method.
This is the oldest method of finding the real root of an equation.
Look at the picture below

Regula Falsi Method C++ Program

//Regula Falsi method //wikkihut.com #include<iostream> #include<math.h> 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); } int main() { int itr=0, maxitr; float x0,x1,x2,x3,aerr; cout<<"Enter the values of x0,x1, allowed error , maximum iterations"<<endl; cin>>x0>>x1>>aerr>>maxitr; regula(&x2,x0,x1,f(x0),f(x1),&itr); 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, root = "<<x3<<endl; return 0; } x2=x3; } while(itr<maxitr) cout<<"Solution does not converge"<<endl; cout<<"Iterations are not sufficient"<<endl; return 1; }

Example

Find the Roots of f(x)=x3-2x-5

let f(x)=x3-2x-5 and we have to find its real root correct to three decimal places.

if we put x=2 and x=3, we find f(2) is negative and f(3) is positive.
it means the root lies between 2 and 3.

therefore, taking
x0=2
x1=3
f(x0)=-1
and
f(x1)=16,
in equation (2) we get
x2=0.0588

i.e., the root lies between 2.0588 and 3.

By repeating this process, the successive approximations are

x4=2.0682 x5=2.0915
.
.
.
x7=2.0941 and
x8=2.0943
and therefore the root is 2.094 .

Graphical explanation.

As in picture we use two points x0 and x1 such that f(x0) and f(x1) have opposite sign,
that is the graph
y=f(x)
cuts the x-axis at least once while going from x0 to x1, which indicates that the root lies between x0 and x1 .

Now we connect the two points f(x0) and f(x1) by a straight line.

The equation of straight line joining these points is :

This straight line cuts the x-axis at point x2,
this point where the line (1) cuts the x-axis is taken as an approximation to the root.

At this point Y=0 and X=x2, put value of Y and X in (1) we get:

which is an approximation to the root. see more

Find f(x2).
If f(x2) and f(x0) are of opposite signs then we replace x1 by x2 and draw a straight line connecting
f(x2) to f(x0)
to find the new intersection point.

If f(x2) and f(x0) are of the same sign then x0 is replaced by x2 and proceed as before.
In both cases the new interval of search is smaller than the initial interval and ultimately convergence is guaranteed.

Algorithm for Regula-falsi method:

  1. read x0,x1,e,n
    remarks: x0 and x1 are two initial guesses to the root such that sign of f

therefore, taking
x0=2.0588
x1=3
f(x0)=-0.3908 and
f(x1)=16, in equation (2) we get

x3=2.0813

  1. (x0) and f(x1) are not same.
  2. f0=f(x0)
  3. f1=f(x1)
  4. for i=1 to n in steps of 1 do
  5. x2=(x0f1-x1f0)/(f1-f0)
  6. f2=f(x2)
  7. if |f2|<e then
  8. begin Write ‘convergent solution’, x2,f2
  9. stop end
  10. if sign (f2)=sign f(0)
  11. then begin x0=x2
  12. f0=f2
  13. else begin x1=x2
  14. f1=f2
  15. end
  16. endfor
  17. write ‘Does not converge in n iterations’
  18. write x3,f2
  19. stop

Suggested Read: