4.3 Root finding methods
Fixed point iteration ¶ Fixed point iteration
If we want to numerically solve f ( x ) = 0 f(x) = 0 f ( x ) = 0 , rearrange f ( x ) f(x) f ( x ) to get g ( x ) = x g(x) = x g ( x ) = x .
Then, choose an inital guess x 0 x_0 x 0 and iterate:
x n + 1 = g ( x n ) x_{n+1} = g(x_n) x n + 1 = g ( x n ) until x n + 1 = x n x_{n+1} = x_n x n + 1 = x n to the desired precision (error tolerance).
Example: Fixed point iteration ¶ Solve
e − x 2 − x = 0 e^{-x^2} - x = 0 e − x 2 − x = 0 First rearrange
x = e − x 2 = g ( x ) \begin{align}
x = e^{-x^2} = g(x)
\end{align} x = e − x 2 = g ( x ) Then, guess x 0 = 0 x_0 = 0 x 0 = 0 and set up a table:
n n n x n x_n x n g ( x n ) g(x_n) g ( x n ) 0 0 1.000 1 1.000 0.368 2 0.368 0.873 ... ... ... 51 0.653 0.6528
At each iteration, g ( x n ) g(x_n) g ( x n ) is used as the new x n x_n x n until at n = 5 n = 5 n = 5 , where
x n = 0.6530 ≈ 0.6528 = g ( x n ) x_n = 0.6530 \approx 0.6528 = g(x_n) x n = 0.6530 ≈ 0.6528 = g ( x n ) .
Note that the choice of rearranging f ( x ) f(x) f ( x ) is not unique, and some choices may
be better than others. For example, convergence is only guaranteed when
∣ g ′ ( x ) ∣ ≤ k < 1 |g'(x)| \le k < 1 ∣ g ′ ( x ) ∣ ≤ k < 1 for all x x x in an interval around the solution. Experience
will guide your choice of rearrangement.
For example, to solve
e x − x 3 = 0 e^x - x^3 = 0 e x − x 3 = 0 One possible (but not obviously better) rearrangement is
e x = x 3 x = ln ( x 3 ) = 3 ln x = g ( x ) \begin{align}
e^x &= x^3 \\
x &= \ln(x^3) = 3 \ln x = g(x)
\end{align} e x x = x 3 = ln ( x 3 ) = 3 ln x = g ( x ) This method may still fail spectacularly! You can try to improve
the stability of the method by slowly “mixing” solutions:
x n + 1 ∗ = g ( x n ) x n + 1 = α x n + 1 ∗ + ( 1 − α ) x n \begin{align}
x_{n+1}^* &= g(x_n) \\
x_{n+1} &= \alpha x_{n+1}^* + (1-\alpha) x_n
\end{align} x n + 1 ∗ x n + 1 = g ( x n ) = α x n + 1 ∗ + ( 1 − α ) x n where α \alpha α is a mixing parameter. Smaller values of α \alpha α mix more
slowly, which can be more stable but also requires more iterations.
Bisection search ¶ Intermediate value theorem
If f ( x ) f(x) f ( x ) is continuous on a ≤ x ≤ b a \le x \le b a ≤ x ≤ b , then it takes on all values from
f ( a ) f(a) f ( a ) to f ( b ) f(b) f ( b ) at some point in that interval. As a result, if f ( a ) > 0 f(a) > 0 f ( a ) > 0 and
f ( b ) < 0 f(b) < 0 f ( b ) < 0 (or vice versa), then f ( x ) = 0 f(x) = 0 f ( x ) = 0 somewhere in that interval!
We can apply this concept to find roots using the following procedure, called
bisection search:
Bisection search
Chose a 0 a_0 a 0 and b 0 b_0 b 0 so f ( a ) f(a) f ( a ) and f ( b ) f(b) f ( b ) have opposite signs.
Starting at n = 0 n = 0 n = 0 , evaluate f ( x n ) f(x_n) f ( x n ) at the midpoint x n = ( a n + b n ) / 2 x_n = (a_n + b_n)/2 x n = ( a n + b n ) /2 .
If f ( x n ) ≈ 0 f(x_n) \approx 0 f ( x n ) ≈ 0 a solution is found within desired precision.
Otherwise, if f ( a n ) f(a_n) f ( a n ) and f ( x n ) f(x_n) f ( x n ) have the same sign, a n + 1 = x n a_{n+1} = x_n a n + 1 = x n ;
else, b n + 1 = x n b_{n+1} = x_n b n + 1 = x n
Increase n n n and repeat from step 2.
Example: Bisection search ¶ Solve
e − x 2 − x = f ( x ) = 0 e^{-x^2} - x = f(x) = 0 e − x 2 − x = f ( x ) = 0 Start with a 0 = 0 a_0 = 0 a 0 = 0 and b 0 = 1 b_0 = 1 b 0 = 1 , then make a table to implement the
procedure:
n n n a n a_n a n b n b_n b n x n x_n x n f ( a n ) f(a_n) f ( a n ) f ( b n ) f(b_n) f ( b n ) f ( x n ) f(x_n) f ( x n ) 0 0 1 0.5 1.0 -0.632 0.279 1 0.5 1 0.75 0.275 -0.632 -0.180 2 0.5 0.75 0.625 0.275 -0.1180 0.052 ... ... ... ... ... ... ... 9 0.625 0.654 0.653 0.001 -0.002 -0.0007
When n = 0 n=0 n = 0 , f ( x 0 ) f(x_0) f ( x 0 ) > 0 so a 1 a_1 a 1 = x 0 x_0 x 0 . Then, when n = 1 n=1 n = 1 , f ( x 1 ) f(x_1) f ( x 1 ) < 0
so b 2 b_2 b 2 = x 1 x_1 x 1 . We continue this procedure until convergence.
Newton-Raphson method ¶ Although we cannot immediately solve the nonlinear equation f ( x ) = 0 f(x) = 0 f ( x ) = 0 , we can
first linearize it, then solve the linear problem.
f ( x ) ≈ f ( x 0 ) + f ′ ( x 0 ) ( x − x 0 ) = 0 x ≈ x 0 − f ( x 0 ) f ′ ( x 0 ) \begin{align}
f(x) &\approx f(x_0) + f'(x_0)(x - x_0) = 0 \\
x &\approx x_0 - \frac{f(x_0)}{f'(x_0)}
\end{align} f ( x ) x ≈ f ( x 0 ) + f ′ ( x 0 ) ( x − x 0 ) = 0 ≈ x 0 − f ′ ( x 0 ) f ( x 0 ) Iterating this process gives the Newton-Raphson method of root finding
Note that this method can converge much more rapidly than the fixed-point or
bisection methods. However, it will fail if f ′ ( x n ) = 0 f'(x_n) = 0 f ′ ( x n ) = 0 .
Example 1: Newton-Raphson method ¶ Solve x 2 = 2 x^2 = 2 x 2 = 2 .
We can rewrite this as f ( x ) = x 2 − 2 = 0 f(x) = x^2-2 = 0 f ( x ) = x 2 − 2 = 0 . We will also evaluate the derivative
f ′ ( x ) = 2 x f'(x) = 2x f ′ ( x ) = 2 x . Let the initial guess be x 0 = 1 x_0 = 1 x 0 = 1 .
n n n x n x_n x n f ( x n ) f(x_n) f ( x n ) f ′ ( x n ) f'(x_n) f ′ ( x n ) 0 1.0 -1.0 2.0 1 1.5 0.25 3.0 2 1.417 6.94 × 1 0 − 3 6.94 \times 10^{-3} 6.94 × 1 0 − 3 2.833 3 1.414
This is close to the known value of 2 \sqrt{2} 2 !
Example 2: Newton-Raphson method ¶ Solve e − x 2 − x = 0 e^{-x^2} - x = 0 e − x 2 − x = 0 .
We define f ( x ) = e − x 2 − x f(x) = e^{-x^2} - x f ( x ) = e − x 2 − x and calculate f ′ ( x ) = − 2 x e − x 2 − 1 f'(x) = -2x e^{-x^2} - 1 f ′ ( x ) = − 2 x e − x 2 − 1 .
We choose an initial guess x 0 = 0 x_0 = 0 x 0 = 0 .
n n n x n x_n x n f ( x n ) f(x_n) f ( x n ) f ′ ( x n ) f'(x_n) f ′ ( x n ) 0 0 1.0 -1.0 1 1.0 -0.6321 -1.736 2 0.6358 0.03164 -1.849 3 0.6529
Note the rapid convergence compared to the methods above!
Skill builder problems ¶ Solve
cos x − x = 0 \cos x - x = 0 cos x − x = 0 to 3 significant figures by:
Rearrange as
x = cos ( x ) = g ( x ) x = \cos(x) = g(x) x = cos ( x ) = g ( x ) n n n x n x_n x n 0 0.500 1 0.878 2 0.639 3 0.803 4 0.695 5 0.768 6 0.719 7 0.752 8 0.730 9 0.745 10 0.735 11 0.742 12 0.737 13 0.740 14 0.738 15 0.740 16 0.739 17 0.739
Hence, x ≈ 0.739 x \approx 0.739 x ≈ 0.739 .
n n n a n a_n a n b n b_n b n x n x_n x n f ( a n ) f(a_n) f ( a n ) f ( b n ) f(b_n) f ( b n ) f ( x n ) f(x_n) f ( x n ) 0 0 1.00 0.500 1.00 -0.460 0.378 1 0.500 1 0.750 0.378 -0.460 -0.0183 2 0.500 0.750 0.625 0.378 -0.0183 0.186 3 0.625 0.750 0.688 0.186 -0.0183 0.0853 4 0.688 0.750 0.719 0.0853 -0.0183 0.0339 5 0.719 0.750 0.734 0.0839 -0.0183 0.0079 6 0.734 0.750 0. 742 0.0079 -0.0183 -0.0052 7 0.734 0.742 0.738 0.0079 -0.0052 0.0013 8 0.738 0.742 0.740 0.0013 -0.0052 -0.0019 9 0.738 0.740 0.739 0.0013 -0.0019 -0.0003 10 0.738 0.739 0.739 0.0013 -0.0003 0.0005
Hence, x ≈ 0.739 x \approx 0.739 x ≈ 0.739 .
For
f ( x ) = cos x − x f ′ ( x ) = − sin x − 1 \begin{align}
f(x) &= \cos x - x \\
f'(x) &= -\sin x - 1
\end{align} f ( x ) f ′ ( x ) = cos x − x = − sin x − 1 The Newton-Raphson update is:
x n + 1 = x n − f ( x n ) f ′ ( x n ) x_{n+1} = x_n - \frac{f(x_n)}{f'(x_n)} x n + 1 = x n − f ′ ( x n ) f ( x n ) n n n x n x_n x n f ( x n ) f(x_n) f ( x n ) f ′ ( x n ) f'(x_n) f ′ ( x n ) 0 1.571 -1.571 -2.0 1 0.7854 − 7.829 × 1 0 − 2 -7.829 \times 10^{-2} − 7.829 × 1 0 − 2 -1.7071 2 0.7395 − 7.549 × 1 0 − 4 -7.549 \times 10^{-4} − 7.549 × 1 0 − 4 -1.674 3 0.7391 − 7.513 × 1 0 − 8 -7.513 \times 10^{-8} − 7.513 × 1 0 − 8 -1.674 4 0.7391
Hence, x ≈ 0.739 x \approx 0.739 x ≈ 0.739 .