26.08.2019
Posted by 

Program to demonstrate Secant method Program to demonstrate the Aitken Steffenson iteration method Explanation File of Program above (Steffen) NEW; Program to demonstrate Brent's method Explanation File of Program above (Zbrent) NEW; Define six real functions for Pegasus method Module to find a real root of a real function f(x) by Pegasus.

  1. Secant Method Matlab Example
  2. Fortran Program For Secant Method Worksheet

Secant method is considered to be the most effective approach to find the root of a non-linear function. It is a generalized from the Newton-Raphson method and does not require obtaining the derivatives of the function. So, this method is generally used as an alternative to Newton Raphson method.

Secant method falls under open bracket type. The programming effort may be a tedious to some extent, but the secant method algorithm and flowchart is easy to understand and use for coding in any high level programming language.

This method uses two initial guesses and finds the root of a function through interpolation approach. Here, at each successive iteration, two of the most recent guesses are used. That means, two most recent fresh values are used to find out the next approximation.

Features of Secant Method:

  • No. of initial guesses – 2
  • Type – open bracket
  • Rate of convergence – faster
  • Convergence – super linear
  • Accuracy – good
  • Approach – interpolation
  • Programming effort – tedious

Secant Method Algorithm:

  1. Start
  2. Get values of x0, x1 and e
    *Here x0 and x1 are the two initial guesses
    e is the stopping criteria, absolute error or the desired degree of accuracy*
  3. Compute f(x0) and f(x1)
  4. Compute x2 = [x0*f(x1) – x1*f(x0)] / [f(x1) – f(x0)]
  5. Test for accuracy of x2
    If [ (x2 – x1)/x2 ] > e, *Here [ ] is used as modulus sign*
    then assign x0 = x1 and x1 = x2
    goto step 4
    Else,
    goto step 6
  6. Display the required root as x2.
  7. Stop

Secant Method Flowchart:

Also see,
Secant Method C Program
Secant Method MATLAB Program

Secant method is an improvement over the Regula-Falsi method, as successive approximations are done using a secant line passing through the points during each iteration. Following the secant method algorithm and flowchart given above, it is not compulsory that the approximated interval should contain the root.

Secant method is faster than other numerical methods, except the Newton Raphson method. Its rate of convergence is 1.62, which is quite fast and high. However, convergence is not always guaranteed in this method. But, overall, this method proves to be the most economical one to find the root of a function.

< Numerical Analysis
  • 1The Secant Method
    • 1.5Exercises

The secant method is an algorithm used to approximate the roots of a given function f. The method is based on approximating f using secant lines.

The Algorithm[edit]

The secant method algorithm requires the selection of two initial approximations x0 and x1, which may or may not bracket the desired root, but which are chosen reasonably close to the exact root.

Secant Method Algorithm
Given f(x)=0:

Let x0 and x1 be initial approximations.

Then
xn=xn1f(xn1)xn1xn2f(xn1)f(xn2){displaystyle x_{n}=x_{n-1}-f(x_{n-1}){frac {x_{n-1}-x_{n-2}}{f(x_{n-1})-f(x_{n-2})}}}

where xn is a better approximation of the exact root, assuming convergence.

Repeat iterative step until either

  1. The total number of iterations N is met
  2. A sufficient degree of accuracy, ϵ{displaystyle epsilon }, is achieved.

Order of Convergence[edit]

We would like to be able to find the order of convergence, p, for the secant method. Hence, we want to find some p so that xn+1xCpxnx{displaystyle leftvert {x_{n+1}-x}rightvert approx C^{p}leftvert {x_{n}-x}rightvert } where C is a constant.

Given a function f, let x be such that f(x)=0 and let xn-1 and xn be approximations to x. Assume x is a simple root and f is twice continuously differentiable (from the assumptions leading to convergence noted on Wikipedia). Let the error at the nth step be denoted by en: en=xn-x. Then we have:

en+1=xn+1x=xnf(xn)xnxn1f(xn)f(xn1)x=(xn1x)f(xn)(xnx)f(xn1)f(xn)f(xn1)=en1f(xn)enf(xn1)f(xn)f(xn1)=enen1(f(xn)enf(xn1)en1f(xn)f(xn1)){displaystyle {begin{aligned}e_{n+1}=x_{n+1}-x&=x_{n}-f(x_{n}){frac {x_{n}-x_{n-1}}{f(x_{n})-f(x_{n-1})}}-x&={frac {(x_{n-1}-x)f(x_{n})-(x_{n}-x)f(x_{n-1})}{f(x_{n})-f(x_{n-1})}}&={frac {e_{n-1}f(x_{n})-e_{n}f(x_{n-1})}{f(x_{n})-f(x_{n-1})}}&=e_{n}e_{n-1}{Bigg (}{frac {{frac {f(x_{n})}{e_{n}}}-{frac {f(x_{n-1})}{e_{n-1}}}}{f(x_{n})-f(x_{n-1})}}{Bigg )}end{aligned}}}.

