Regula Falsi Method Using C

Earlier in Regula Falsi or False Position Method Algorithm and Regula Falsi or False Position Method Pseudocode , we discussed about an algorithm and pseudocode for computing real root of non-linear equation using Regula Falsi or False Position Method. In this tutorial we are going to implement Regula Falsi or False Position Method using C programming language.

Complete Program for Regula Falsi or False Position Method using C Programming Language


/* Program: Finding real roots of nonlinear
   equation using Regula Falsi or False Position Method
   Author: CodeSansar
   Date: November 18, 2018 */
/* Header Files */
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining equation to be solved.
   Change this equation to solve another problem. */
#define   f(x)   x*log10(x) - 1.2

int main()
{
	
	 float x0, x1, x2, f0, f1, f2, e;
	 int step = 1;
	 clrscr();
	 /* Inputs */
	 up:
	 printf("\nEnter two initial guesses:\n");
	 scanf("%f%f", &x0, &x1);
	 printf("Enter tolerable error:\n");
	 scanf("%f", &e);
	 /* Calculating Functional Values */
	 f0 = f(x0);
	 f1 = f(x1);
	 /* Checking whether given guesses brackets the root or not. */
	 if( f0*f1 > 0.0)
	 {
		  printf("Incorrect Initial Guesses.\n");
		  goto up;
	 }
	 /* Implementing Regula Falsi or False Position Method */
	 printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
	 do
	 {
		  x2 = x0 - (x0-x1) * f0/(f0-f1);
		  f2 = f(x2);
		  printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);
		
		  if(f0*f2 < 0)
		  {
			   x1 = x2;
			   f1 = f2;
		  }
		  else
		  {
			   x0 = x2;
			   f0 = f2;
		  }
		  step = step + 1;
	
	 }while(fabs(f2)>e);

	 printf("\nRoot is: %f", x2);
	 getch();
	 return 0;
}

Output: Regula Falsi or False Position Method Using C

Enter two initial guesses:
2
3
Enter tolerable error:
0.000001

Step            x0              x1              x2              f(x2)
1               2.000000        3.000000        2.721014        -0.017091
2               2.721014        3.000000        2.740206        -0.000384
3               2.740206        3.000000        2.740636        -0.000009
4               2.740636        3.000000        2.740646        -0.000000

Root is: 2.740646

Recommended Readings

  1. Regula Falsi or False Position Method Algorithm
  2. Regula Falsi or False Position Method Pseudocode
  3. Regula Falsi or False Position Method Using C
  4. Regula Falsi or False Position Method Using MATLAB with Output
  5. Regula Falsi or False Position Method Online Calculator