1、函数y1和y2分别测量食饵和捕食者的种群大小。二次交叉项说明了物种之间的相互作用。注意,当没有捕食者时,被捕食者数量增加,但当没有被捕食者时,捕食者数量减少。为了模拟一个系统,创建一个函数,该函数返回给定状态和时间值的状态导数列向量。对于这个示例,我们创建了一个名为LOTKA.M的文件。type lotka
2、模拟0区间上的LOTKA中定义的微分方程% Define initial conditions.t0 = 0;tfinal = 15;y0 = [20 20]';% Simulate the differential equation.tfinal = tfinal*(1+eps);[t,y] = ode23(@lotka,[t0 tfinal],y0);
3、Plot the result of the simulation two different ways.subplot(1,2,1)plot(t,y)title('Time history')subplot(1,2,2)plot(y(:,1),y(:,2))title('Phase plane plot')
4、Now simulate LOTKA using ODE45, instead of ODE23. ODE45 takes longer at each step荑樊综鲶, but also takes larger steps. Nevertheless, the output of ODE45 is smooth because by default the solver uses a continuous extension formula to produce output at 4 equally spaced time points in the span of each step taken. The plot compares this result against the previous.[T,Y] = ode45(@lotka,[t0 tfinal],y0);subplot(1,1,1)title('Phase plane plot')plot(y(:,1),y(:,2),'-',Y(:,1),Y(:,2),'-');legend('ode23','ode45')