May 10, 2024

Implement Newton’s Method in Python From Scratch by Using SymPy Symbolic Toolbox and Automatic Differentiation


In this post, we explain how to implement Newton’s method in Python from scratch. We will be using the SymPy symbolic toolbox to automatically differentiate the function and automatically define the Newton iteration. The YouTube video accompanying this video is given below

First, we briefly revise Newton’s method. The purpose of Netwon’s method is to solve the equations having the following form:

(1)   \begin{align*}f(x)=0\end{align*}

starting from some initial guess of the variable x that is denoted by x_{0}. The idea of Newton’s method is shown is illustrated in the Figure below.

The basic idea is to compute a line that passes through the point x_{n} and f(x_{n}) and that is tangent to the function. This function will intersect the x axis at the point x_{n+1}. Then, we repeat this procedure for the point (x_{n+1},f(x_{n+1})). That is, we generate the tangent line at the point (x_{n+1},f(x_{n+1})). By repeating this procedure iteratively, we define a series of points x_{n},x_{n+1},x_{n+2},\ldots that under some conditions that we will not discuss here, will converge to the solution of (1). Let us formulate this procedure mathematically. From the above figure, we see that the slope of the function f(x) at the point x_{n} is given by

(2)   \begin{align*}f' (x_{n})=\frac{f(x_{n})-0}{x_{n}-x_{n+1}}\end{align*}

From the last equation, we can determine x_{n+1} as follows

(3)   \begin{align*}x_{n+1}=x_{n}-\frac{f(x_{n})}{f'(x_{n})}\end{align*}

So Newton’s method can be summarized as follows

  1. Pick an initial guess of the solution for n=0, that is, select x_{0}
  2. For n=0,1,2,3,\ldots, update the solution

(4)   \begin{align*}x_{n+1}=x_{n}-\frac{f(x_{n})}{f'(x_{n})}\end{align*}

Let us write the Python code to compute this solution. Let us use the following test case

(5)   \begin{align*}f(x)=x^2+10x+21\end{align*}

The zeros of this function are obviously at x=-3 and x=-7. This can be seen by either recalling the formula for the zeros of the quadratic equation

(6)   \begin{align*}ax^2+bx+c=0\end{align*}


that are given by

(7)   \begin{align*}x_{1,2}=\frac{-b\pm \sqrt{b^2-4ac}}{2a}\end{align*}

or by recalling that the zeros of the quadratic function directly participate in the coefficients:

(8)   \begin{align*}x^2-(x_{1}+x_{2})x+x_{1}x_{2}=0\end{align*}

We will use Python to code the Newton method. We will use Python symbolic toolbox to perform differentiation. Instead of manually computing the first derivative, we will let Python to symbolically differentiate the function f(x). The code is given below

# -*- coding: utf-8 -*-
"""
Created on Wed May 25 08:37:25 2022

@author: ahaber
"""

# Newtwon method implementation in python

import sympy as sym
import numpy as np


x=sym.Symbol('x')

f=x**2+10*x+21

diff_f=sym.diff(f,x)
diff_f

f_func=sym.lambdify(x,f,'numpy')
diff_f_func=sym.lambdify(x,diff_f,'numpy')

def newtonMethod(x0,iterationNumber, f,df):
    x=x0
    
    for i in range(iterationNumber):
        
        x=x-f(x)/df(x)
    
    residual=np.abs(f(x))
    return x,residual

solution,residual = newtonMethod(-2,200,f_func,diff_f_func)

We use the Python toolbox SymPy to define symbolic variable x on the code line 14. The code line 16 is used to define the function f(x). The code line 18 is used to differentiate the function. The code lines 21 and 22 are used to convert the defined function and its derivative into an executable function that can be used as a standard function (see the newtonMethod function defined in the sequel). The function “newtonMethod” takes as arguments x_{0} initial guess of the solution, number of iterations, and functions defining f and its derivative. We simply iterate over “iterationNumber” and update the solution x. A the end we return the computed solution and the residual.

That is it! It is simple as that. This code can be modified and used in more complex codes.