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