徐伟
向微分方程中传递额外的参数方法
2023-5-24 09:54
阅读:1237

向微分方程中传递额外的参数方法:

方法一:利用全局变量

利用全局变量对其中的参数赋值,实现对函数中某些参数的控制。

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


转载本文请联系原作者获取授权,同时请注明本文来自徐伟科学网博客。

链接地址:https://wap.sciencenet.cn/blog-216525-1389192.html?mobile=1

收藏

分享到:

当前推荐数:0
推荐到博客首页
网友评论0 条评论
确定删除指定的回复吗?
确定删除本博文吗?