多语言展示
当前在线:1175今日阅读:150今日分享:23

matlab中的微分方程的数值积分

这个例子展示了如何在一个简单的微分方程上使用ODe23和ODe45。常微分方程的数值解的函数是ODe23和ODe45。他们采用可变步长的龙格-库塔积分方法。ODe23使用简单的2阶和3阶的中精度公式,ODe45使用4阶和5阶的对来获得更高的精度。考虑一对一阶常微分方程称为洛特卡-沃尔泰拉捕食者-食饵模型。 y1' = (1 - alpha*y2)*y1 y2' = (-1 + beta*y1)*y2
工具/原料
1

matlab软件

2

电脑

方法/步骤
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')

推荐信息