Python 用scipy fsolve求解非线性二元方程时的运行时误差

Python 用scipy fsolve求解非线性二元方程时的运行时误差,python,scipy-optimize,Python,Scipy Optimize,我试图用SCIPY FSOLVE优化解决一个复杂的水蒸发问题,但它会产生运行时错误。我已经付出了所有的努力,为了进一步,我们需要这个解决方案。我附上我的代码和所有必需的数据。我无法在代码中确定此错误背后的确切原因。请帮忙 import numpy as np h0 = 50 # CBL height at initial time in meters # calculation of saturation deficit theta0 = 4.78*h0/1000 + 293.6 # init

我试图用SCIPY FSOLVE优化解决一个复杂的水蒸发问题,但它会产生运行时错误。我已经付出了所有的努力,为了进一步,我们需要这个解决方案。我附上我的代码和所有必需的数据。我无法在代码中确定此错误背后的确切原因。请帮忙

import numpy as np
h0 = 50 # CBL height at initial time in meters 
# calculation of saturation deficit
theta0 = 4.78*h0/1000 + 293.6 # initial air temp (K) at h0 (km)
q0 = -0.00285*h0/1000 + 0.01166 # initial specific humidity (kg/kg) at h0 (km)
estar = 0.611*np.exp((17.3*(theta0-273.15))/((theta0-273.15)+237.3))/1000 # calculating saturation vapor pressure and taking temp in celcius
qstar = 0.622*estar/0.101325 # saturated specific humidity at given temp
saturation_deficit = qstar-q0
alpha = 24
def D(alpha, saturation_deficit):
    if saturation_deficit<0:
        D = 0
    elif saturation_deficit>(1/alpha):
        D = 1/alpha
    else:
        D = saturation_deficit
    return D;
D = D(alpha,saturation_deficit)
# Calculation of surface resistance
t0 = 5 #hrs
t = 6 #hrs
PSIS = -0.1 # in MPa
Sx = 800 # watt/square meters
delta = 14 # hrs
c = 400
St = Sx*np.sin(np.pi*(t-t0)/delta)
d = 1+c/1000
f1S = (c+St)/(d*St)
f2T = 1
f3D = (1-alpha*D)**(-1)
# calculation of fourth stress function in calculation of rs
rrs = 0.0047 # in MPa
Ksat = 6.3*10**(-6) # in meter/s
PSISAT = -0.003 # in MPa
b = 7.1
k1 = 0.4*10**(-11)
Zef = 1 # effective depth in meters
Ks = Ksat*(PSISAT/PSIS)**(3/b+2)
rsr = 0.0013*k1/(Zef*Ks)
rsp = rsr+rrs
n = 5.5
PSIC = -2 # value in megapascal
rsmin = 40
r123 = rsmin*f1S*f2T*f3D
# calculation of ra
# calculting vapor pressure from sp humidity
e = q0*0.101325*10000/0.622 # taking e in hPa
ea = 0.522*e**(1/7)
sigma = 5.67*10**(-8) # in w/square meter/k to power 4
L = ea*sigma*theta0**(4)
a = 0.2
etav = 0.97
f = 0.05
U = 4 # in m/s
k = 0.4 #van-karman constant
p = 3/4
z0 = 0.05 # m
zr = 50 # m
g = 9.81
ra0 = (np.log(zr/z0))**(2)/U/k**(2)
zeta = 0.622/0.101325*4098*estar/(237.3+theta0-273.15)**(2)
# please focus on this optimization
import numpy as np
from scipy.optimize import fsolve
def func(x):
    return [x[0]*(zeta+1+(r123*(1 + ((PSIS-rsp*x[0])/PSIC)**(n)))/(ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2)))**(p)))-(zeta*(1-f)*((1-a)*St+etav*(L-sigma*x[1]**(4)))+1.225*2260*1000*D/((ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2)))**(p)))),
            x[1] - theta0+(ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2)))**(p))*((1-f)*((1-a)*St+etav*(L-sigma*x[1]**(4)))-x[0])/1.225/1005]
root = fsolve(func, [.00011, 27])
root

Out[44]: array([ 1., 27.])

import numpy as np
from scipy.optimize import fsolve
def func(x):
    return [x[0]*(zeta+1+(r123*(1 + ((PSIS-rsp*x[0])/PSIC)**(n)))/(ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2)))**(p)))-(zeta*(1-f)*((1-a)*St+etav*(L-sigma*x[1]**(4)))+1.225*2260*1000*D/((ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2)))**(p)))),
            x[1] - theta0+(ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2)))**(p))*((1-f)*((1-a)*St+etav*(L-sigma*x[1]**(4)))-x[0])/1.225/1005]
