Secant

 #include<iostream>

#include<math.h>

#include<iomanip>

#define err 0.0001


using namespace std;


float f(float x)

{

    return //Your equation

}


float secant(float x0,float x1)

{

    return x1-((x1-x0)/(f(x1)-f(x0))*f(x1));

}


int main()

{

    int i=2;

    float x0,x1,x2,x3;

    cout<<"Enter initial approximate roots a: ";

    cin>>x0;

    cout<<"Enter initial approximate roots b: ";

    cin>>x1;

    cout<<setprecision(6)<<"Iteration\tRoot\n";

    x2 = secant(x0,x1);


    while(fabs(x2-x1)>err)

    {

        cout<<"X"<<i<<"\t\t"<<x2<<endl;

        x3 = secant(x1,x2);

        x1=x2;

        x2=x3;

        i++;

    }


    cout<<"\nAfter "<<i-1<<" iteration Root = "<<x2;

    return 0;

}

0 Comments: