|
向微分方程中传递额外的参数方法:
方法一:利用全局变量
利用全局变量对其中的参数赋值,实现对函数中某些参数的控制。
clear;clc;close all
global e
e=0;
[t1,x1]=ode45(@fun0412e,[0,20],[25;2]);
e=0.3;
[t2,x2]=ode45(@fun0412e,[0,20],[25;2]);
e=0.1;
[t3,x3]=ode45(@fun0412e,[0,20],[25;2]);
plot(t1,x1,t2,x2,t3,x3)
figure
plot(x1(:,1),x1(:,2),x2(:,1),x2(:,2),x3(:,1),x3(:,2))
legend('e=0','e=0.3','e=0.1')
figure
plot(t1,x1(:,2)./(x1(:,1)+x1(:,2)),t2,x2(:,2)./(x2(:,1)+x2(:,2)),...
'*',t3,x3(:,2)./(x3(:,1)+x3(:,2)),'.')
legend(‘没有捕捞时’,‘战前’,‘战中')
function f=fun0412e(t,x)
global e
r1=1;la1=0.1;r2=0.5;la2=0.02;
f=[x(1)*(r1-e-la1*x(2));
x(2)*(-r2-e+la2*x(1))];
end
方法二利用匿名函数
通过在函数外部定义参数并在指定函数句柄时传递这些参数,可以传入额外参数。
clear;clc;close all tspan=[0,20];x0=[25;2]; e=0; [t1,x1]=ode45(@(t,x)fun0412ep(t,x,e),tspan,x0); e=0.3; [t2,x2]=ode45(@(t,x)fun0412ep(t,x,e),tspan,x0); e=0.1; [t3,x3]=ode45(@(t,x)fun0412ep(t,x,e),tspan,x0); plot(t1,x1,t2,x2,t3,x3) figure plot(x1(:,1),x1(:,2),x2(:,1),x2(:,2),x3(:,1),x3(:,2)) legend('e=0','e=0.3','e=0.1') figure plot(t1,x1(:,2)./(x1(:,1)+x1(:,2)),t2,x2(:,2)./(x2(:,1)+x2(:,2)),... '*',t3,x3(:,2)./(x3(:,1)+x3(:,2)),'.') legend('没有捕捞时','战前','战中') function f=fun0412ep(t,x,e) r1=1;la1=0.1;r2=0.5;la2=0.02; f=[x(1)*(r1-e-la1*x(2)); x(2)*(-r2-e+la2*x(1))]; end
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-22 17:09
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社