C Program For Regula Falsi Method

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.

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)=2x3-2x-5

Here,

x012
f(x)-5-57
Tabulation of given data

1st iteration :

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

Therefore, Root lies between x0=1 and x1=2.

x2=x0-f(x0)⋅x1-x0f(x1)-f(x0) => x2=1-(-5)⋅2-17-(-5) => x2=1.41667

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

2nd iteration :

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

Therefore, now, Root lies between x0=1.41667 and x1=2.

x3=x0-f(x0)⋅x1-x0f(x1)-f(x0) => x3=1.41667-(-2.14699)⋅2-1.416677-(-2.14699) => x3=1.55359

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

3rd iteration :

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

Therefore, Now, Root lies between x0=1.55359 and x1=2.

x4=x0-f(x0)⋅x1-x0f(x1)-f(x0) => x4=1.55359-(-0.60759)⋅2-1.553597-(-0.60759) =>x4=1.58924.

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

4th iteration :

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

Therefore Now, Root lies between x0=1.58924 and x1=2.

x5=x0-f(x0)⋅x1-x0f(x1)-f(x0) => x5=1.58924-(-0.15063)⋅2-1.589247-(-0.15063) => x5=1.59789.

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

5th iteration :

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

Therefore, Now, Root lies between x0=1.59789 and x1=2.

x6=x0-f(x0)⋅x1-x0f(x1)-f(x0) =>x6=1.59789-(-0.0361)⋅2-1.597897-(-0.0361) => x6=1.59996

Hence, f(x6)=f(1.59996)=2⋅1.599963-2⋅1.59996-5=-0.00858<0

6th iteration :

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

Therefore, Now, Root lies between x0=1.59996 and x1=2.

x7=x0-f(x0)⋅x1-x0f(x1)-f(x0) =>x7=1.59996-(-0.00858)⋅2-1.599967-(-0.00858) => x7=1.60045

Hence, f(x7)=f(1.60045)=2⋅1.600453-2⋅1.60045-5=-0.00203<0

7th iteration :

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

Therefore, Now, Root lies between x0=1.60045 and x1=2.

x8=x0-f(x0)⋅x1-x0f(x1)-f(x0)=> x8=1.60045-(-0.00203)⋅2-1.600457-(-0.00203) => x8=1.60056

Hence, f(x8)=f(1.60056)=2⋅1.600563-2⋅1.60056-5=-0.00048<0

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

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

nx0f(x0)x1f(x1)x2f(x2)Update
11-5271.41667-2.14699x0=x2
21.41667-2.14699271.55359-0.60759x0=x2
31.55359-0.60759271.58924-0.15063x0=x2
41.58924-0.15063271.59789-0.0361x0=x2
51.59789-0.0361271.59996-0.00858x0=x2
61.59996-0.00858271.60045-0.00203x0=x2
71.60045-0.00203271.60056-0.00048x0=x2
Tabulation of results calculated by the Regula Falsi Method.

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:

  1. Start
  2. Input two guesses: x1 and x2(these guesses should be such that the root of the equation falls between them)
  3. If guesses are invalid, program alerts the user to enter again.
  4. After that, Find f1=f(x1) and f2=f(x2).
  5. if f1f2>0 program is stopped
  6. else, find x0=x1-(f(x1)f(x2))/(f(x2)-f(x1)) and f0=f(x0)
  7. if f1f0<0, x2=x0 and f2=f0.
  8. else, x1=x0 and f1=f0
  9. 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.
  10. 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.

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

Output:

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

Regula Falsi Method Output

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.


Leave a Comment

Your email address will not be published. Required fields are marked *