Home » Numerical Techniques Using C++ » REGULA FALSI METHOD with C++ program

# REGULA FALSI METHOD with C++ program

## 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

## 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 :

Y-f(x0) = [f(x1)-f(x0)/(x1-x0)]*(X-x0).————>      (1)

This straight line cuts the x-axis at 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:

x2=x0-f(x0){(x1-x0)/(f(x0)-f(x1))} ————–> (2)

which is an approximation to the root. see more

We 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.

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

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.

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(x) 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

Now f(x2)=f( 0.0588)=-0.3982

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

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

x3=2.0813

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 .

## Regula Falsi Method C++ Program

```.hljs{display:block;overflow-x:auto;padding:.5em;color:#abb2bf;background:#282c34}.hljs-keyword,.hljs-operator{color:#f92672}.hljs-pattern-match{color:#f92672}.hljs-pattern-match .hljs-constructor{color:#61aeee}.hljs-function{color:#61aeee}.hljs-function .hljs-params{color:#a6e22e}.hljs-function .hljs-params .hljs-typing{color:#fd971f}.hljs-module-access .hljs-module{color:#7e57c2}.hljs-constructor{color:#e2b93d}.hljs-constructor .hljs-string{color:#9ccc65}.hljs-comment,.hljs-quote{color:#b18eb1;font-style:italic}.hljs-doctag,.hljs-formula{color:#c678dd}.hljs-section,.hljs-name,.hljs-selector-tag,.hljs-deletion,.hljs-subst{color:#e06c75}.hljs-literal{color:#56b6c2}.hljs-string,.hljs-regexp,.hljs-addition,.hljs-attribute,.hljs-meta-string{color:#98c379}.hljs-built_in,.hljs-class .hljs-title{color:#e6c07b}.hljs-attr,.hljs-variable,.hljs-template-variable,.hljs-type,.hljs-selector-class,.hljs-selector-attr,.hljs-selector-pseudo,.hljs-number{color:#d19a66}.hljs-symbol,.hljs-bullet,.hljs-link,.hljs-meta,.hljs-selector-id,.hljs-title{color:#61aeee}.hljs-emphasis{font-style:italic}.hljs-strong{font-weight:700}.hljs-link{text-decoration:underline}.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines) {white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```//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;
}``````

## Algorithm for Regula-falsi method:

remarks: x0 and x1 are two initial guesses to the root such that sign of f(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

Other Important Methods