Back to Code List

Newton Method

A super simple implementation of Newton's Method as an example of passing functions in python.

def power(x,num): return x**num

def f1(x) : return power(x,3) - 2*x - 5

def f2(x) : return power(x,6) - 2

def dfdx1(x) : return 3*power(x,2) - 2

def dfdx2(x) : return 6*power(x,5)

def newton_raphson(guess, f, dfdx):
    return guess - f(guess)/dfdx(guess)

print newton_raphson(2,f1,dfdx1)
print newton_raphson(1,f2,dfdx2)