The Secant Method
Newton's method needs , which may be unavailable, expensive, or buried in a black-box simulation. The secant method replaces the tangent by the line through the last two iterates. It needs only one new function value per step and still converges superlinearly.
The iteration
The secant line through and crosses zero at
It is Newton's method with replaced by the difference quotient.
import numpy as np
def secant(f, x0, x1, tol=1e-14, maxit=50):
f0, f1 = f(x0), f(x1)
hist = [x0, x1]
for _ in range(maxit):
x2 = x1 - f1 * (x1 - x0) / (f1 - f0)
hist.append(x2)
if abs(x2 - x1) < tol * max(1, abs(x2)):
break
x0, f0, x1, f1 = x1, f1, x2, f(x2)
return x2, hist
f = lambda x: x * np.exp(x) - 2
r, hist = secant(f, 0.0, 1.0)
err = np.abs(np.array(hist) - r)
for k, e in enumerate(err[:-1]):
print(f"k = {k}: error {e:.2e}")k = 0: error 8.53e-01
k = 1: error 1.47e-01
k = 2: error 1.17e-01
k = 3: error 1.31e-02
k = 4: error 1.23e-03
k = 5: error 1.24e-05
k = 6: error 1.17e-08
k = 7: error 1.12e-13
k = 8: error 0.00e+00
Order of convergence
Analysis gives . Trying leads to , so
the golden ratio. Per step this is slower than Newton's , but each secant step costs one function evaluation while a Newton step costs and . If costs as much as , two secant steps (order ) cost the same as one Newton step, so the secant method is often faster in practice.
In double precision the method converges in so few steps that the order is hard to read off the errors. With 300-digit arithmetic from mpmath the pattern is clear:
import mpmath as mp
mp.mp.dps = 300
f = lambda x: x * mp.exp(x) - 2
r = mp.findroot(f, 0.85)
x0, x1 = mp.mpf(0), mp.mpf(1)
errs = [abs(x0 - r), abs(x1 - r)]
for _ in range(11):
x0, x1 = x1, x1 - f(x1) * (x1 - x0) / (f(x1) - f(x0))
errs.append(abs(x1 - r))
L = [mp.log(e) for e in errs]
orders = [(L[k + 1] - L[k]) / (L[k] - L[k - 1]) for k in range(4, 12)]
print("errors:", [mp.nstr(e, 2) for e in errs[4:]])
print("estimated orders:", [mp.nstr(q, 4) for q in orders])errors: ['0.0012', '1.2e-5', '1.2e-8', '1.1e-13', '1.0e-21', '8.7e-35', '6.8e-56', '4.5e-90', '2.4e-145']
estimated orders: ['1.941', '1.516', '1.66', '1.603', '1.624', '1.616', '1.619', '1.618']
Inverse interpolation
Instead of fitting a line through the last points and solving , one can fit as a function of and evaluate it at . With three points this is inverse quadratic interpolation (order about ), used inside Brent's method together with bisection (Rootfinding in Practice).
Weaknesses
- Like Newton, it can diverge from a poor start, and it does not keep a bracket.
- When the division is unstable; implementations stop when the step is tiny.
- Regula falsi (false position) keeps a bracket by always using the two points with opposite signs, but it can stall with one endpoint stuck, giving only linear convergence. Variants such as the Illinois method fix that.
In software
scipy.optimize.newton(f, x0)withoutfprimeuses the secant method;root_scalar(f, x0=..., x1=..., method="secant")too.- Excel's
IRRandRATEuse secant-like iterations from a default guess, which is why they sometimes report#NUM!and accept a better guess as an argument.
Exercises
Exercise 1: One step by hand
Do one secant step for with , .
Answer
.
Solution
, : .
Exercise 2: Efficiency index
The efficiency of a method with order and evaluations per step is . Compare Newton (, ) and secant.
Answer
Newton ; secant .
Solution
The efficiency index is the order per function evaluation; the secant method wins when costs as much as .
Exercise 3: Regula falsi stalls (code)
Run 20 steps of regula falsi (keep the bracket) for on and compare with brentq on the same bracket.
Answer
Regula falsi is still off by about after 20 steps (the right endpoint never moves); brentq reaches an error of about (its default tolerance) in 10 function calls.
Solution
from scipy.optimize import brentq
f = lambda x: x**10 - 1
a, b = 0.0, 1.3
for _ in range(20):
c = b - f(b) * (b - a) / (f(b) - f(a))
if f(a) * f(c) < 0:
b = c
else:
a = c
r, info = brentq(f, 0.0, 1.3, full_output=True)
print(f"regula falsi: error {abs(c - 1):.2e}, right end still {b}")
print(f"brentq: error {abs(r - 1):.1e} after {info.function_calls} calls")regula falsi: error 4.47e-02, right end still 1.3
brentq: error 1.5e-13 after 10 calls
Test yourself
The secant method's order of convergence is about…
- 1
- 1.618
- 2
- 3
Why can the secant method beat Newton in run time?
- It has higher order
- It needs only one function evaluation per step and no derivative
- It always brackets the root
- It uses less memory
How many starting values does the secant method need?
- 1
- 2
- 3
- none