Newton-Raphson Method - Python Numerical Methods
Newton-Raphson Method - Python Numerical Methods
This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers
and Scientists, the content is also available at Berkeley Python Numerical Methods. Print to PDF
The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning
experience. The code is released under the MIT license. If you find this content useful, please consider supporting the
work on Elsevier or Amazon!
< 19.3 Bisection Method | Contents | 19.5 Root Finding in Python >
Newton-Raphson Method
Let f (x) be a smooth and continuous function and xr be an unknown root of f (x). Now assume that x0 is a guess for xr .
Unless x0 is a very lucky guess, f (x0 ) will not be a root. Given this scenario, we want to find an x1 that is an improvement on
x0 (i.e., closer to xr than x0 ). If we assume that x0 is “close enough” to xr , then we can improve upon it by taking the linear
approximation of f (x) around x0 , which is a line, and finding the intersection of this line with the x-axis. Written out, the linear
approximation of f (x) around x0 is f (x) ′
≈ f (x0 ) + f (x0 )(x − x0 ) . Using this approximation, we find x1 such that
f (x 1 ) = 0 . Plugging these values into the linear approximation results in the equation
′
0 = f (x0 ) + f (x0 )(x1 − x0 ),
f (x0 )
which when solved for x1 is $x1 = x0 − ′
. $
f (x0 )
An illustration of how this linear approximation improves an initial guess is shown in the following figure.
Written generally, a Newton step computes an improved guess, xi , using a previous guess xi−1 , and is given by the equation
g(xi−1 )
xi = xi−1 − .
′
g (xi−1 )
The Newton-Raphson Method of finding roots iterates Newton steps from x0 until the error is less than the tolerance.
–
TRY IT! Again, the √2 is the root of the function f (x) = x
2
− 2 . Using x0 = 1.4 as a starting point, use the previous
–
equation to estimate √2 . Compare this approximation with the value computed by Python’s sqrt function.
2
1.4 − 2
x = 1.4 − = 1.4142857142857144
2(1.4)
import numpy as np
f = lambda x: x**2 - 2
f_prime = lambda x: 2*x
newton_raphson = 1.4 - (f(1.4))/(f_prime(1.4))
newton_raphson = 1.4142857142857144
sqrt(2) = 1.4142135623730951
TRY IT! Write a function my_newton(f , df , x0, tol), where the output is an estimation of the root of f, f is a function object
f (x) , df is a function object to f ′ (x) , x0 is an initial guess, and tol is the error tolerance. The error measurement should be
|f (x)| .
–
TRY IT! Use my_newton= to compute √2 to within tolerance of 1e-6 starting at x0 = 1.5.
https://pythonnumericalmethods.berkeley.edu/notebooks/chapter19.04-Newton-Raphson-Method.html 1/2
12/5/23, 9:54 AM Newton-Raphson Method — Python Numerical Methods
estimate = 1.4142135623746899
sqrt(2) = 1.4142135623730951
If x0 is close to xr , then it can be proven that, in general, the Newton-Raphson method converges to xr much faster than the
bisection method. However since xr is initially unknown, there is no way to know if the initial guess is close enough to the root
to get this behavior unless some special information about the function is known a priori (e.g., the function has a root close to
x = 0 ). In addition to this initialization problem, the Newton-Raphson method has other serious limitations. For example, if
the derivative at a guess is close to 0, then the Newton step will be very large and probably lead far away from the root. Also,
depending on the behavior of the function derivative between x0 and xr , the Newton-Raphson method may converge to a
different root than xr that may not be useful for our engineering application.
TRY IT! Compute a single Newton step to get an improved approximation of the root of the function
f (x) = x
3
+ 3x
2
− 2x − 5 and an initial guess, x0 = 0.29 .
x0 = 0.29
x1 = x0-(x0**3+3*x0**2-2*x0-5)/(3*x0**2+6*x0-2)
print("x1 =", x1)
x1 = -688.4516883116648
Note that f ′ (x0 ) = −0.0077 (close to 0) and the error at x1 is approximately 324880000 (very large).
that this root is much farther from the initial guess than the other root at x = 1 , and it may not be the root you wanted from
an initial guess of 0.
< 19.3 Bisection Method | Contents | 19.5 Root Finding in Python >
© Copyright 2020.
https://pythonnumericalmethods.berkeley.edu/notebooks/chapter19.04-Newton-Raphson-Method.html 2/2