代码如下,133行算的a出现虚数,a不应当是虚数,导致134行条件无法运行。
这些代码运行出来得到的Evaporation都等于Ep,实际上不是这样的。似乎我设置的那些if条件没有运行,不知道错在哪里了
import pandas as pd
import numpy as np
import math
df = pd.read_excel("1987.xlsx")#读取资料
WM, WUM, WLM = 140, 20, 70#给定参数
WDM = WM - WUM - WLM
B, C = 0.3, 0.16
WMM = WM * (1 + B)
IM = 0.002
FC = 24
CG = 0.98
W0, WU0, WL0, WD0 =100, 10, 40, 50
Kc = 0.97
print(df.values)#打印一下看看读取得对不对
df["P"] = df["P1"] * 0.33 + df["P2"] * 0.14 + df["P3"] * 0.33 + df["P4"] * 0.20#算面平均降雨量
df["Ep"] = df["E0"] * Kc#算蒸发能力
print(df["Ep"])
print(df["P"])
df.to_excel("1987forecast.xlsx")#生成数据
Precipitation = list(df["P"])#将降雨资料以列表形式呈现
print(Precipitation)
Ep = list(df["Ep"])#将蒸发能力以列表形式呈现
print(Ep)
WU = []
WL = []
WD = []
W = []
EU = []
EL = []
ED = []
Evaporation =[]
R = []#第一天的R没算
PE = []
a = []
#第一天数据
if WU0 + Precipitation[0] >= Ep[0]:
EU.append(Ep[0])
EL.append(0)
ED.append(0)
elif WU0 + Precipitation[0] < Ep[0] and WL0 >= C * WLM:
EU.append(WU0 + Precipitation[0])
EL.append(round((Ep[0] - EU[0]) * WL0 / WLM,1))
ED.append(0)
elif WU0 + Precipitation[0] < Ep[0] and WL0 < C * WLM and WL0 >= C * (Ep[0] - EU[-1]):
#有问题,EU
EU.append(WU0 + Precipitation[0])
EL.append(round(C * (Ep[0] - EU[0]),1))
ED.append(0)
elif WU0 + Precipitation[0] < Ep[0] and WL0 >= C * (Ep[0] - EU[-1]):
EU.append(round(WU0 + Precipitation[0],1))
EL.append(WL0)
ED.append(round(C * (Ep[0] - EU[0]) - EL[0],1))
Evaporation.append(round(EU[0] + EL[0] + ED[0],1))#逻辑有问题但不影响
PE.append(Precipitation[0] - Evaporation[0])#计算第一天的PE
if PE[0] > 0:
a.append(WMM * (1 - pow((1 - W0/WM), (1 / (1 + B)))))#计算a;这里写的W0
if a + PE[0] <= WMM:
R.append(PE[0] - WM * pow((1 - a / WMM), B + 1) + WM * pow((1 - (PE[0] + a) / WMM), B + 1))
else:
R.append(PE[0] - (WM - W0))#这里写的W0
else:
R.append(0)
a.append(0)
if WU0 + Precipitation[0] - R[0] <= WUM:
WU.append(WU0 + Precipitation[0] - R[0])
WL.append(WL0)
WD.append(WD0)
else:
if WL0 + WU0 + Precipitation[0] - R[0] - WLM - WUM<= WUM + WLM:
WU.append(WLM)
WL.append(WL0 + WU0 + Precipitation[0] - R[0] - WLM)
WD.append(WD0)
else:
if WD0 + WU0 + WL0 + Precipitation[0] - R[0] - WUM - WLM - WDM <= WUM + WLM:
WU.append(WUM)
WL.append(WLM)
WD.append(WD0)
else:
WU.append(WUM)
WL.append(WLM)
WD.append(WDM)
W.append(WU[0] + WL[0] + WD[0])
print(R)
print(WU)
print(WL)
print(WD)
print(W)
print(EU)
print(EL)
print(ED)
print(Evaporation)
#第二天及之后
for i in range(1,365):#第二天,索引从1开始
if WU[i-1] + Precipitation[i] >= Ep[i]:#计算E
EU.append(Ep[i])
EL.append(0)
ED.append(0)
elif WU[i-1] + Precipitation[i] < Ep[i] and WL[i-1] >= C * WLM:
EU.append(WU[i-1] + Precipitation[i])
EL.append(round((Ep[i] - EU[i]) * WL[i-1] / WLM,1))
ED.append(0)
elif WU[i-1] + Precipitation[i] < Ep[i] and WL[i-1] < C * WLM and WL[i-1] >= C * (Ep[i] - EU[i]):
EU.append(WU[i-1] + Precipitation[i])
EL.append(round(C * (Ep[i] - EU[i]),1))
ED.append(0)
elif WU[i-1] + Precipitation[i] < Ep[i] and WL[i-1] >= C * (Ep[i] - EU[i]):
EU.append(WU[i-1] + Precipitation[i])
EL.append(WL[i-1])
ED.append(round(C * (Ep[i] - EU[i]) - EL[i],1))
Evaporation.append(EU[i] + EL[i] + ED[i])
PE.append(Precipitation[i] - Evaporation[i])
if PE[i] > 0:
a.append(WMM * (1 - pow((1 - W[i-1]/WM), (1 / (1 + B)))))#计算a
if a[i] + PE[i] <= WMM:
R.append(PE[i] - WM * pow((1 - a[i] / WMM), B + 1) + WM * pow((1 - (PE[i] + a[i]) / WMM), B + 1))
else:
R.append(PE[i] - (WM - W[i-1]))
else:
R.append(0)
a.append(0)
if WU[i-1] + Precipitation[i] - R[i] <= WUM:#计算W
WU.append(WU[i-1] + Precipitation[i] - R[i])
WL.append(WL[i-1])
WD.append(WD[i-1])
else:
if WL[i-1] + WU[i-1] + Precipitation[i] - R[i] - WLM - WUM <= WUM + WLM:
WU.append(WLM)
WL.append(WL[i-1] + WU[i-1] + Precipitation[i] - R[i] - WLM)
WD.append(WD[i-1])
else:
if WD[i-1] + WU[i-1] + WL[i-1] + Precipitation[i] - R[i] - WUM - WLM - WDM <= WUM + WLM:
WU.append(WUM)
WL.append(WLM)
WD.append(WD[i-1])
else:
WU.append(WUM)
WL.append(WLM)
WD.append(WDM)
W.append(WU[i] + WL[i] + WD[i])
print(WU)
print(WL)
print(WD)
print(W)
print(EU)
print(EL)
print(ED)
print(Evaporation)
print(R)
df["EU"] = EU
df["EL"] = EL
df["ED"] = ED
df["WU"] = WU
df["WL"] = WL
df["WD"] = WD
df["W"] = W
df["Evaporation"] = Evaporation
df["PE"] = PE
df["R"] = R
df.to_excel("1987forecast.xlsx")#生成数据
需要用到的Precipitation:
[0.034, 0.66, 0.16, 4.5120000000000005, 0.020000000000000004, 0.0, 0.0, 0.0, 0.020000000000000004, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.014000000000000002, 0.08200000000000002, 0.0, 0.0, 0.0, 0.23099999999999998, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.014000000000000002, 0.24000000000000002, 0.0, 0.0, 0.020000000000000004, 0.020000000000000004, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.12, 7.992000000000001, 1.957, 10.606, 4.156, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.198, 0.0, 0.0, 0.23099999999999998, 0.033, 0.0, 7.296000000000001, 43.093, 72.23100000000001, 45.976000000000006, 6.216, 6.328, 35.088, 45.42100000000001, 0.9590000000000001, 10.775, 56.504999999999995, 6.0969999999999995, 0.033, 0.0, 0.0, 0.033, 0.0, 0.0, 1.7500000000000002, 9.992, 5.915, 4.713, 32.417, 22.796, 0.846, 0.15200000000000002, 0.014000000000000002, 2.441, 12.835, 15.089, 39.414, 3.899, 0.033, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.336, 1.219, 0.0, 0.098, 0.423, 0.77, 7.799, 5.640000000000001, 0.099, 0.014000000000000002, 0.42900000000000005, 0.42000000000000004, 2.7060000000000004, 1.077, 7.159000000000001, 41.566, 2.511, 2.849, 24.581, 31.988000000000003, 52.10600000000001, 61.525, 0.42300000000000004, 7.3580000000000005, 9.382000000000001, 14.501500000000002, 26.937500000000004, 27.831, 0.11200000000000002, 5.059, 3.9195, 8.7795, 2.0829999999999997, 2.934, 7.884, 6.0440000000000005, 8.765500000000001, 2.1385, 2.442, 0.396, 0.0, 0.0, 0.0, 0.0, 0.084, 1.86, 1.13, 0.468, 3.8250000000000006, 8.237, 2.2310000000000003, 7.301, 0.0, 0.0, 0.05600000000000001, 0.033, 1.8760000000000003, 14.155000000000001, 0.23099999999999998, 0.388, 4.460000000000001, 4.26, 16.481, 16.55, 1.4700000000000002, 3.1550000000000002, 12.760000000000002, 28.155, 2.3360000000000003, 1.436, 0.0, 0.0, 2.0580000000000003, 1.9730000000000003, 12.352, 3.321, 5.764000000000001, 20.184, 19.383, 6.396, 4.139, 4.223000000000001, 12.389000000000003, 8.154, 5.264000000000001, 29.499000000000002, 17.914, 4.9830000000000005, 56.693, 167.08800000000002, 46.874, 45.217000000000006, 2.044, 1.142, 0.0, 0.0, 0.0, 0.0, 0.0, 4.719, 21.860000000000003, 0.0, 0.0, 0.0, 0.168, 2.0940000000000003, 12.083000000000002, 22.697000000000003, 2.5180000000000002, 15.788, 1.8290000000000002, 10.776, 28.62, 18.259, 0.9830000000000001, 0.066, 0.0, 0.033, 0.0, 0.098, 1.6170000000000002, 0.0, 0.6940000000000001, 10.000000000000002, 0.5810000000000001, 0.0, 0.0, 0.0, 0.028000000000000004, 0.33099999999999996, 0.0, 0.0, 0.0, 0.45400000000000007, 2.3890000000000007, 9.101, 10.227, 0.0, 0.0, 1.8810000000000002, 0.033, 0.0, 1.484, 11.991, 2.662, 0.29000000000000004, 12.144, 30.325000000000003, 3.6050000000000004, 0.0, 0.0, 0.0, 0.0, 0.42000000000000004, 0.79, 0.0, 0.0, 0.0, 0.0, 4.737, 1.3969999999999998, 0.275, 0.0, 0.466, 1.865, 0.0, 0.014000000000000002, 0.0, 0.0, 0.020000000000000004, 1.012, 0.033, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15200000000000002, 0.0, 1.1700000000000002, 2.617, 0.0, 0.30800000000000005, 0.132, 0.46199999999999997, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.539000000000001, 8.264000000000001, 13.225, 0.0, 0.23099999999999998, 0.0, 0.0, 0.0, 12.453, 45.52700000000001, 9.451, 0.742, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.614, 0.0, 0.0]
需要用到的Ep
[2.91, 0.582, 1.067, 0.873, 2.8129999999999997, 2.328, 2.619, 2.425, 1.649, 0.776, 2.522, 3.783, 3.589, 2.91, 1.067, 2.231, 2.328, 2.134, 3.007, 1.261, 1.94, 1.746, 3.298, 5.044, 4.85, 3.492, 2.619, 3.395, 2.328, 2.425, 2.425, 2.231, 3.9769999999999994, 4.365, 2.8129999999999997, 2.7159999999999997, 2.8129999999999997, 3.104, 2.91, 1.94, 2.7159999999999997, 2.619, 1.649, 2.037, 2.8129999999999997, 2.425, 2.619, 2.037, 3.104, 3.007, 3.88, 3.007, 3.2009999999999996, 1.552, 1.649, 1.746, 1.455, 1.94, 2.134, 2.425, 3.007, 1.746, 1.3579999999999999, 2.619, 2.7159999999999997, 3.2009999999999996, 1.746, 1.843, 4.753, 1.164, 2.328, 3.9769999999999994, 3.007, 2.037, 3.395, 3.783, 1.261, 0.776, 2.134, 2.134, 0.6789999999999999, 1.067, 2.037, 3.492, 2.134, 0.582, 1.3579999999999999, 2.231, 2.619, 3.589, 2.231, 3.88, 1.843, 0.388, 2.037, 1.067, 0.582, 1.552, 2.425, 3.686, 6.208, 3.2009999999999996, 1.455, 1.552, 3.492, 1.746, 1.94, 2.231, 5.238, 2.619, 1.843, 1.94, 6.014, 4.074, 4.365, 5.82, 2.7159999999999997, 2.91, 4.074, 2.91, 4.753, 3.395, 3.589, 2.134, 1.94, 2.91, 3.395, 1.164, 2.231, 2.134, 3.9769999999999994, 5.4319999999999995, 4.85, 2.7159999999999997, 4.268, 3.395, 1.649, 1.3579999999999999, 2.7159999999999997, 1.552, 1.261, 0.97, 0.97, 2.619, 3.492, 1.455, 1.067, 1.067, 1.649, 1.067, 2.619, 3.007, 3.783, 2.619, 0.97, 1.3579999999999999, 1.94, 1.94, 3.88, 1.746, 4.365, 4.365, 3.2009999999999996, 4.074, 3.88, 4.365, 1.455, 2.134, 1.3579999999999999, 1.261, 2.8129999999999997, 2.231, 3.88, 3.589, 2.522, 4.074, 4.268, 3.783, 3.104, 5.335, 4.85, 4.656, 2.8129999999999997, 6.305, 4.753, 5.335, 4.462, 4.074, 3.783, 4.170999999999999, 4.074, 4.656, 4.85, 5.529, 3.686, 5.141, 4.074, 4.559, 0.776, 4.074, 3.88, 4.559, 4.365, 2.231, 3.88, 0.0, 2.8129999999999997, 3.589, 2.619, 4.074, 0.291, 0.291, 2.8129999999999997, 3.2009999999999996, 4.85, 4.559, 5.044, 4.656, 3.298, 4.946999999999999, 4.85, 3.686, 3.88, 4.85, 5.529, 4.946999999999999, 2.91, 1.261, 4.462, 3.589, 3.783, 3.492, 2.7159999999999997, 2.522, 4.268, 4.85, 3.88, 4.85, 4.85, 4.85, 4.559, 3.007, 3.686, 4.268, 3.783, 3.686, 4.365, 4.559, 4.462, 3.88, 5.4319999999999995, 7.469, 5.044, 3.783, 2.328, 4.365, 4.753, 4.753, 3.9769999999999994, 3.492, 4.559, 3.395, 3.686, 2.522, 0.97, 2.231, 2.91, 6.014, 1.649, 2.037, 1.94, 2.425, 1.843, 2.619, 3.007, 2.425, 5.335, 3.9769999999999994, 3.007, 3.298, 3.9769999999999994, 1.552, 3.395, 2.425, 3.686, 3.783, 2.619, 4.85, 3.9769999999999994, 2.425, 3.9769999999999994, 3.88, 2.134, 3.88, 3.589, 4.85, 5.335, 3.88, 3.88, 1.261, 0.873, 1.649, 1.455, 1.261, 3.589, 5.723, 3.104, 5.335, 2.522, 2.91, 3.686, 2.91, 2.425, 1.843, 1.455, 2.619, 2.8129999999999997, 3.007, 2.134, 1.843, 3.686, 3.395, 1.261, 0.097, 0.097, 0.097, 2.037, 2.7159999999999997, 0.6789999999999999, 2.91, 2.7159999999999997, 4.074, 0.291, 0.194, 3.395, 2.328, 2.134, 1.94, 5.141, 3.007, 2.619, 1.94, 2.328, 2.425, 2.425, 1.94, 1.261, 2.231, 3.298, 2.91, 2.425, 1.455, 2.425, 3.492, 2.8129999999999997, 1.455, 2.522, 1.261, 1.164, 1.552, 1.164, 1.455, 0.582, 2.91, 1.455]
对负数开方了呗,打印参与计算的过程值,看看到底是什么
pow((1 - W0/WM), (1 / (1 + B))
很显然这里1 - W0/WM是负数,而1 / (1 + B)小于1
所以W0>WM,B>0
仔细检查是不是这样
根据你提供的代码,我发现以下几个问题可能导致出现错误: