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.

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.4 Newton-Raphson Method | Contents | 19.6 Summary and Problems >

Root Finding in Python

As you may think, Python has the existing root-finding functions for us to use to make things easy. The function we will use to find the root is f_solve from the scipy.optimize.

The f_solve function takes in many arguments that you can find in the documentation, but the most important two is the function you want to find the root, and the initial guess.

TRY IT! Compute the root of the function \(f(x) = x^3 - 100x^2 - x + 100\) using f_solve.

from scipy.optimize import fsolve
f = lambda x: x**3-100*x**2-x+100

fsolve(f, [2, 80])
array([  1., 100.])

We know that this function has two roots \(x = 1\) and \(x = 100\), therefore, we can get the two roots out fairly simple using the f_solve function.

< 19.4 Newton-Raphson Method | Contents | 19.6 Summary and Problems >