C++ Program for Secant Method to find the roots of an Equation

//Secant Method for finding the roots of an equation
using namespace std;
double f(double x);    //declare the function for the given equation
double f(double x)    //define the function here, ie give the equation
    double a=pow(x,3)-x-11.0;    //write the equation whose roots are to be determined
    return a;
int main()
cout.setf(ios::fixed);        //set the precision of the output
double a,b,c,e;
cout<<"Enter the initial guess\na=";
cout<<"b=\n";                //take an intial guess
cout<<"Enter the degree of accuracy\n";
cin>>e;                    //take the desired accuracy
    b=c;                //make b equal to the last calculated value of c
    c=b-(b-a)/(f(b)-f(a))*f(b);    //calculate c
    if (f(c)==0)
        cout<<"\nThe root of the equation is "<=e);            //check if the error is greater than the desired accuracy
cout<<"\nThe root of the equation is "<


Explanation of the code:

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.
[wpedon id="7041" align="center"]

4 thoughts on “C++ Program for Secant Method to find the roots of an Equation

Leave a Reply

Your email address will not be published. Required fields are marked *