Since f(x)=0 and recalling that en=xn-x, we can rewrite the last line above as:String Module Error: function rep expects a number as second parameter, received '

'
en+1=enen1(f(xn)f(x)xnxf(xn1)f(x)xn1xf(xn)f(xn1)).{displaystyle e_{n+1}=e_{n}e_{n-1}{Bigg (}{frac {{frac {f(x_{n})-f(x)}{x_{n}-x}}-{frac {f(x_{n-1})-f(x)}{x_{n-1}-x}}}{f(x_{n})-f(x_{n-1})}}{Bigg )}.}

(1)

String Module Error: function rep expects a number as second parameter, received '

'

Next, let's just consider the numerator in (1).

Secant Method Matlab Example

Let F(ω)=f(ω)f(x)ωx{displaystyle F(omega )={frac {f(omega )-f(x)}{omega -x}}} where ω=...xn1,xn,xn+1,...{displaystyle omega =...x_{n-1},x_{n},x_{n+1},...}. ThusString Module Error: function rep expects a number as second parameter, received '

'
F(ω)=f(ω)(ωx)+f(x)f(ω)(ωx)2.{displaystyle F'(omega )={frac {f'(omega )(omega -x)+f(x)-f(omega )}{(omega -x)^{2}}}.}

(2)

Fortran Program For Secant Method Worksheet

String Module Error: function rep expects a number as second parameter, received '

'According to the Mean Value Theorem, on [xn-1,xn], there exists some ζn{displaystyle zeta _{n}} between xn-1 and xn such that F(ζn)=F(xn)F(xn1)xnxn1{displaystyle F'(zeta _{n})={frac {F(x_{n})-F(x_{n-1})}{x_{n}-x_{n-1}}}}String Module Error: function rep expects a number as second parameter, received '

