C Program for Regula Falsi Method

3 Min Read

Regula Falsi method, also known as the false position method, is the oldest approach to find the real root of a function. It is a closed bracket method and closely resembles the bisection method.

The C Program for regula falsi method requires two initial guesses of opposite nature. Like the secant method, interpolation is done to find the new values for successive iterations, but in this method one interval always remains constant.

The programming effort for Regula Falsi or False Position Method in C language is simple and easy. The convergence is of first order and it is guaranteed. In manual approach, the method of false position may be slow, but it is found superior to the bisection method.

Features of Regula Falsi Method:

  • Type – closed bracket
  • No. of initial guesses – 2
  • Convergence – linear
  • Rate of convergence – slow
  • Accuracy – good
  • Programming effort – easy
  • Approach – interpolation

Below is a short and simple source code in C program for regula falsi method to find the root of cos(x) – x*e^x. Here, x0 and x1 are the initial guesses taken.

Variables:

  • itr – a counter which keeps track of the no. of iterations performed
  • maxmitr – maximum number of iterations to be performed
  • x0, x1 – the limits within which the root lies
  • x2 – the value of root at nth iteration
  • x3 – the value of root at (n+1)th iteration
  • allerr – allowed error
  • x – value of root at nth iteration in the regula function
  • f(x0), f(x1) – the values of f(x) at x0 and x1 respectively

f(x) = cos(x) – x*e^x

Source Code for Regula Falsi Method in C:

#include<stdio.h>
#include<math.h>
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);
    printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
void main ()
{
    int itr = 0, maxmitr;
    float x0,x1,x2,x3,allerr;
    printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
    scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
    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) < allerr)
        {
            printf("After %d iterations, root = %6.4f\n", itr, x3);
            return 0;
        }
        x2=x3;
    }
    while (itr<maxmitr);
    printf("Solution does not converge or iterations not sufficient:\n");
    return 1;
}

Input/Output:


Also see,
Regula Falsi Method MATLAB Program
Regula Falsi Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation

If you have any questions regarding the Regula Falsi Method (False Position Method) or its source code in C programming presented above, mention them in the comments below.

Share This Article
4 Comments

Leave a Reply

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

English
Exit mobile version