N. Achyut Numerical Method False Position Method in C++ Programming

False Position Method in C++ Programming

False Position Method in C++ Programming

False Position Method states

Given a function f(x) on floating number x and two numbers ‘a’ and ‘b’ such that f(a)*f(b) < 0 and f(x) is continuous in [a, b]. Here f(x) represents algebraic or transcendental equation. Find root of function in interval [a, b] (Or find a value of x such that f(x) is 0).

False Position Method in C++ Programming

#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;

#define f(x) 3*x*x-6*x+2

int main()
{
    int i=1;
    float x0,x1,x2,f0,f1,f2,e=1;
    cout<< "enter the initial guess x1 and x2:";
    cin>>x1>>x2;
    do
    {
        f1=f(x1);
        f2=f(x2);
        if(f1*f2>0)
        {
            cout<< "x1 and x2 doesnot bracket the root";
            exit(0);
        }
        else
        {
            x0=(x1-(f1*(x2-x1))/(f2-f1)); //working formula
            f0=f(x0);
            e=fabs((x2-x1)/x2);
            if(f1*f0<0)
            {
                x2=x0;
            }
            else
            {
                x1=x0;
            }
            i++;
        }
    }while(e>=0.0001 && f1!=0 && i!=100);
    cout<< "nthe root of the equation="<<x0;
    return 0;
}

Output of the program :

Enter the initial guess x1 and x2 : 1
2
The root of the equation = 1.57739

You may like some other numerical method Questions :

BISECTION METHOD SOLUTION IN C++ PROGRAMMING
FALSE POSITION METHOD IN C++ PROGRAMMING
SECANT METHOD IN C++ PROGRAMMING
FIXED POINT METHOD / C++ PROGRAMMING
NEWTON-RAPHSON METHOD IN C++ PROGRAMMING

Related Post