'
F(xn)F(xn1)=F(ζn)(xnxn1).{displaystyle Longleftrightarrow F(x_{n})-F(x_{n-1})=F'(zeta _{n})(x_{n}-x_{n-1}).}

(3)

String Module Error: function rep expects a number as second parameter, received '

'Now using a Taylor expansion of f(x){displaystyle f(x)} around ω{displaystyle omega }, we haveString Module Error: function rep expects a number as second parameter, received '

'
f(x)=f(ω)+(xω)f(ω)+(xω)22f(νn)f(x)f(ω)(xω)f(ω)=(xω)22f(νn).{displaystyle {begin{aligned}f(x)&=f(omega )+(x-omega )f'(omega )+{frac {(x-omega )^{2}}{2}}f'(nu _{n})Rightarrow f(x)-f(omega )-(x-omega )f'(omega )&=-{frac {(x-omega )^{2}}{2}}f'(nu _{n}).end{aligned}}}

(4)

String Module Error: function rep expects a number as second parameter, received '

'

Next, we can combine equations (2), (3), and (4) to show that F(xn)F(xn1)=(xnxn1)2f(νn){displaystyle F(x_{n})-F(x_{n-1})={frac {(x_{n}-x_{n-1})}{2}}f'(nu _{n})}.

Returning to (1

), we now have:String Module Error: function rep expects a number as second parameter, received '

Fortran Program For Secant Method'
en+1=enen1(F(xn)F(xn1)f(xn)f(xn1))=enen1(xnxn1)2[f(xn)f(xn1)]f(νn).{displaystyle e_{n+1}=e_{n}e_{n-1}{Bigg (}{frac {F(x_{n})-F(x_{n-1})}{f(x_{n})-f(x_{n-1})}}{Bigg )}={frac {e_{n}e_{n-1}(x_{n}-x_{n-1})}{2[f(x_{n})-f(x_{n-1})]}}f'(nu _{n}).}

(5)

String Module Error: function rep expects a number as second parameter, received '

'

Again applying the Mean Value Theorem, there exists some ξn{displaystyle xi _{n}} in [xn-1,xn] such that f(ξn)=f(xn)f(xn1)xnxn1{displaystyle f'(xi _{n})={frac {f(x_{n})-f(x_{n-1})}{x_{n}-x_{n-1}}}}. Then (5) becomes:

en+1=enen1f(νn)2f(ξn)enen1f(x)2f(x).{displaystyle e_{n+1}=e_{n}e_{n-1}{frac {f'(nu _{n})}{2f'(xi _{n})}}approx e_{n}e_{n-1}{frac {f'(x)}{2f'(x)}}.}

Next, recall that we have convergence of order p when limnxn+1xxnxp=limnen+1enp=μ{displaystyle lim _{nto infty }{frac {leftvert {x_{n+1}-x}rightvert }{leftvert {x_{n}-x}rightvert ^{p}}}=lim _{nto infty }{frac {leftvert {e_{n+1}}rightvert }{leftvert {e_{n}}rightvert ^{p}}}=mu } for some constant μ>0{displaystyle mu >0}. Our goal is to figure out what p is for the secant method.

Let Sn=en+1enp{displaystyle S_{n}={frac {leftvert {e_{n+1}}rightvert }{leftvert {e_{n}^{p}}rightvert }}}
en+1=Snenp=Sn(Sn1en1p)p=SnSn1pen1p2{displaystyle Leftrightarrow leftvert {e_{n+1}}rightvert =S_{n}leftvert {e_{n}}rightvert ^{p}=S_{n}(S_{n-1}leftvert {e_{n-1}^{p}}rightvert )^{p}=S_{n}S_{n-1}^{p}leftvert {e_{n-1}}rightvert ^{p^{2}}}.

Then we have:en+1enen1=SnSn1pen1p2Sn1en1pen1=SnSn1p1en1p2p1{displaystyle {frac {leftvert {e_{n+1}}rightvert }{leftvert {e_{n}}rightvert leftvert {e_{n-1}}rightvert }}={frac {S_{n}S_{n-1}^{p}leftvert {e_{n-1}}rightvert ^{p^{2}}}{S_{n-1}leftvert {e_{n-1}}rightvert ^{p}leftvert {e_{n-1}}rightvert }}=S_{n}S_{n-1}^{p-1}leftvert {e_{n-1}}rightvert ^{p^{2}-p-1}}.

We want limn(SnSn1p1en1p2p1)=μ{displaystyle lim _{nto infty }{Big (}S_{n}S_{n-1}^{p-1}leftvert {e_{n-1}}rightvert ^{p^{2}-p-1}{Big )}=mu }, again where μ{displaystyle mu } is some constant. Since Sn{displaystyle S_{n}} and Sn1p1{displaystyle S_{n-1}^{p-1}} are constants and limnen=0{displaystyle lim _{nto infty }e_{n}=0} (assuming convergence) we must have p2p1=0{displaystyle p^{2}-p-1=0}. Thus p=1+521.618{displaystyle p={frac {1+{sqrt {5}}}{2}}approx 1.618}.[1]

A Numerical Example[edit]

The function f(x)=sinx+xex{displaystyle f(x)=sin x+xe^{x}} has a root between -3 and -4. Let's approximate this root accurate to four decimal places.

Let x0 = -3 and x1 = -4.

Next, using our recurrence formula where

f(x0)=f(3)=sin(3)3e30.2905{displaystyle f(x_{0})=f(-3)=sin(-3)-3e^{-3}approx -0.2905}


and

f(x1)=f(4)=sin(4)4e40.6835{displaystyle f(x_{1})=f(-4)=sin(-4)-4e^{-4}approx 0.6835},


we have:

x2=x1f(x1)x1x0f(x1)f(x0)=4.6835(4+3.6835+.2905)3.2983.{displaystyle x_{2}=x_{1}-f(x_{1}){frac {x_{1}-x_{0}}{f(x_{1})-f(x_{0})}}=-4-.6835({frac {-4+3}{.6835+.2905}})approx -3.2983.}

In the next iteration, we use f(x1) = .6835 and f(x2) = .0342 and see that

x3=x2f(x2)x2x1f(x2)f(x1)=3.2983.0342(3.2983+4.0342.6835)3.2613.{displaystyle x_{3}=x_{2}-f(x_{2}){frac {x_{2}-x_{1}}{f(x_{2})-f(x_{1})}}=-3.2983-.0342({frac {-3.2983+4}{.0342-.6835}})approx -3.2613.}

Similarly, we can compute x4 and x5. These calculations have been organized in the table below:

i{displaystyle i}012345
xi{displaystyle x_{i}}-3-4-3.2983-3.2613-3.2665-3.2665

Hence the iterative method converges to -3.2665 after 4 iterations.

Pseudo Code[edit]

Below is pseudo code that will perform iterations of the secant method on a given function f.

Exercises[edit]

Exercise 1[edit]

Find an approximation to 5{displaystyle {sqrt {5}}} correct to four decimal places using the secant method on f(x)=x25{displaystyle f(x)=x^{2}-5}.

We know 5=2.2361{displaystyle {sqrt {5}}=2.2361}.
Let x0 = 2 and x1 = 3. Then f(x0) = f(2) = -1 and f(x1) = f(3) = 4.
In our first iteration, we have:

x2=x1f(x1)x1x0f(x1)f(x0)=34(324+1)=2.2{displaystyle x_{2}=x_{1}-f(x_{1}){frac {x_{1}-x_{0}}{f(x_{1})-f(x_{0})}}=3-4({frac {3-2}{4+1}})=2.2}

In the second iteration, f(x1) = 4, f(x2) = -0.16 and we thus have

x3=x2f(x2)x2x1f(x2)f(x1)=2.2+.16(2.23.164)2.2333.{displaystyle x_{3}=x_{2}-f(x_{2}){frac {x_{2}-x_{1}}{f(x_{2})-f(x_{1})}}=2.2+.16({frac {2.2-3}{-.16-4}})approx 2.2333.}

Similarly, x3 and x4 can be calculated, and are shown in the table below:

i{displaystyle i}012345
xi{displaystyle x_{i}}232.22.23332.23612.2361

Thus after 4 iterations, the secant method converges to 2.2361, an approximation to 5{displaystyle {sqrt {5}}} correct to four decimal places.

Exercise 2[edit]

Find a root of f(x)=x+ex{displaystyle f(x)=x+e^{x}} by performing five iterations of the secant method beginning with x0 = -1 and x1 = 0.

1st Iteration: x0 = -1, x1 = 0, f(x0) = -0.63212, and f(x1) = 1. Then
x2=x1f(x1)x1x0f(x1)f(x0)=(11+.63212)=.6127{displaystyle x_{2}=x_{1}-f(x_{1}){frac {x_{1}-x_{0}}{f(x_{1})-f(x_{0})}}=({frac {-1}{1+.63212}})=-.6127}

2nd Iteration: x1 = 0, x2 = -.6127, f(x1) = 1, and f(x2) = -.07081. Then
x3=x2f(x2)x2x1f(x2)f(x1)=.6127+.07081(.6127.070811)=.57218{displaystyle x_{3}=x_{2}-f(x_{2}){frac {x_{2}-x_{1}}{f(x_{2})-f(x_{1})}}=-.6127+.07081({frac {-.6127}{-.07081-1}})=-.57218}

3rd Iteration: x2 = -.6127, x3 = -.57218, f(x2) = -.07081, and f(x3) = -.00789. Then
x4=x3f(x3)x3x2f(x3)f(x2)=.57218+.00789(.57218+.6127.00789+.07081)=.5671{displaystyle x_{4}=x_{3}-f(x_{3}){frac {x_{3}-x_{2}}{f(x_{3})-f(x_{2})}}=-.57218+.00789({frac {-.57218+.6127}{-.00789+.07081}})=-.5671}

4th Iteration: x3 = -.57218, x4 = -.5671, f(x3) = -.00789, and f(x4) = 6.7843*10-5. Then
x5=x4f(x4)x4x3f(x4)f(x3)=.56716.7843×105(.5671+.572186.7843×105+.00789)=.56714{displaystyle x_{5}=x_{4}-f(x_{4}){frac {x_{4}-x_{3}}{f(x_{4})-f(x_{3})}}=-.5671-6.7843times 10^{-5}({frac {-.5671+.57218}{6.7843times 10^{-5}+.00789}})=-.56714}

5th Iteration: x4 = -.5671, x5 = -.56714, f(x4) = 6.7843*10-5, and f(x5) = 5.1565*10-6. Then
x6=x5f(x5)x5x4f(x5)f(x4)=.567145.1565×106(.56714+.56715.1565×1066.7843×105)=.56714{displaystyle x_{6}=x_{5}-f(x_{5}){frac {x_{5}-x_{4}}{f(x_{5})-f(x_{4})}}=-.56714-5.1565times 10^{-6}({frac {-.56714+.5671}{5.1565times 10^{-6}-6.7843times 10^{-5}}})=-.56714}

Thus after 5 iterations, the method converges to -.56714 as one of the roots of f(x)=x+ex{displaystyle f(x)=x+e^{x}}.

Quiz[edit]

The following is a quiz covering information presented on the associated secant method page on Wikipedia as well as the current page.


References[edit]

  1. http://www.radford.edu/~thompson/Fall10/434/Chapter4/secant_convergence.pdf
Retrieved from 'https://en.wikiversity.org/w/index.php?title=Numerical_Analysis/The_Secant_Method&oldid=1875288'