Monday, June 12, 2017

Regula Falsi Method

Regula Falsi Method

Regula Falsi Method is also known as "false position method" is a bracketing method used to solve equations of form f(x)=0Interpolation is the approach of this method to find the root of nonlinear equations by finding new values for successive iterations. In this method, unlike the secant method, one interval always remains constant.

I am gonna solve f(x)=x^2-4x-10 using this method in C++...

Algorithm:

  1. Start
  2. Read values of x0, x1 and e
    *Here x0 and x1 are the two initial guesses
    e is the degree of accuracy or the absolute error i.e. the stopping criteria*
  3. Computer function values f(x0) and f(x1)
  4. Check whether the product of f(x0) and f(x1) is negative or not.
    If it is positive take another initial guesses.
    If it is negative then goto step 5.
  5. Determine:
    x = [x0*f(x1) – x1*f(x0)] / (f(x1) – f(x0))
  6. Check whether the product of f(x1) and f(x) is negative or not.
    If it is negative, then assign x0 = x;
    If it is positive, assign x1 = x;
  7. Check whether the value of f(x) is greater than 0.00001 or not.
    If yes, goto step 5.
    If no, goto step 8.
    *Here the value 0.00001 is the desired degree of accuracy, and hence the stopping criteria.*
  8. Display the root as x.
  9. Stop

Program

#include <iostream>
#include<cmath>
#include<iomanip>

using namespace std;

double fun(double x)
{
  return(x*x-4*x-10);
}

const double TOL=1e-5;

int main()
{
    double x3,x1,x2,xs,f1,f2,f3;
    int iter=0;
    cout << "Enter brackting numbers x1, x2" << endl;
    cin>>x1>>x2;

    f1=fun(x1);
    f2=fun(x2);

    if((f1*f2)>0)
        {
            cout<<"\nDoesn't bracket...\n";
         }
    else
    {
        do
        {
            xs=x3;
            x3=x1-(f1*(x2-x1))/(f2-f1);
            f3=fun(x3);
            iter++;

            cout<<setprecision(10)<<setw(3)<<iter<<setw(15)<<x1<<setw(15)<<x2<<setw(15)<<fun(x3)<<endl;

            if((f1*f3)<0)

                x2=x3;
            else
                x1=x3;
                f1=fun(x1);
                f2=fun(x2);
        }while(fabs(fun(x3))>=TOL);//Terminating case
    }
    cout<<"\nThe root of the equation is :"<<x3;
    cout<<"\n\nf(x)="<<fun(x3);
    return 0;
}

OUTPUT




I have uploaded video on this topic :https://youtu.be/u-S4w4eclf0
Plz suscribe , like and donot forget to follow me in this blog...

2 comments:

Addition of two Binary numbers using C

Addition of two Binary numbers using C  The operation of adding two binary numbers is one of the fundamental tasks performed by a digita...