You are on page 1of 1

12/5/23, 9:55 AM Root Finding in Python — 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.

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
Print to PDF
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 >

© Copyright 2020.

https://pythonnumericalmethods.berkeley.edu/notebooks/chapter19.05-Root-Finding-in-Python.html 1/1

You might also like