Secant Method to find the root of an equation in C++

In this tutorial, we will learn how to find out the root of an equation using the secant method in C++. Before proceeding further let’s first understand what is the secant method.

Secant method: it is an algorithm that is used for finding the root of an equation. It starts its iteration process with two initial approximations. In each stage, it tries to approximate the root of an equation. It stops when the error becomes less than the desired accuracy.

Algorithm:

  1. Initialize x1, x2, e (desired accuracy) and iteration number.
  2. Calculate f(x1), f(x2).
  3. Increment the iteration number.
  4. Calculate the intermediate value xm1 using formula => (x1 * f(x2) – x2 * f(x1)) / (f(x2) – f(x1)).
  5. Update intervals value.
  6. Check if xm1 is the root of an equation. If yes, then break the loop.
  7. Otherwise, calculate xm2 using formula => (x1 * f(x2) – x2 * f(x1)) / (f(x2) – f(x1)).
  8. Repeat steps 3-7 until error becomes less than the desired accuracy.
  9. You will get the root of an equation in xm1.

Program to find the root of an equation using the Secant Method in C++

#include<iostream>
/* 'cmath' header file is included for pow and fabs functions */
#include<cmath>
using namespace std; 

double secant_method(double ,double ,double ,int* );
double f(double );

int main() 
{ 
  double x1,x2,e;
  int iteration_no=0;
  
    cout<<"\nEnter the first initial approximation: ";
    cin>>x1;
    
    cout<<"\nEnter the second initial approximation: ";
    cin>>x2;
    
    /* it should as low as possible, like 0.001 or 0.0001 */
    cout<<"\nEnter the degree of accuracy: ";
    cin>>e;
    
    double res=secant_method(x1, x2, e, &iteration_no);
    cout<<"\nRoot of the given equation is "<<res; 
    cout<<"\nNo. of iterations are "<<iteration_no;
 
    return 0; 
} 
  
double secant_method(double x1, double x2, double e, int* iteration_no) 
{ 
    double xm1,xm2,k,f1,f2;
  
  do
  {
      /* calulating and storing values of f(x1) and f(x2) into f1 and f2 resp */
      f1=f(x1);
      f2=f(x2);
      
        /* updating iteration number */
            *iteration_no+=1; 
            
        /* calculating the intermediate value */
            xm1 = (x1 * f2 - x2 * f1) / (f2 - f1); 
  
            /* checking wheather xm1 is root of the equation or not */
            k = f(x1) * f(xm1); 
  
            /* updating the value of intervals */ 
            x1 = x2; 
            x2 = xm1; 
  
            /* if xm1 is the root of equation then break the loop */
            if (k == 0) 
                break; 
                
            xm2 = (x1 * f2 - x2 * f1) / (f2 - f1);
            
  }while( fabs(xm2 - xm1) >= e); /* check if the error is greater than the desired accuracy. fabs function is used to return non-negative float value of its argument */

 return xm1;

}

double f(double no) 
{ 
    /* write the equation whose roots are to be determined. Here we are using equation as x^3 + x - 1 */
    double res = pow(no, 3) + no - 1; 
    return res; 
}

Input/Output:

Enter the first initial approximation: 0

Enter the second initial approximation: 1

Enter the degree of accuracy: 0.001

Root of the given equation is 0.682326
No. of iterations are 5

Time Complexity

It has linear time complexity. i.e. O(1).

You may also learn:




  1. Play Video in C++
  2. Add text watermark to an image in C++


Leave a Reply

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