Rivers分享 http://blog.sciencenet.cn/u/sdssxwfd

博文

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

已有 1277 次阅读 2023-5-24 09:54 |系统分类:教学心得

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

方法一:利用全局变量

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

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://blog.sciencenet.cn/blog-216525-1389192.html

上一篇:利用windows的命令合并txt文件
下一篇:得用dos命令统计文件夹中所有文件名
收藏 IP: 58.58.52.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-20 11:16

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部