Subscribe Us

Responsive Advertisement

Advertisement

1. Bisection Method

 1. Bisection Method using c++



//equation x^3 - x -1

#include<bits/stdc++.h>

using namespace std;

double val(double x)

{

    return x*x*x-x-1;

}

void bisection(double a,double b)

{

    if(val(a)*val(b)>0)

    {

        cout<<"Wrong input\n";

        return ;

    }

    double root;

    root=a;

    while(abs(b-a)>=.0000000000001)

    {

        root=(a+b)/2;

        if(val(root)==0.0)break;

        else if(val(a)*val(root)<0)b=root;

        else a=root;

    }

    cout<<"Root is : "<<root<<endl;

}

int main()

{

    cout<<"Enter two initial value : ";

    double x,y;

    cin>>x>>y;

    bisection(x,y);



    return 0;

}


Post a Comment

0 Comments