使用SciPy解决Lotka

使用SciPy解决Lotka

本文介绍了使用SciPy解决Lotka-Volterra模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Lotka-Volterra模型

I have the following Lotka-Volterra model

dN1/dt = N1(1-N1-0.7N2)

dN1/dt = N1(1-N1-0.7N2)

dN2/dt = N2(1-N2-0.3N1)

dN2/dt = N2(1-N2-0.3N1)

其中N旁边的1和2是下标.

where the 1 and 2 next to N are subscripts.

我想使用SciPy解决此问题并可视化结果.我想用y轴上的N2和N1上的N1进行绘图.如果在第一个方程式中将N1设置为零,则得到N2 = 1/0.7,如果在第二个方程式中将N2设置为零,则得到N1 = 0.3/1.假设这两条线相交.如何在Python中执行此操作?

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?

我阅读了此教程(幻灯片6至16)在线.这就是我到目前为止所拥有的.

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.

推荐答案

@WarrenWeckesser的评论非常好,您应该从这里开始.我将仅尝试突出显示隐式图和显式图之间的差异.

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.

首先,设置:

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

重要的是要认识到方程是耦合的,应该向odeint提供一个函数,该函数返回耦合方程的导数.由于您有2个方程式,因此需要返回一个长度为2的数组,每个项代表传入变量的导数(在本例中为数组N(t) = [N1(t), N2(t)]).

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)]).

然后,您可以对N1和N2使用不同的初始条件来绘制它:

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

您会注意到,N1N2都演变为某个最终值,但是两个值是不同的.对于给定的方程,隐式图中的曲线不相交.

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.

这篇关于使用SciPy解决Lotka-Volterra模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:17
查看更多