How can i vary the value of one parameter and plot them on same graph?
3 次查看(过去 30 天)
显示 更早的评论
Hi there!
function idtry
%
r = 1.3;
tspan = [0:20:40];
%
sol = dde23(@ddems,r,@ddemshist,tspan);
time = sol.x;
SD = sol.y(1,:);
ID = sol.y(2,:);
VD = sol.y(3,:);
hold on
plot(time,SD,'b','LineWidth',2)
plot(time,VD,'g','LineWidth',2)
hold off
plot(time,ID,'--r','LineWidth',2)
xlabel('Time(days)');
ylabel('Infected Dogs Population');
legend('I_d')
grid on
grid minor
function s = ddemshist(t)
% Constant history function for DDEX1.
s = [40 0 0]';
% --------------------------------------------------------------------------
function dydt = ddems(t,y,Z)
Ad = 15; mud = 0.2; k = 2.9; cd = 0.01;
Bd = 0.4; r = 0.2; md = 0.02;
% Differential equations function for DDEX1.
ylag1 = Z(:,1);
dydt = [ Ad-Bd*y(1)*ylag1(1)*exp(-mud*r)-(mud+k)*y(1)+cd*y(3)
Bd*y(1)*ylag1(1)*exp(-mud*r)-(mud+md)*y(2)
k*y(1)-(cd+mud)*y(3)
];
I want to vary the value of k to be [0.8, 1.6, 2.4, 2.9] and plot on the same graph.. how can i do that please?
0 个评论
采纳的回答
Voss
2023-2-17
编辑:Voss
2023-2-18
One way is to make the function ddems nested inside the function idtry and make k a variable in idtry's workspace; then k will be available in ddems's workspace as well because it's a nested function. Then you can loop over the values of k in idtry.
idtry
function idtry
%
rr = 1.3;
tspan = [0:20:40];
%
hold on
k_all = [0.8, 1.6, 2.4, 2.9];
for ii = 1:numel(k_all)
k = k_all(ii);
sol = dde23(@ddems,rr,@ddemshist,tspan);
time = sol.x;
% SD = sol.y(1,:);
ID = sol.y(2,:);
% VD = sol.y(3,:);
% hold on
% plot(time,SD,'b','LineWidth',2) % why plot these at all? they're just going to
% plot(time,VD,'g','LineWidth',2) % be replaced by the next plot() after "hold off"
% hold off
plot(time,ID,'--','LineWidth',2,'DisplayName',sprintf('I_d k=%.1f',k));
end
xlabel('Time(days)');
ylabel('Infected Dogs Population');
% legend('I_d')
legend()
grid on
grid minor
function s = ddemshist(t)
% Constant history function for DDEX1.
s = [40 0 0]';
end
% --------------------------------------------------------------------------
function dydt = ddems(t,y,Z)
Ad = 15; mud = 0.2; %k = 2.9;
cd = 0.01;
Bd = 0.4; r = 0.2; md = 0.02;
% Differential equations function for DDEX1.
ylag1 = Z(:,1);
dydt = [ Ad-Bd*y(1)*ylag1(1)*exp(-mud*r)-(mud+k)*y(1)+cd*y(3)
Bd*y(1)*ylag1(1)*exp(-mud*r)-(mud+md)*y(2)
k*y(1)-(cd+mud)*y(3)
];
end
end
15 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!