【发布时间】:2011-12-06 04:37:41
【问题描述】:
我一直在向社区寻求帮助,我很感激
所以我一直在研究一个解决python中牛顿方法的程序,但由于某种原因它不起作用,有人可以看看吗?谢谢你=)
import sympy
from collections import defaultdict
def main():
dir(sympy)
print ("NEWTONS METHOD")
print ("Write your expression in terms of 'x' ")
e = sympy.sympify(raw_input("input expression here: "))
f = sympy.Symbol('x')
func1 = e
func1d = sympy.diff(e,f) #takes the dirivative of the function
print ("the dir of your function = "), func1d
x = input("number to substitute for x: ")
a = input("how many digits would you like to round to [recomended at least 4]")
func1sub = func1.subs({'x':x}) #substitutes the user given value of x into the equation
func1dsub = func1d.subs({'x':x}) #substitutes the user given value of x into the equation
func1sub = float(func1sub)
func1dsub = float(func1dsub)
func1sub = round(func1sub)
func1dsub = round(func1dsub)
round(func1sub,a)
round(func1dsub,a)
n = x - (func1sub/func1dsub)
x1 = 0
x2 = 0
n = x - (func1sub/func1dsub)
x1 = n
x1 = round(x1)
n = x2 - (func1sub/func1dsub)
x2 = n
x2 = round(x2)
while 0 == 0:
if abs(x1-x2) < .0001:
print x1
break
else:
n = x2 - (func1sub/func1dsub)
x2 = n
if abs(x - n) < .03:
print x
if func1dsub == 0:
print ("ERROR CAN NOT DIVIDE BY 0")
main()
【问题讨论】:
-
它在我运行它之后就像它在做某事一样挂起,但没有任何结果。我已经查看了几个小时了,实际上并没有看到有什么问题,我假设它有一些语法错误或者对 python 来说很新。
-
你不应该在循环的每一步替换值吗?
-
你的意思是在每次迭代后重新添加替代品吗?
-
这似乎更好地转移到http://codereview.stackexchange.com/
-
@moooeeeeep,代码审查只针对工作代码。此处发布的任何损坏的代码都将关闭或迁移回此处。
标签: python loops equation newtons-method