C program of Regula Falsi method to find root

C program of Regula Falsi method to find root

720
1
SHARE

C program of Regula Falsi method to find root of polynomials : The Regula–Falsi Method is a numerical method for estimating the roots of a polynomial f(x).   A value x replaces the midpoint in the Bisection Method and serves as the new approximation of a root of f(x).   The objective is to make convergence faster.   Assume that f(x) is continuous.

Algorithm for the Regula–Falsi Method: Given a continuous function f(x)

  1. Find points a and b such that a < b and f(a) * f(b) < 0.
  2. Take the interval [a, b] and determine the next value of x1.
  3. If f(x1) = 0 then x1 is an exact root, else if f(x1) * f(b) < 0 then let a = x1, else if f(a) * f(x1) < 0 then let b = x1.
  4. Repeat steps 2 & 3 until f(xi) = 0 or |f(xi)| £ DOA, where DOA stands for degree of accuracy.

Now let us see how to write a which accepts Maximum power of X , co-efficient of each x^ and Interval lower & upper bound as input and then displays all the iterations & calculates the root of the polynomial.

For Example : Consider f(x) = x3 + 3x – 5, where [ a = 1, b = 2 ] and DOA = 0.001

Now Maximum power of X =3, Coeffients x^0 = -5,   x^1 = 3,  x^2 = 0,  x^3 = 1 , Interval lower bound  = 1 and Interval upper bound =2. (Note : Follow these conventions else code may not produce proper results).

C program of Regula Falsi method to find root of polynomials :

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define DOA 0.001

int user_power,i=0,cnt=0,flag=0;
int coef[10]={0};
float x1,x2,x3=0,t=0;
float fx1=0,fx2=0,fx3=0,temp=0;
int check()
{
    printf("\n\n\tInterval lower bound a = ");
    scanf("%f",&x1);

    printf("\n\tInterval upper bound b = ");
    scanf("%f",&x2);

    fx1=fx2=fx3=0.0;

    for(i=user_power;i>=1;i--)
    {
            fx1+=coef[i] * (pow(x1,i)) ;
            fx2+=coef[i] * (pow(x2,i))  ;
    }
    fx1+=coef[0];
    fx2+=coef[0];
    if( (fx1*fx2)>0)
    {
        printf("\n\tInterval lower and upper bounds are not perfect.");
        return(1);
    }
    return(0);
}

int main()
{
    printf("-----------------------------------------------------------\n");
    printf("----------------------Made by C code champ-----------------\n");
    printf("-----------------------------------------------------------\n\n");
    printf("\n\n\t   PROGRAM FOR REGULAR-FALSI METHOD\n");

    printf("\n\n\nENTER THE MAXIMUM POWER OF X = ");
    scanf("%d",&user_power);

    for(i=0;i<=user_power;i++)
    {
        printf("\n\t x^%d = ",i);
        scanf("%d",&coef[i]);
    }

    printf("\n");

    printf("\n\n\t THE POLYNOMIAL IS = ");
    for(i=user_power;i>=0;i--)/*for printing coefficients*/
    {
        printf(" %dx^%d",coef[i],i);
    }

    while(1)
    {
        if(check()==0)
        {
            flag=1;
            break;
        }
        check();
    }

    printf("-----------------------------------------------------------\n");
    printf("\n ITERATION    a       f(a)      b      f(b)      x       f(x)   \n");
    printf("-----------------------------------------------------------\n");

    if(flag==1)
    {

        do
        {
            cnt++;
            fx1=fx2=fx3=0;
            for(i=user_power;i>=1;i--)
            {
                fx1+=coef[i] * (pow(x1,i)) ;
                fx2+=coef[i] * (pow(x2,i))  ;
            }
            fx1+=coef[0];
            fx2+=coef[0];
            temp=x3;
            x3=((x1*fx2)-(x2*fx1))/(fx2-fx1);

            for(i=user_power;i>=1;i--)
            {
                fx3+=coef[i]*(pow(x3,i));
            }
            fx3+=coef[0];

            printf("\n \t%d     %.4f  %.4f  %.4f  %.4f  %.4f  %.4f",cnt,x1,fx1,x2,fx2,x3,fx3);
            t=fx1*fx3;
            if(t>0)
            {
                x1=x3;

            }
            if(t<0)
            {
                x2=x3;
            }
            fx3=0;
        }while((fabs(temp-x3))>=DOA);
        printf("\n\n\n\tROOT OF POLYNOMIAL EQUATION IS = %f",x3);
    }
    getch();
}

We hope you all have enjoyed the to find root. If you have any queries related to the code ask us in form of comments.

1 COMMENT

  1. Great post. I was checking constantly this blog and I’m impressed! Extremely helpful info particularly the last part :) I care for such information a lot. I was seeking this certain information for a very long time. Thank you and best of luck.

LEAVE A REPLY