Subscribe Us

Responsive Advertisement

Advertisement

5. Secant Method

 

5. Secant Method

by ujjal roy



#include<bits/stdc++.h>

using namespace std;

//equation x^3-x-1

double f(double x)

{

    return x*x*x-x-1;

}

void secant(double x0,double x1)

{

    if(f(x0)*f(x1)>=0)

    {

        cout<<"Wrong input\n";

        return;

    }

    double root;

    root=x0;

    while(abs(x0-x1)>=.0001)

    {

        root=(x0*f(x1)-x1*f(x0))/(f(x1)-f(x0));

        x0=x1;

        x1=root;


    }

    cout<<"root is : "<<fixed<<setprecision(4)<<root<<endl;

}

int main()

{

    cout<<"Enter Number of element ";

    double x0,x1;

    cin>>x0>>x1;

    secant(x0,x1);




    return 0;

}


Post a Comment

0 Comments