问题
I have the following Lotka-Volterra model
dN1/dt = N1(1-N1-0.7N2)
dN2/dt = N2(1-N2-0.3N1)
where the 1 and 2 next to N are subscripts.
I want to solve this using SciPy and visualize the results. I want to make a plot with N2 on the y axis and N1 on the N1. If you set N1 to zero in the first equation, you get N2 = 1/0.7 and if you set N2 to zero in the second equation, you get N1 = 0.3/1. The two lines are suppose to intersect. How do I do this in Python?
I read this tutorial (slides 6 to 16) online. This is what I have so far.
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
def derivN1(y,t):
yprime=np.array([1-0.7y[0]])
return yprime
def derivN2(y,t):
yprime=np.array([1-0.3y[0]])
return yprime
start=0
end=1
numsteps=1000
time=np.linspace(start,end,numsteps)
y0=np.array([10])
yN1=integrate.odeint(derivN1,y0,time)
yN2=integrate.odeint(derivN2,y0,time)
plt.plot(time,yN1[:])
plt.plot(time,yN2[:])
But the plot isn't correct. UPDATE: I think I used the wrong approach. I'm reading another online tutorial. I'll work through the problem some more. In the meantime, if anyone knows how to solve it let me know.
回答1:
The comment made by @WarrenWeckesser is a very good one, you should start there. I'll merely try to highlight the differences between the implicit plot and the explicit plot.
First, the setup:
import numpy as np
from scipy import integrate
import matplotlib.pyplot as plt
time=np.linspace(0,15,5*1024)
def derivN(N, t):
"""Return the derivative of the vector N, which represents
the tuple (N1, N2). """
N1, N2 = N
return np.array([N1*(1 - N1 - .7*N2), N2*(1 - N2 - .3*N1)])
def coupled(time, init, ax):
"""Visualize the system of coupled equations, by passing a timerange and
initial conditions for the coupled equations.
The initical condition is the value that (N1, N2) will assume at the first
timestep. """
N = integrate.odeint(derivN, init, time)
ax[0].plot(N[:,0], N[:,1], label='[{:.1f}, {:.1f}]'.format(*init)) # plots N2 vs N1, with time as an implicit parameter
l1, = ax[1].plot(time, N[:,0], label='[{:.1f}, {:.1f}]'.format(*init))
ax[1].plot(time, N[:,1], color=l1.get_color())
It is important to realize that your equations are coupled and you should present to odeint
a function that returns the derivative of your coupled equations. Since you have 2 equations, you need to return an array of length 2, each item representing the derivative in terms of the passed in variable (which in this case is the array N(t) = [N1(t), N2(t)]
).
Then you can plot it at all, using different initial conditions for N1 and N2:
fh, ax = plt.subplots(1,2)
coupled(time, [.3, 1/.7], ax)
coupled(time, [.4, 1/.7], ax)
coupled(time, [1/.7, .3], ax)
coupled(time, [.5, .5], ax)
coupled(time, [.1, .1], ax)
ax[0].legend()
ax[1].legend()
ax[0].set_xlabel('N1')
ax[0].set_ylabel('N2')
ax[1].set_xlabel('time')
ax[1].set_ylabel(r'$N_i$')
ax[0].set_title('implicit')
ax[1].set_title('explicit (i.e. vs independant variable time)')
plt.show()

You'll notice that both N1
and N2
evolve to some final value, but that both values are different. The curves in the implicit plot do not intersect for the given equations.
来源:https://stackoverflow.com/questions/28332217/solve-lotka-volterra-model-using-scipy