How can i vary the value of one parameter and plot them on same graph?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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?
Accepted Answer
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 Comments
Abdullahi
on 17 Feb 2023
Hi there, thank you so much for your help.. why is it that the shape of my plot is getting attached to y-axis? how can i get the plot away from y-axis?
Do you mean, why are the curves nearly vertical near x = 0? All I can say is, because that's what dde23 gave you.
Maybe set the axes XScale to 'log' to see that region better.
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
set(gca(),'XScale','log');
xlabel('Time(days)');
ylabel('Infected Dogs Population');
% legend('I_d')
legend('Location','Northwest')
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
Ok.. that works well, but can we get the curves to rise at 10^-6? so we can study the behaviour of the curves
We can set the x-limits of the axes to start at 1e-6 (see below), but changing the curves themselves requires changing the equations and/or parameters, which would mean you'd be solving a different problem than the problem this code solves.
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
xl = xlim();
set(gca(), ...
'XScale','log', ...
'XLim',[1e-6 xl(2)]);
xlabel('Time(days)');
ylabel('Infected Dogs Population');
% legend('I_d')
legend('Location','Northwest')
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
Abdullahi
on 17 Feb 2023
Ok. Thank you so much
Voss
on 17 Feb 2023
You're welcome!
Abdullahi
on 17 Feb 2023
Hi, i tried to vary a different parameter (ah) this time using same approach as above but it is producing only one plot. what's is the problem?
function ihplot
%
r = 0.4;
tspan = [0:20:40];
%
hold on
ah_all = [0.8, 1.2, 1.6, 2.0];
for ii = 1:numel(ah_all)
ah = ah_all(ii);
sol = dde23(@ddems,r,@ddemshist,tspan);
time = sol.x;
IH = sol.y(2,:);
plot(time,IH,'-','LineWidth',2.5,'DisplayName',sprintf('I_h ah=%.1f',ah));
end
xlabel('Time(days)');
ylabel('Infected Humans Population');
% legend('I_d')
legend('ah=0.8','ah=1.2','ah=1.6','ah=2.0')
grid on
grid minor
function s = ddemshist(t)
% Constant history function for DDEX1.
s = [500 100 0 0 0]';
end
% --------------------------------------------------------------------------
function dydt = ddems(t,y,Z)
Ah = 300; muh = 0.2; mud = 0.03; Kd = 1000;
ah = 0.6; ad = 1; vh = 0.8; k = 0.2;
Bh = 0.4; Bd = 0.5; r = 0.4; dh = 0.4; d = 0.1;
mh = 0.02; rh = 0.2;
% Differential equations function for DDEX1.
ylag1 = Z(:,1);
dydt = [ Ah-Bh*y(1)*ylag1(1)*exp(-mud*r)-(muh+vh)*y(1)+dh*y(3)+d*y(5)
Bh*y(1)*ylag1(1)*exp(-mud*r)-(muh+mh+ah)*y(2)
vh*y(1)-(dh+muh)*y(3)
ah*y(2)-(muh+rh)*y(4)
rh*y(4)-(muh+d)*y(5)
];
end
end
Voss
on 17 Feb 2023
'ah' is still being set in ddems(). You have to remove that in order for the 'ah' in ihplot() to be used. (Notice that I commented out the line setting 'k' in ddems() in my answer - same thing.)
Abdullahi
on 18 Feb 2023
work great.. Thank you so much
Voss
on 18 Feb 2023
You're welcome!
Abdullahi
on 18 Feb 2023
can i vary the lags (r) in the same way?
Voss
on 18 Feb 2023
Yes, but since there is a variable 'r' in both the function idtry and the function ddems, one of them has to be changed to something else. Actually, I should've changed one of them when I made ddems a nested function, but I didn't notice it until now. Now I have edited the code in my answer and comments so that one of the r's is called rr. Notice the curves have changed slightly.
Once you have changed one of the 'r's to something else, you can loop over r values like you've done before with k and ah. However, notice in the case of r (what I call rr in my updated code), it is used only as an input to dde23 and is not used in ddems, unlike k and ah.
Abdullahi
on 18 Feb 2023
the two r's are same, i renamed the lag (i.e. tau) in the original equations to be r.. (see equations below).
If the two r's are the same, then remove the one defined in ddems and just use the one defined in idtry/ihplot. Loop over r values in idtry/ihplot, like you did with k and ah.
Abdullahi
on 18 Feb 2023
ok.. let me try that
More Answers (0)
Categories
Find more on Profile and Improve Performance in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)