如果条件在时间不返回正确的值
If condition at time not returning correct value
我在 Python 工作来回答这个问题:一群外星人是否应该带来 500 万人口和 100 万资源负载 vs 100 万人口和 500 万负载...我试图找出 200 年后这两个选项中的哪一个会使新星球上的人口最大化。这是我的代码:
这是我的微分函数
def derivs3(y1, t):
c = P0 + R0
r = a / c
q = (a + b) / c
Pi = y1[0]
Ri = y1[1]
Wi = y1[2]
# the model equations
dPdt = q * Pi*Ri/(1+Wi)
dRdt = - q * Pi*Ri/(1+Wi) + (a / q) * Wi / (t + .0001)
dWdt = b
return [dPdt, dRdt, dWdt]
在这里,我定义我的参数:
# model parameters
a = 0.02 # related to conversion of unallocated resources into population
b = 0.0001 # related to growth of knowledge
W0 = 0.0 # initial amount of knowledge
# time period
Tmax = 600 # years
这里是我 运行 odeint 和绘制结果的地方:
# Put your code here
t = np.arange(0, Tmax, 0.1)
P0 = 5
R0 = 1
y0 = [P0,R0,W0]
soln = odeint(derivs3, y0, t)
PSol = soln[:, 0]
RSol = soln[:, 1]
WSol = soln[:, 2]
P0 = 1
R0 = 5
y0 = [P0,R0,W0]
soln = odeint(derivs3, y0, t)
PSol2 = soln[:, 0]
RSol2 = soln[:, 1]
WSol2 = soln[:, 2]
plt.plot(t,PSol)
plt.plot(t,PSol2)
plt.legend(("5Bil Aliens, 1Bil Resources","1Bil Aliens, 5Bil Resources"), loc='upper left', prop={'size':15}, bbox_to_anchor=(1,1))
plt.grid()
plt.xlabel("time (years)")
plt.ylabel("Population (billions)")
plt.title("Populations vs. Time")
这里是出现问题的地方:
if PSol[200] > PSol2[200]:
print("To maximize population after 200 years (for a total of", round(PSol[200],2),"billion aliens), the aliens should take a population of 5 Billion Aliens, and a load of 1 Billion Resources.")
elif PSol[200] < PSol2[200]:
print("To maximize population after 200 years (for a total of", round(PSol2[200],2),"billion aliens), the aliens should take a population of 1 Billion Aliens, and a load of 5 Billion Resources.")
else:
print("The population after 200 years will be the same (for a total of", round(PSol2[200],2),"billion aliens), whether the aliens take a population of 5 Billion Aliens and a load of 1 Billion Resources, or a population of 1 Billion Aliens and a load of 5 Billion Resources")
所以它 return 正在打印以下打印语句,这些语句与我得到的图表不一致。这可能是索引的问题,但我使用 PSol[200] 和 PSol2[200] 因为我想知道如果他们想在 200 年后人口最大化,他们应该带来多少外星人和资源。 见下文(忽略约600年的线,因为我没有调整它们,知道它们会return同样的问题):
这是图表。我知道是对的(求助室),所以一定是索引值偏了。
我没有看到 t
在您的代码中定义的位置,但问题似乎是由 t[200] != 200
引起的(如您所建议的)。只需添加 print t[200]
行即可相对容易地检查。
如果确实如此,您需要确定 t==200
的索引或进行插值。我倾向于使用 numpy.interp 进行插值,因为这将允许您调查不是时间步长整数倍的时间。
PSol_200 = np.interp(200, t, PSol)
PSol2_200 = np.interp(200, t, PSol2)
编辑:
通过您最近的编辑,我们可以看到您的时间步长是 0.1 而不是 1,因此 t==200
应该出现在索引 2000 而不是 200。您比较的是 20 年后而不是 200 年后的人口。
我在 Python 工作来回答这个问题:一群外星人是否应该带来 500 万人口和 100 万资源负载 vs 100 万人口和 500 万负载...我试图找出 200 年后这两个选项中的哪一个会使新星球上的人口最大化。这是我的代码:
这是我的微分函数
def derivs3(y1, t):
c = P0 + R0
r = a / c
q = (a + b) / c
Pi = y1[0]
Ri = y1[1]
Wi = y1[2]
# the model equations
dPdt = q * Pi*Ri/(1+Wi)
dRdt = - q * Pi*Ri/(1+Wi) + (a / q) * Wi / (t + .0001)
dWdt = b
return [dPdt, dRdt, dWdt]
在这里,我定义我的参数:
# model parameters
a = 0.02 # related to conversion of unallocated resources into population
b = 0.0001 # related to growth of knowledge
W0 = 0.0 # initial amount of knowledge
# time period
Tmax = 600 # years
这里是我 运行 odeint 和绘制结果的地方:
# Put your code here
t = np.arange(0, Tmax, 0.1)
P0 = 5
R0 = 1
y0 = [P0,R0,W0]
soln = odeint(derivs3, y0, t)
PSol = soln[:, 0]
RSol = soln[:, 1]
WSol = soln[:, 2]
P0 = 1
R0 = 5
y0 = [P0,R0,W0]
soln = odeint(derivs3, y0, t)
PSol2 = soln[:, 0]
RSol2 = soln[:, 1]
WSol2 = soln[:, 2]
plt.plot(t,PSol)
plt.plot(t,PSol2)
plt.legend(("5Bil Aliens, 1Bil Resources","1Bil Aliens, 5Bil Resources"), loc='upper left', prop={'size':15}, bbox_to_anchor=(1,1))
plt.grid()
plt.xlabel("time (years)")
plt.ylabel("Population (billions)")
plt.title("Populations vs. Time")
这里是出现问题的地方:
if PSol[200] > PSol2[200]:
print("To maximize population after 200 years (for a total of", round(PSol[200],2),"billion aliens), the aliens should take a population of 5 Billion Aliens, and a load of 1 Billion Resources.")
elif PSol[200] < PSol2[200]:
print("To maximize population after 200 years (for a total of", round(PSol2[200],2),"billion aliens), the aliens should take a population of 1 Billion Aliens, and a load of 5 Billion Resources.")
else:
print("The population after 200 years will be the same (for a total of", round(PSol2[200],2),"billion aliens), whether the aliens take a population of 5 Billion Aliens and a load of 1 Billion Resources, or a population of 1 Billion Aliens and a load of 5 Billion Resources")
所以它 return 正在打印以下打印语句,这些语句与我得到的图表不一致。这可能是索引的问题,但我使用 PSol[200] 和 PSol2[200] 因为我想知道如果他们想在 200 年后人口最大化,他们应该带来多少外星人和资源。 见下文(忽略约600年的线,因为我没有调整它们,知道它们会return同样的问题):
这是图表。我知道是对的(求助室),所以一定是索引值偏了。
我没有看到 t
在您的代码中定义的位置,但问题似乎是由 t[200] != 200
引起的(如您所建议的)。只需添加 print t[200]
行即可相对容易地检查。
如果确实如此,您需要确定 t==200
的索引或进行插值。我倾向于使用 numpy.interp 进行插值,因为这将允许您调查不是时间步长整数倍的时间。
PSol_200 = np.interp(200, t, PSol)
PSol2_200 = np.interp(200, t, PSol2)
编辑:
通过您最近的编辑,我们可以看到您的时间步长是 0.1 而不是 1,因此 t==200
应该出现在索引 2000 而不是 200。您比较的是 20 年后而不是 200 年后的人口。