Welcome to the newest post of geekstocode. Today for brushing up our coding skills, we will learn **C Program for Regula Falsi Method**.

This program can be asked in your mathematical papers as well as in coding.

Table of Contents

**What is Regula Falsi Method?**

Regula Falsi Method also called method of false position or the method of chords, is a numerical method for solving a polynomial equation with one unknown.

Above all, It is one of the oldest approaches similar to the bisection method algorithm. And, it uses binary search to find the unknown.

More reading on bisection method.

This is a method which follows trial and error technique of using the test values for the variable and after that the test values are adjusted according to the outcome.

Both these methods have the same purpose but, the regula falsi method is preferred because the bisection method converges at a slow speed.

The Regula Falsi Method is given by the following equation:

Where, we find point (c,0) when the secant line L joins the points (a, f (a)) and (b, f (b)) crosses the x-axis. After that, we find ‘c’ by solving two versions of equations of slope ‘m’.

**Example Problem of Regula Falsi Method:**

Let *us take the example of : f*(*x*)=2*x*^{3}-2*x*-5

Here,

x | 0 | 1 | 2 |
---|---|---|---|

f(x) | -5 | -5 | 7 |

### 1*s**t* iteration :

Here *f*(1)=-5<0 and *f*(2)=7>0

Therefore, Root lies between *x*0=1 and *x*1=2.

*x*2=*x*0-*f*(*x*0)⋅*x*1-*x*0*f*(*x*1)-*f*(*x*0) => *x*2=1-(-5)⋅2-17-(-5) => *x*2=1.41667

Hence, f(*x*2)=*f*(1.41667)=2⋅1.416673-2⋅1.41667-5=-2.14699<0

### 2*n**d* iteration :

Here *f*(1.41667)=-2.14699<0 and *f*(2)=7>0

Therefore, now, Root lies between *x*0=1.41667 and *x*1=2.

*x*3=*x*0-*f*(*x*0)⋅*x*1-*x*0*f*(*x*1)-*f*(*x*0) => *x*3=1.41667-(-2.14699)⋅2-1.416677-(-2.14699) => *x*3=1.55359

Hence, *f*(*x*3)=*f*(1.55359)=2⋅1.553593-2⋅1.55359-5=-0.60759<0.

### 3*r**d* iteration :

Here *f*(1.55359)=-0.60759<0 and *f*(2)=7>0.

Therefore, Now, Root lies between *x*0=1.55359 and *x*1=2.

*x*4=*x*0-*f*(*x*0)⋅*x*1-*x*0*f*(*x*1)-*f*(*x*0) => *x*4=1.55359-(-0.60759)⋅2-1.553597-(-0.60759) =>*x*4=1.58924.

Hence, *f*(*x*4)=*f*(1.58924)=2⋅1.589243-2⋅1.58924-5=-0.15063<0.

### 4*t**h* iteration :

Here *f*(1.58924)=-0.15063<0 and *f*(2)=7>0.

Therefore Now, Root lies between *x*0=1.58924 and *x*1=2.

*x*5=*x*0-*f*(*x*0)⋅*x*1-*x*0*f*(*x*1)-*f*(*x*0) => *x*5=1.58924-(-0.15063)⋅2-1.589247-(-0.15063) => *x*5=1.59789.

Hence, *f*(*x*5)=*f*(1.59789)=2⋅1.597893-2⋅1.59789-5=-0.0361<0.

### 5*t**h* iteration :

Here *f*(1.59789)=-0.0361<0 and *f*(2)=7>0

Therefore, Now, Root lies between *x*0=1.59789 and *x*1=2.

*x*6=*x*0-*f*(*x*0)⋅*x*1-*x*0*f*(*x*1)-*f*(*x*0) =>*x*6=1.59789-(-0.0361)⋅2-1.597897-(-0.0361) => *x*6=1.59996

Hence, *f*(*x*6)=*f*(1.59996)=2⋅1.599963-2⋅1.59996-5=-0.00858<0

### 6*t**h* iteration :

Here *f*(1.59996)=-0.00858<0 and *f*(2)=7>0

Therefore, Now, Root lies between *x*0=1.59996 and *x*1=2.

*x*7=*x*0-*f*(*x*0)⋅*x*1-*x*0*f*(*x*1)-*f*(*x*0) =>*x*7=1.59996-(-0.00858)⋅2-1.599967-(-0.00858) => *x*7=1.60045

Hence, *f*(*x*7)=*f*(1.60045)=2⋅1.600453-2⋅1.60045-5=-0.00203<0

### 7*t**h* iteration :

Here *f*(1.60045)=-0.00203<0 and *f*(2)=7>0

Therefore, Now, Root lies between *x*0=1.60045 and *x*1=2.

