-1
を解決するために、それを呼び出して、私はニュートン法ニュートン法による根を見つけるための機能と一般式
#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
ため、この機能を書かれており、二次方程式
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)
となっ解決するために、それを呼び出していますエラー
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
thnks
を修正してください
を'root_newton'です。同じことが 'derivative' btwでも起こります。 –
def def(dx):をdef def(x)に変更します。 –