Python牛顿法问题
我一直在向社区寻求帮助,我很感激
所以我一直在开发一个程序来解决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()
I have been asking the community alot for help and i appreciate it all
So ive been working on a program that solves newtons method in python but for some reason its not working could someone look over it please? thanks you =)
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里遇到了无限循环:
该循环中重要的部分似乎是:
并且您的循环条件是
abs(x1-x2)
abs(x1-x2)
abs(x1-x2)
abs(x1-x2) < .0001
,所以让我们重写这个:所以也许
x2 -= (func1sub / func1dsub)
正在以错误的方式推动x2
。我会添加一个像这样的打印语句,并确保这些值实际上收敛:另外,我不太熟悉牛顿的方法,但在您的代码中
func1sub / func1dsub
永远不会改变,但应该'每次迭代都会改变吗?You're getting an infinite loop here:
The part of this loop which matters seems to be:
And your loop condition is
abs(x1-x2) < .0001
, so let's rewrite this:So maybe
x2 -= (func1sub / func1dsub)
is pushingx2
the wrong way. I'd add a print statement like this and make sure the values are actually converging:Also, I'm not that familiar with Newton's method, but in your code
func1sub / func1dsub
never changes, but shouldn't it change on every iteration?