Welcome, guest | Sign In | My Account | Store | Cart

Newton Raphson Root Finding

Python, 17 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# Name : Alexander Baker
# Date : 2nd December 2008
# Description : based on Newton Raphson method of root finding, based on 
#   x_{n+1} = x_{n} - f(x_{n})/df(x_{n})/dx
# http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.newton.html#scipy.optimize.newton
# http://pathfinder.scar.utoronto.ca/~dyer/csca57/book_P/node35.html

import scipy.optimize
from scipy import optimize

def q(x):
    return (3.0-x)*scipy.exp(x) - 3.0
    
def qprime(x):
    return (2.0-x)*scipy.exp(x)    

print scipy.optimize.newton(q, 5, qprime, maxiter=500)

3 comments

Gabriel Genellina 14 years, 10 months ago  # | flag

Someone else already said this on another of your recipes: tag them!

bruce wernick 14 years, 9 months ago  # | flag

Come on Alexander, Newton-Raphson can be written in pure Python in a few lines!

I think scipy is overrated. Ask any of the python experts about a numerical method and you get the brain dead response "why not use scipy" or any of the other bloated packages.

Why is there so little original thought in the Python community?

alexander baker (author) 14 years, 8 months ago  # | flag

Bruce,

Whilst necessity is the mother of all invention, familiarity breads contempt! Guess that I like to stand on the shoulders of others.

Alex