Python Newton方法 - 炸毁
我正在用牛顿方法遇到一些问题。我试图调试,但不知道似乎是根本原因。以为我会在这里发布它,看看是否有人可以提供帮助。
我有一个简单的功能来计算油密度RHO_P0,该功能需要初始猜测然后计算Rho_a,然后迭代以使Rho_p0的最终值。
from scipy.optimize import newton
def mccain_hill_rhop0(rho_p0, Rs,Sg,So):
try:
a0 = -49.8930
a1 = 85.0149
a2 = - 3.70373
a3 = 0.0479818
a4 = 2.98914
a5 = - 0.0356888
print(rho_p0)
rho_a=(a0+a1*Sg+a2*Sg*rho_p0+a3*Sg*rho_p0**2+a4*rho_p0+a5*rho_p0**2)
rho_p0=(Rs*Sg+4600*So)/(73.71+Rs*Sg/rho_a)
return rho_p0
except RuntimeError:
return None
Rs=1000
Sg=0.55
So=0.8
rho_p01=52.8-0.01*Rs
rho_p0= newton(mccain_hill_rhop0, rho_p01, args=(Rs,Sg,So,), tol=10**(-3) , maxiter=100)
print(rho_p0)
这是错误消息。我不知道为什么在第二次迭代之后吹来。有什么想法吗?感谢您的帮助
RuntimeError: Tolerance of 2.5470966514398777e+33 reached. Failed to converge after 7 iterations, value is 2.5470966514398786e+33.
42.8
42.80438
-452.7973894722485
1393.5747708318825
672642.4222763113
1505488387.2917664
8.483536060177454e+17
2.5470966514398786e+33
I am running into some issues with Newton method. I tried to debug but dont know what seems to be the root cause. Thought I will post it out here to see if somebody can help.
I have a simple function to compute oil density, rho_p0, which takes an initial guess and then computes rho_a and then iterates to make the final value for rho_p0.
from scipy.optimize import newton
def mccain_hill_rhop0(rho_p0, Rs,Sg,So):
try:
a0 = -49.8930
a1 = 85.0149
a2 = - 3.70373
a3 = 0.0479818
a4 = 2.98914
a5 = - 0.0356888
print(rho_p0)
rho_a=(a0+a1*Sg+a2*Sg*rho_p0+a3*Sg*rho_p0**2+a4*rho_p0+a5*rho_p0**2)
rho_p0=(Rs*Sg+4600*So)/(73.71+Rs*Sg/rho_a)
return rho_p0
except RuntimeError:
return None
Rs=1000
Sg=0.55
So=0.8
rho_p01=52.8-0.01*Rs
rho_p0= newton(mccain_hill_rhop0, rho_p01, args=(Rs,Sg,So,), tol=10**(-3) , maxiter=100)
print(rho_p0)
Here is the error message. I dont know why it blows after the second iteration. Any idea? Thanks for your help
RuntimeError: Tolerance of 2.5470966514398777e+33 reached. Failed to converge after 7 iterations, value is 2.5470966514398786e+33.
42.8
42.80438
-452.7973894722485
1393.5747708318825
672642.4222763113
1505488387.2917664
8.483536060177454e+17
2.5470966514398786e+33
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我重新布置了返回函数,以在分子中使用我要求解的变量,并且起作用。
输出
I rearranged the return function to have the variable I am solving for in the numerator and it worked.
output