vendredi 22 février 2019

If condition at time not returning correct value

I'm working in Python to answer this question about whether a group of aliens should bring a population of 5 million and a resource load of 1 million vs population of 1M and load of 5M... So I have this graph that shows the answer (I know the graph is right)

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]

a = 0.02 
b = 0.0001
W0 = 0.0

# time period
Tmax = 600 # years

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 Aliences, 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")

And here is where the issue is arising:

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")

So it's returning the following print statements, which don't align with the graph that I'm getting. It's probably a problem with indexing, but I'm using 200 because I want to know how many aliens and resources they should bring if they want to maximize population after 200 years. See below (ignore the line about 600 years, because I haven't adjusted them, knowing they'll return the same problems):

1 Here

Aucun commentaire:

Enregistrer un commentaire