I have written this function for newton's method
#root_Newton.py
def root_newton ( f, df, guess, tolerance = 1.0e-6):
dx = 2 * tolerance
while dx > tolerance:
x1 = x - f(x)/df(x)
dx = abs (x - x1)
x = x1
return x
and calling it to solve a quadratic equation
from math import *
from root_Newton import root_newton
def function(x):
x = x**2 - 1*x -6
return x
def derivative(dx):
dx = 2*x - 1
return dx
func = root_newton (function , derivative , 1.7)
print 'Found f(x) =0 at x = %0.8f +/- %0.8f' % ( func , tolerance)
and getting the error
File "quadfromnewtonsroot.py", line 11, in <module>
func = root_newton (function , derivative , 1.7)
File "/home/trina/Pictures/python/root_Newton.py", line 4, in root_newton
x1 = x - f(x)/df(x)
UnboundLocalError: local variable 'x' referenced before assignment
please help me fix the error, thnks