*x*8=*x*0-*f*(*x*0)⋅*x*1-*x*0*f*(*x*1)-*f*(*x*0)=*> *x8=1.60045-(-0.00203)⋅2-1.600457-(-0.00203) => *x*8=1.60056

Hence, *f*(*x*8)=*f*(1.60056)=2⋅1.600563-2⋅1.60056-5=-0.00048<0

Approximate root of the equation 2*x*3-2*x*-5=0 using False Position method is 1.60056.

If we finally tabulate the data, we will get these results:

n | x0 | f(x0) | x1 | f(x1) | x2 | f(x2) | Update |

1 | 1 | -5 | 2 | 7 | 1.41667 | -2.14699 | x0=x2 |

2 | 1.41667 | -2.14699 | 2 | 7 | 1.55359 | -0.60759 | x0=x2 |

3 | 1.55359 | -0.60759 | 2 | 7 | 1.58924 | -0.15063 | x0=x2 |

4 | 1.58924 | -0.15063 | 2 | 7 | 1.59789 | -0.0361 | x0=x2 |

5 | 1.59789 | -0.0361 | 2 | 7 | 1.59996 | -0.00858 | x0=x2 |

6 | 1.59996 | -0.00858 | 2 | 7 | 1.60045 | -0.00203 | x0=x2 |

7 | 1.60045 | -0.00203 | 2 | 7 | 1.60056 | -0.00048 | x0=x2 |

**Algorithm :**

Now, to understand any program, we understand algorithm first. So, here is the algorithm followed by the Regula Falsi method.

This will make the program easier to explain and thus will help you to code better. The algorithm is given below:

- Start
- Input two guesses: x1 and x2(these guesses should be such that the root of the equation falls between them)
- If guesses are invalid, program alerts the user to enter again.
- After that, Find f1=f(x1) and f2=f(x2).
- if f1f2>0 program is stopped
- else, find x0=x1-(f(x1)f(x2))/(f(x2)-f(x1)) and f0=f(x0)
- if f1f0<0, x2=x0 and f2=f0.
- else, x1=x0 and f1=f0
- if mod of (x2-x1)/x2 is less or equal to error criterion i.e e, then print root and stop 10. else, goto step 6.
- Stop

**C Program** **For Regula Falsi Method:**

```
//C Program for regula falsi method
#include<stdio.h>
#include<math.h>
#include<conio.h>
#define f(x) x*x-4*x-10
#define e 0.0001
void main()
{
float x0,x1,x2,f0,f1,f2,root;
int i=1;
printf("Regula Falsi Method to find root of equation x^2-4x-10=0\n");
printf("Enter values of x1 and x2\n");//defining the two initial guesses.
a:printf("x1: ");
scanf("%f",&x1);
printf("x2: ");
scanf("%f",&x2);
f1=f(x1);
f2=f(x2);
if((f1f2)>0) { printf("Enter Again\n"); goto a; }
else { printf("steps\tx0\tf0\tx1\tf(x1)\tx2\tf(x2)\n");
b:x0=x1-((f1(x2-x1))/(f2-f1));
f0=f(x0);
printf("%d\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\n",i,x0,f0,x1,f1,x2,f2);
i++;
}
if((f1*f0)<0)
{
x2=x0;
f2=f0;
}
else
{
x1=x0;
f1=f0;
}
if(fabs(f(x0))<=e)
{
root=x0;
printf("The root is %.4f",root);
goto c;
}
else
{
goto b;
}
c:
getch();
}
```

**Program Explanation**:

The code above first initialises required value by user interference. However, here x1 and x2 are entered by the user according to the roots.

The program then follows the above algorithm and uses if-else statement to calculate the desired assumptions.

Note: You can also use cosine and logarithmic function for coding the solution of Regula Falsi Method. However, they are much difficult to understand.

To make it more comfortable to understand, let us follow detailed steps of the code.

- For instance, we are defining a pre-defined equation. However, you can modify this equation according to your wishes.
- After that, we declare variables x0,f0,f1,f2, x1, x2 and root for the final answer.
- Since the regula falsi method takes two initial guesses, we will ask the user to enter them.
- We calculate f1 and f2 by calling f() function.
- We enter values again if f1 and f2 are greater than 0.
- Else we calculate the value of b.
- Similarly, we initialize f0 through f() function.
- Thus, we calculate more values through the formula and iterate the loop for the number of assumptions.
- Finally, we display the result in the form of a table.

**Output:**

Here is how the output and input of the program looks like:

**Conclusion:**

The output of the above program will give the values of x0, f0, x1, f(x1), x2 and f(x2) for the given approximations and thus calculate the final root of the given equation.

In addition, this code of c program for regula falsi method is an efficient and easy to understand way to implement the Regula Falsi method in c. In other words, It will provide high accuracy.