Open Methods: Secant Method
The Method
The secant method is an alternative to the Newton-Raphson method by replacing the derivative  with its finite-difference approximation. The secant method thus does not require the use of derivatives especially when
 with its finite-difference approximation. The secant method thus does not require the use of derivatives especially when  is not explicitly defined. In certain situations, the secant method is preferable over the Newton-Raphson method even though its rate of convergence is slightly less than that of the Newton-Raphson method. Consider the problem of finding the root of the function
 is not explicitly defined. In certain situations, the secant method is preferable over the Newton-Raphson method even though its rate of convergence is slightly less than that of the Newton-Raphson method. Consider the problem of finding the root of the function  . Starting with the Newton-Raphson equation and utilizing the following approximation for the derivative
. Starting with the Newton-Raphson equation and utilizing the following approximation for the derivative  :
:
      ![Rendered by QuickLaTeX.com \[ f'(x_i)=\frac{f(x_i)-f(x_{i-1})}{x_i-x_{i-1}} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-d0d703e34c25783035310166108ee027_l3.png)
the estimate for iteration  can be computed as:
 can be computed as:
      ![Rendered by QuickLaTeX.com \[ x_{i+1}=x_i-f(x_i)\frac{x_i-x_{i-1}}{f(x_i)-f(x_{i-1})} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-c6d311072018853c2d7f34845f06989f_l3.png)
Obviously, the secant method requires two initial guesses  and
 and  .
.
Example
As an example, let’s consider the function  . Setting the maximum number of iterations
. Setting the maximum number of iterations  ,
,  ,
,  , and
, and  , the following is the Microsoft Excel table produced:
, the following is the Microsoft Excel table produced:

The Mathematica code below can be used to program the secant method with the following output:

f[x_] := Sin[5 x] + Cos[2 x];
xtable = {0.5, 0.4};
er = {1,1};
es = 0.0005;
MaxIter = 100;
i = 2;
While[And[i <= MaxIter, Abs[er[[i]]] > es], xnew = xtable[[i]] - (f[xtable[[i]]]) (xtable[[i]] - xtable[[i - 1]])/(f[xtable[[i]]] -f[xtable[[i - 1]]]); xtable = Append[xtable, xnew];  ernew = (xnew - xtable[[i]])/xnew; er = Append[er, ernew]; i++];
T = Length[xtable];
SolutionTable = Table[{i - 1, xtable[[i]], er[[i]]}, {i, 1, T}];
SolutionTable1 = {"Iteration", "x", "er"};
T = Prepend[SolutionTable, SolutionTable1];
T // MatrixForm
import numpy as np import pandas as pd def f(x): return np.sin(5*x) + np.cos(2*x) xtable = [0.5, 0.4] er = [1,1] es = 0.0005 MaxIter = 100 i = 1 while i <= MaxIter and abs(er[i]) > es: xnew = xtable[i] - (f(xtable[i]))*(xtable[i] - xtable[i - 1])/(f(xtable[i]) -f(xtable[i - 1])) xtable.append(xnew) ernew = (xnew - xtable[i])/xnew er.append(ernew) i+=1 SolutionTable = [[i, xtable[i], er[i]] for i in range(len(xtable))] pd.DataFrame(SolutionTable, columns=["Iteration", "x", "er"])
The following code runs the Secant method to find the root of a function  with two initial guesses
 with two initial guesses  and
 and  . The value of the estimate and approximate relative error at each iteration is displayed in the command window. Additionally, two plots are produced to visualize how the iterations and the errors progress. The program waits for a keypress between each iteration to allow you to visualize the iterations in the figure. Call the function with secant(@(x) f(x), x0, x1). For example try secant(@(x) sin(5.*x)+cos(2.*x),0.5,0.4)
. The value of the estimate and approximate relative error at each iteration is displayed in the command window. Additionally, two plots are produced to visualize how the iterations and the errors progress. The program waits for a keypress between each iteration to allow you to visualize the iterations in the figure. Call the function with secant(@(x) f(x), x0, x1). For example try secant(@(x) sin(5.*x)+cos(2.*x),0.5,0.4)
Convergence Analysis of the Secant Method
The  estimate in the secant method is obtained as follows:
 estimate in the secant method is obtained as follows:
      ![Rendered by QuickLaTeX.com \[ x_{i+1}=x_i-f(x_i)\frac{x_i-x_{i-1}}{f(x_i)-f(x_{i-1})} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-b84b798efc0175333f5b2b1960673678_l3.png)
Multiplying both sides by -1 and adding the true value of the root  where
 where  for both sides yields:
 for both sides yields:
      ![Rendered by QuickLaTeX.com \[ x_t-x_{i+1}=x_t-x_i+f(x_i)\frac{x_i-x_{i-1}}{f(x_i)-f(x_{i-1})} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-1993d74da950f7d26b10ba068689e492_l3.png)
Using algebraic manipulations:
      ![Rendered by QuickLaTeX.com \[ \begin{split} x_t-x_{i+1}&=\frac{(x_t-x_i)(f(x_i)-f(x_{i-1}))+f(x_i)(x_i-x_{i-1})}{f(x_i)-f(x_{i-1})}\\ &=\frac{f(x_i)(x_t-x_{i-1})-f(x_{i-1})(x_t-x_i)}{f(x_i)-f(x_{i-1})}\\ &=(x_t-x_{i-1})(x_t-x_i)\frac{\frac{f(x_i)}{x_t-x_i}-\frac{f(x_{i-1})}{x_t-x_{i-1}}}{f(x_i)-f(x_{i-1})} \end{split} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-e9e56f81563b88b0e12826549f008699_l3.png)
Using the Mean Value Theorem, the denominator on the right-hand side can be replaced with:
      ![Rendered by QuickLaTeX.com \[ f(x_i)=f(x_{i-1})+f'(\zeta)(x_i-x_{i-1}) \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-9e7e242b38972674edc47293de87ff60_l3.png)
for some  between
 between  and
 and  . Therefore,
. Therefore,
      ![Rendered by QuickLaTeX.com \[ x_t-x_{i+1}=(x_t-x_{i-1})(x_t-x_i)\frac{\frac{f(x_i)}{x_t-x_i}-\frac{f(x_{i-1})}{x_t-x_{i-1}}}{f'(\zeta)(x_i-x_{i-1})} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-292df7d69860ffdb2cd9c9a5aab2dfe8_l3.png)
Using Taylor’s theorem for  and
 and  around
 around  we get:
 we get:
      ![Rendered by QuickLaTeX.com \[\begin{split} \frac{f(x_i)}{x_t-x_i}=f'(x_t)+\frac{f''(\xi_1)}{2}{(x_t-x_i)}\\ \frac{f(x_i)}{x_t-x_{i-1}}=f'(x_t)+\frac{f''(\xi_2)}{2}{(x_t-x_{i-1})} \end{split} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-6775b5eb8381df36f43839b13b044c66_l3.png)
for some  between
 between  and
 and  and some
 and some  between
 between  and
 and  . Using the above expressions we can reach the equation:
. Using the above expressions we can reach the equation:
      ![Rendered by QuickLaTeX.com \[ x_t-x_{i+1}=(x_t-x_{i-1})(x_t-x_i)\frac{\frac{f''(\xi_1)}{2}{(x_t-x_i)}-\frac{f''(\xi_2)}{2}{(x_t-x_{i-1})}}{f'(\zeta)(x_i-x_{i-1})} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-2d46c4b9831529210eada05bb5d0b29b_l3.png)
 and
 and  can be assumed to be identical and equal to
 can be assumed to be identical and equal to  , therefore:
, therefore:
      ![Rendered by QuickLaTeX.com \[\begin{split} x_t-x_{i+1}&=(x_t-x_{i-1})(x_t-x_i)\frac{\frac{f''(\xi)}{2}{(x_t-x_i-x_t+x_{i-1})}}{f'(\zeta)(x_i-x_{i-1})}\\ &=(x_t-x_{i-1})(x_t-x_i)\frac{-f''(\xi)}{2f'(\zeta)} \end{split} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-83b9edce2f74284e04719ee652fa6041_l3.png)
 (1)    
Comparing the convergence equation of the Newton Raphson method with 1 shows that the convergence in the secant method is not quite quadratic. To find the order of convergence, we need to solve the following equation for a positive  and
 and  :
:
      ![Rendered by QuickLaTeX.com \[ |E_{i+1}|=C|E_{i}|^p \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-4a42f833c4be4a94973326bdcd6fb923_l3.png)
Substituting into equation 1 yields:
      ![Rendered by QuickLaTeX.com \[ C|E_{i}|^p=|E_{i}||E_{i-1}|\bigg{|}\frac{-f''(\xi)}{2f'(\zeta)}\bigg{|}\Rightarrow E_{i}=\left|\frac{-f''(\xi)}{2Cf'(\zeta)}\right|^{\frac{1}{p-1}}|E_{i-1}|^{\frac{1}{p-1}} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-bac4bb5f3a5ab59f5d920c39f59f56b9_l3.png)
But we also have:
      ![Rendered by QuickLaTeX.com \[ |E_{i}|=C|E_{i-1}|^p \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-c82e52c13c9b6c3fea3b4839e5706adf_l3.png)
Therefore:  . This equation is called  the golden ratio and has the positive solution for
. This equation is called  the golden ratio and has the positive solution for  :
:
      ![Rendered by QuickLaTeX.com \[ p=\frac{\sqrt{5}+1}{2}\approx 1.618034 \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-dd80d46a80eadfd8ca38abb95fdfeff1_l3.png)
while
      ![Rendered by QuickLaTeX.com \[ C=\left|\frac{-f''(\xi)}{2f'(\zeta)}\right|^{p-1}=\left|\frac{-f''(\xi)}{2f'(\zeta)}\right|^{0.618034} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-2d1471e9fd2708e039fd2e0cab406d1f_l3.png)
implying that the error convergence is not quadratic but rather:
      ![Rendered by QuickLaTeX.com \[ E_{i+1}\propto E_i^{1.618034} \]](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-e1c27c610c7ec0f1afefb661bbc7642d_l3.png)
The following tool visualizes how the secant method converges to the true solution using two initial guesses. Using  ,
,  ,
,  , and solving for the root of
, and solving for the root of ![Rendered by QuickLaTeX.com f(x)=\sin[5x]+\cos[2x]=0](https://engcourses-uofa.ca/wp-content/ql-cache/quicklatex.com-ce12fca1e8554b4c99099a0b20f0d1f7_l3.png) yields
 yields  .
.
The following Mathematica Code was utilized to produce the above tool:
View Mathematica CodeManipulate[
 f[x_] := x^3 - x + 3;
 f[x_] := Sin[5 x] + Cos[2 x];
 xtable = {-0.5, -0.6};
 xtable = {0.35, 0.4};
 er = {1, 1};
 es = 0.0005;
 MaxIter = 100;
 i = 2;
 While[And[i <= MaxIter, Abs[er[[i]]] > es], xnew = xtable[[i]] - (f[xtable[[i]]]) (xtable[[i]] - xtable[[i-1]])/(f[xtable[[i]]] -f[xtable[[i - 1]]]); xtable = Append[xtable, xnew];  ernew = (xnew - xtable[[i]])/xnew; er = Append[er, ernew]; i++];
 T = Length[xtable];
 SolutionTable = Table[{i - 1, xtable[[i]], er[[i]]}, {i, 1, T}];
 SolutionTable1 = {"Iteration", "x", "er"};
 T = Prepend[SolutionTable, SolutionTable1];
 T // MatrixForm;
 If[n > 3,
  (LineTable1 = {{T[[n - 2, 2]], 0}, {T[[n - 2, 2]], f[T[[n - 2, 2]]]}, {T[[n - 1, 2]], f[T[[n - 1, 2]]]}, {T[[n, 2]], 0}};
   LineTable2 = {{T[[n - 1, 2]], 0}, {T[[n - 1, 2]], f[T[[n - 1, 2]]]}}), LineTable1 = {{}}; LineTable2 = {{}}];
 Grid[{{Plot[f[x], {x, 0, 1}, PlotLegends -> {"f(x)"}, ImageSize -> Medium, Epilog -> {Dashed, Line[LineTable1], Line[LineTable2]}]}, {Row[{"Iteration=", n - 2, "    x_n=", T[[n, 2]], "   f(x_n)=", f[T[[n, 2]]]}]}}], {n, 2, 8, 1}]
import numpy as np
import sympy as sp
import pandas as pd
import matplotlib.pyplot as plt
from ipywidgets.widgets import interact
def f(x): return sp.sin(5*x) + sp.cos(2*x)
xtable = [-0.5, -0.6]
xtable = [0.35, 0.4]
er = [1, 1]
es = 0.0005
MaxIter = 100
i = 1
while i <= MaxIter and abs(er[i]) > es:
  xnew = xtable[i] - (f(xtable[i]))*(xtable[i] - xtable[i-1])/(f(xtable[i]) -f(xtable[i - 1]))
  xtable.append(xnew)
  ernew = (xnew - xtable[i])/xnew
  er.append(ernew)
  i+=1
SolutionTable = [[i, xtable[i], er[i]] for i in range(len(xtable))]
display(pd.DataFrame(SolutionTable, columns=["Iteration", "x", "er"]))
@interact(n=(2,8,1))
def update(n=2):
  LineTable1 = [[],[]]
  LineTable2 = [[],[]]
  if n > 3:
    LineTable1[0] = [SolutionTable[n - 4][1], SolutionTable[n - 4][1], 
                     SolutionTable[n - 3][1], SolutionTable[n-2][1]]
    LineTable1[1] = [0, f(SolutionTable[n - 4][1]), f(SolutionTable[n - 3][1]), 0]
    LineTable2[0] = [SolutionTable[n - 3][1], SolutionTable[n - 3][1]]
    LineTable2[1] = [0, f(SolutionTable[n - 3][1])]
  x_val = np.arange(0,1.1,0.03)
  y_val = np.sin(5*x_val) + np.cos(2*x_val)
  plt.plot(x_val,y_val, label="f(x)")
  plt.plot(LineTable1[0],LineTable1[1],'k--')
  plt.plot(LineTable2[0],LineTable2[1],'k--')
  plt.xlim(0,1,1); plt.ylim(-2,2)
  plt.legend(); plt.grid(); plt.show()
  print("Iteration =",n-2," x_n =",round(SolutionTable[n-2][1],5)," f(x_n) =",round(f(SolutionTable[n-2][1]),5))

