Python 类型错误';浮动';对象在使用for循环时不可调用
我一直在测试两个非线性方程的牛顿-拉普森方法, 原型代码可以工作,但我当时正在考虑如何使它更有效 很有用,因为原型是关于两个方程的输入 首先是猜测,最好实现for循环 而不是像6或10那样开始一个过程来解决一个问题 我研究的方程太多了Python 类型错误';浮动';对象在使用for循环时不可调用,python,methods,numerical,Python,Methods,Numerical,我一直在测试两个非线性方程的牛顿-拉普森方法, 原型代码可以工作,但我当时正在考虑如何使它更有效 很有用,因为原型是关于两个方程的输入 首先是猜测,最好实现for循环 而不是像6或10那样开始一个过程来解决一个问题 我研究的方程太多了 # Prototype of N-R for a system of two non-linear equations #evaluating functions of two variables # f(x,y)=1.6 * x ** 2 + 3.6 * x
# Prototype of N-R for a system of two non-linear equations
#evaluating functions of two variables
# f(x,y)=1.6 * x ** 2 + 3.6 * x * y - 7.8 * x - 2.6 * y + 5.2
# g(x,y)=0.9 * y ** 2 + 3.1 * x **2 - 6.2 * x + 6.2 * y
# x = 0.5
# y =0.4
from math import *
eq1 = raw_input('Enter the equation 1: ')
eq2 = raw_input('Enter the equation 2: ')
x0 = float(input('Enter x: '))
y0 = float(input('Enter y: '))
def f(x,y):
return eval(eq1)
def g(x,y):
return eval(eq2)
Ea_X = 1
x = x0
y = y0
for n in range(1, 8):
a = (f(x + 1e-06, y) - f(x,y)) / 1e-06 #in this one start the trouble
b = (f(x, y + 1e-06) - f(x,y)) / 1e-06
c = 0 - f(x,y)
d = (g(x + 1e-06, y) - g(x,y)) / 1e-06
eE = (g(x, y + 1e-06) - g(x,y)) / 1e-06
f = 0 - g(x,y)
print "f(x, y)= ", eq1
print "g(x, y)= ", eq2
print """x y """
print x, y
print """a b c d e f """
print a, b, c, d, e, f
print """
a * x + b * y = c
d * x + e * y = f
"""
print a," * x + ",b," * y = ",c
print d," * x + ",eE," * y = ",f
_Sy = (c - a * f / d) / (b - a * eE / d)
_Sx = (f / d) - (eE / d) * _Sy
Ea_X = (_Sx ** 2 + _Sy ** 2)**0.5
x = x + _Sx
y = y + _Sy
print "Sx = ", _Sx
print "Sy = ", _Sy
print "x = ", x
print "y = ", y
print "|X_1 - X_0| = ", Ea_X
问题是:
# Prototype of N-R for a system of two non-linear equations
# f(x,y)=1.6 * x ** 2 + 3.6 * x * y - 7.8 * x - 2.6 * y + 5.2
# g(x,y)=0.9 * y ** 2 + 3.1 * x **2 - 6.2 * x + 6.2 * y
# x = 0.5
# y =0.4
# evaluating functions of two variables
from math import *
eq1 = raw_input('Enter the equation 1: ')
eq2 = raw_input('Enter the equation 2: ')
x0 = float(input('Enter x: '))
y0 = float(input('Enter y: '))
def f(x,y):
return eval(eq1)
def g(x,y):
return eval(eq2)
Ea_X = 1
x = x0
y = y0
a = (f(x + 1e-06, y) - f(x,y)) / 1e-06
b = (f(x, y + 1e-06) - f(x,y)) / 1e-06
c = 0 - f(x,y)
d = (g(x + 1e-06, y) - g(x,y)) / 1e-06
eE = (g(x, y + 1e-06) - g(x,y)) / 1e-06
f = 0 - g(x,y)
print "f(x, y)= ", eq1
print "g(x, y)= ", eq2
print """x y """
print x, y
print """a b c d e f """
print a, b, c, d, e, f
print """
a * x + b * y = c
d * x + e * y = f
"""
print a," * x + ",b," * y = ",c
print d," * x + ",eE," * y = ",f
_Sy = (c - a * f / d) / (b - a * eE / d)
_Sx = (f / d) - (eE / d) * _Sy
Ea_X = (_Sx ** 2 + _Sy ** 2)**0.5
x = x + _Sx
y = y + _Sy
print "Sx = ", _Sx
print "Sy = ", _Sy
print "x = ", x
print "y = ", y
print "|X_1 - X_0| = ", Ea_X
您正在将f
从函数重新绑定到行中的float
f = 0 - g(x,y)
您可以为名称
f
指定一个数字。由于函数和其他变量在Python中共享一个名称空间(函数只是一个可调用对象,绑定到任何变量),这使得未来的迭代失败。为上行中指定的值选择另一个名称。f=1.0;f(“oop”)
-一旦错误重现,只需确定原因;沿着小路往回走。嗯,…,没错,我刚刚用“fF”重命名了“f”,thanx:P
f = 0 - g(x,y)