Python Newton方法 - 炸毁

发布于 2025-02-03 15:02:37 字数 1126 浏览 1 评论 0原文

我正在用牛顿方法遇到一些问题。我试图调试,但不知道似乎是根本原因。以为我会在这里发布它,看看是否有人可以提供帮助。

我有一个简单的功能来计算油密度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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-02-10 15:02:37

我重新布置了返回函数,以在分子中使用我要求解的变量,并且起作用。

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
            rho_a=(a0+a1*Sg+a2*Sg*rho_p0+a3*Sg*rho_p0**2+a4*rho_p0+a5*rho_p0**2)
            print((rho_p0, rho_a))
            rho_p0=rho_a*(Rs*Sg+4600*So-73.71*rho_p0)/(Rs*Sg)
            return rho_p0
        except RuntimeError:
            return None

输出

(42.8, 20.580650689599977)
(42.80438, 20.581334274602213)
(59.200734064755764, 20.63974342124446)
(57.3824271698717, 20.879752959108018)
(57.3871110909038, 20.879213691644452)
57.38705738842851

I rearranged the return function to have the variable I am solving for in the numerator and it worked.

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
            rho_a=(a0+a1*Sg+a2*Sg*rho_p0+a3*Sg*rho_p0**2+a4*rho_p0+a5*rho_p0**2)
            print((rho_p0, rho_a))
            rho_p0=rho_a*(Rs*Sg+4600*So-73.71*rho_p0)/(Rs*Sg)
            return rho_p0
        except RuntimeError:
            return None

output

(42.8, 20.580650689599977)
(42.80438, 20.581334274602213)
(59.200734064755764, 20.63974342124446)
(57.3824271698717, 20.879752959108018)
(57.3871110909038, 20.879213691644452)
57.38705738842851
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文