root = fsolve(func, [.00011, 27])
root
C:\Users\Dell\thesis work paper result 1 regeneration\untitled1.py:4: RuntimeWarning: invalid value encountered in double_scalars
C:\Users\Dell\thesis work paper result 1 regeneration\untitled1.py:5: RuntimeWarning: invalid value encountered in double_scalars
将numpy导入为np
h0=50#初始时间的CBL高度,以米为单位
#饱和赤字的计算
θ=4.78*h0/1000+293.6#h0(km)时的初始空气温度(K)
q0=-0.00285*h0/1000+0.01166#h0(km)处的初始比湿度(kg/kg)
estar=0.611*np.exp((17.3*(θ0-273.15))/(θ0-273.15)+237.3))/1000 35;计算饱和蒸汽压并以celcius为单位测量温度
qstar=0.622*estar/0.101325#给定温度下的饱和比湿度
饱和_赤字=qstar-q0
α=24
def D(α,饱和_赤字):
如果饱和度不足(1/alpha):
D=1/α
其他:
D=饱和/赤字
返回D;
D=D(α,饱和\亏损)
#表面电阻的计算
t0=5小时
t=6小时
PSIS=-0.1#单位为兆帕
Sx=800瓦/平方米
增量=14小时
c=400
St=Sx*np.sin(np.pi*(t-t0)/delta)
d=1+c/1000
f1S=(c+St)/(d*St)
f2T=1
f3D=(1-α*D)**(-1)
#rs计算中第四应力函数的计算
rrs=0.0047(单位:MPa)
Ksat=6.3*10**(-6)#以米/秒为单位
PSISAT=-0.003#单位为兆帕
b=7.1
k1=0.4*10**(-11)
Zef=1#有效深度(米)
Ks=Ksat*(PSISAT/PSIS)**(3/b+2)
rsr=0.0013*k1/(Zef*Ks)
rsp=rsr+rrs
n=5.5
PSIC=-2#兆帕斯卡值
rsmin=40
r123=rsmin*f1S*f2T*f3D
#ra的计算
#从sp湿度计算蒸汽压
e=q0*0.101325*10000/0.622#取hPa中的e
ea=0.522*e**(1/7)
西格玛=5.67*10**(-8)#单位为w/平方米/k,功率为4
L=ea*sigma*theta0**(4)
a=0.2
etav=0.97
f=0.05
U=4#m/s
k=0.4#范卡门常数
p=3/4
z0=0.05#m
zr=50#m
g=9.81
ra0=(np.log(zr/z0))**(2)/U/k**(2)
zeta=0.622/0.101325*4098*estar/(237.3+theta0-273.15)**(2)
#请关注这个优化
将numpy作为np导入
从scipy.optimize导入fsolve
def func(x):
返回[x[0]*(zeta+1+(r123*(1+((rsp*x[0])/PSIC)**(n))/(ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2))**(p))-(zeta*(1-f)*(1-a)*St+etav,
x[1]-θ0+(ra0/(1+(5*zr*g*(x[1]-θ0)/θ0/U**(2))**(p))*((1-f)*((1-a)*St+etav*(L-sigma*x[1]**(4))-x[0])/1.225/1005]
root=fsolve(func,[.00011,27])
根
Out[44]:数组([1,27.])
将numpy作为np导入
从scipy.optimize导入fsolve
def func(x):
返回[x[0]*(zeta+1+(r123*(1+((rsp*x[0])/PSIC)**(n))/(ra0/(1+(5*zr*g*(x[1]-theta0)/theta0/U**(2))**(p))-(zeta*(1-f)*(1-a)*St+etav,
x[1]-θ0+(ra0/(1+(5*zr*g*(x[1]-θ0)/θ0/U**(2))**(p))*((1-f)*((1-a)*St+etav*(L-sigma*x[1]**(4))-x[0])/1.225/1005]
root=fsolve(func,[.00011,27])
根
C:\Users\Dell\thesis work paper result 1 regeneration\untitled1.py:4:RuntimeWarning:在双标量中遇到无效值
C:\Users\Dell\thesis work paper result 1 regeneration\untitled1.py:5:RuntimeWarning:在双标量中遇到无效值

您至少应提供所有必要的变量(例如,
Rn
缺失),并正确设置代码格式。这增加了有人愿意帮助的机会。@joni很抱歉,我没有提供Rn,尽管我正在优化的方程不包括Rn。我已经编辑了这个问题。谢谢如果可能的话,请帮忙