Mathematics Numerical Analysis Secant Method | Root Finding Method | Algorithm and...

Secant Method | Root Finding Method | Algorithm and Cpp Implementation


Like many other Root finding methods, such as Bisection, or Newton-Raphson method, Secant method is also worth mentioning. Because Secant Method is widely used to find root of nonlinear equation. In Bisection Method we have seen how we can find root of an equation within a closed interval. In this method we will find root of an equation $f(x)=0$ within an open interval using the formula we have shown below:

$$x_{i+1}= x_{i}-\frac{ f(x_{i})*(x_{i}-x_{i-1})}{f(x_{i})-f(x_{i-1})}$$

This formula is derived from Newton-Raphson Method: $x_{i+1}=x_{i}-\frac{f(x_{i})}{f'(x_{i})}$, where $f'(x_{i})$ represents the change in $f(x_{i})-f(x_{i-1})$ with respect to $x_{i}-x_{i-1}$. However for above equation, $x_{i}$ and $x_{i-1}$ are two initial guesses to find $x_{i+1}.$ This is also an iterative process, because increasing the value of $i$ we can get better and better value for $x_{i+1},$ which is the estimated root of the $eq^{n}$.

Secant Method | Root Finding Method | Algorithm and Cpp Implementation

Secant Method Algorithm

Step1: Chose the initial guess $i=1$
followed by the Second Step which is guessing $x_{i}, $ and $x_{i-1}$
In the Third Step we should calculate $f(x_{i+1})$ using the above formula.
Followed by Step 4, which is repeating the step 2 and 3 to find the absolute relative approximate error, $|Ɛ_{a}|$ between present approximation, $x_{i+1}$ and previous approximation, $x_{i}$. So,


Now we need to check if $|Ɛ_{a}|≤ Ɛ_{s},$ (where $ Ɛ_{s} $ is the pre-specified tolerance), then we will stop, else we will repeat step-2 and step-3 incrementing the value of i till the condition is true. If we don’t want to check pre specified tolerance in percentage then we can simply set $|Ɛ_{a}|=\left|{x_{i+1}-x_{i}}\right|$, and then check if $|Ɛ_{a}|≤ Ɛ_{s}$.

Example of Solving an equation $x^{3}=20$ using the Secant Method:

given the initial guesses are: $x_{0}=4.0$ and $x_{1}=5.5$. So we have to solve or find the root of the equation after 2 iteration. Which means in this example we need to find two approximate roots of the equation.

$Sl^{n}: $ According to $f(x)=0$ form the given equation is: $f(x) = x^{3}-20=0$. Now we will find the root(s) of the equation using the Secant Method, which is: $x_{i+1}= x_{i}-\frac{ f(x_{i})*(x_{i}-x_{i-1})}{f(x_{i})-f(x_{i-1})}.$

Now let $i=1,$ so from the above equation substituting the value of $i$ we get,

$$x_{1+1}= x_{1}-\frac{ f(x_{1})*(x_{1}-x_{1-1})}{f(x_{1})-f(x_{1-1})}$$

$$\text{Or, }x_{2}= x_{1}-\frac{ f(x_{1})*(x_{1}-x_{0})}{f(x_{1})-f(x_{0})}$$

Here, $f(x_{1})=f(5.5)=(5.5)^{3}-20=146.4,$ and $f(x_{0})=f(4.0)=(4.0)^{3}-20=44$. So,


$$\text{Or, }x_{2}=3.35$$

Which is a root of the $eq^{n}, x^{3}-20=0$. So the absolute relative approximation, $|Ɛ_{a}|=\left|\frac{x_{2}-x_{1}}{x_{2}}\right|*100=63.92\text{%}$. As the question asked to find two approximate roots of the equation, therefore we’ll approach to find the second approximation by incrementing $i$ by 1. So for $i=2,$

$$ x_{3}= x_{2}-\frac{ f(x_{2})*(x_{2}-x_{1})}{f(x_{2})-f(x_{1})}$$

$$\text{Or, } x_{3}= 3.35-\frac{ 17.76*(3.35-5.5)}{17.76-146.4}$$

$$\text{Or, } x_{3}=3.053$$

Which is the second approximation of the given equation. So the absolute relative approximation for $x_{3}=3.053$ is $|Ɛ_{a}|=\left|\frac{x_{3}-x_{2}}{x_{3}}\right|*100=9.6\text{%}$. Which indicates the value of $x_{3}$ is much closer to exact root of the equation, and the approximate error in the value is 9.6%. Now we’ll see the C++ Implementation of The Secant Method.

C++ Implementation of Root finding Method of Nonlinear Equation

For the simplicity of the program let consider a=$x_{i-1},$ b=$x_{i}$, and c=$x_{i+1}$. As we approach through iteration until we met our condition, |c-b| >e, we will assign a=b, b=c, and $c=b-\frac{f(b)*(b-a)}{f(b)-f(a)},$ which indicates we incrementing $i$ by 1 until we met our condition. here e is the pre-specified tolerance. Now let implement the method using C++ code:

 1 #include<iostream>
 2  #include<iomanip>
 3  #include<cmath>
 4  using namespace std;
 5  double givenEqn(double x);
 6  double givenEqn(double x){
 7      double a = x*x*x - 20;
 8      return a;
 9  }
10  int main(){
11      cout.precision(3);
12      cout.setf(ios::fixed);
13      double a, b, c, e;
14      int stepCount = 1;
15      cout<<"Enter the initial guess\na= ";
16      cin>>b;
17      cout<<"Enter second guess\nb= ";
18      cin>>c;
19      cout<<"Enter the degree of accuracy\ne: ";
20      cin>>e;
21      cout<<"Steps"<<setw(15)<<"a"<<setw(15)<<"b"<<setw(15)<<"c"<<setw(16)<<"f(c)"<<setw(16)<<"|c-b|>e"<<endl;
22      cout<< "------------------------------------------------------------------------------------\n"<<endl;
23      do{
24          a=b;
25          b=c;
26          c=b-((b-a)/(givenEqn(b)-(givenEqn(a)))*givenEqn(b));//Secant Method
27          cout<<stepCount<<setw(21)<<a<<setw(15)<<b<<setw(15)<<c<<setw(15)<<givenEqn(c)<<setw(14)<<fabs(c-b)<<endl;
28          if(givenEqn(c)==0){
29              cout<<"Root of the equation: "<<c<<endl;
30              return 0;
31          }
32          stepCount++;
33      }while(abs(c-b)>e);
34      cout<<"\nThe Root of the Equation: "<<c<<endl;
35      return 0;
36  }
Secant Method | Root Finding Method | Algorithm And Cpp Implementation

Download Source Code

Latest Articles

Property Decorator | Getters Setters and Deleters in Python

In this article, we will talk about the Property Decorator in Python. It enables the class functionality...

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap comparing to other languages. It stores data as a key-value...

Hash Table | Indexing | Hashing Algorithm | Python Implementation

This article will talk about a high-level view of the Hash Table. As a programmer, this technique...

Eigenvector Eigenvalue | Linear Algebra Fundamentals

Eigenvector ($bar{v}$) in linear algebra is a non-zero vector (matrix) that doesn't change its direction during linear...

Pivot Table | Microsoft Excel | Create Data Insight Easily

Pivot table in microsoft Excel is an useful function that gives us a way to create insight...

Macro Function in Microsoft Excel | Automate Repetitive Task

This article we will talk about the Macro. It is a function in microsoft excel which basically...

Must read

Dictionaries | HashMap in Python | Working with Key-Values

Dictionaries in Python is similar to Hashmap...

You might also likeRELATED
Recommended to you