MATLAB Answers

SA
0

Using dde23 with events option

Asked by SA
on 15 Apr 2019
Latest activity Commented on by SA
on 16 Apr 2019
Can someone kindly check this code for me?
I can't seem to find my error. I can't plot the graph because my dimensions are unequal.
%Solving and plotting Model 3
xfinal = 60; %will vary
xRange = [0,xfinal];
y0=[0.005; 1-0.005; 0];%the initial conditions for I,S and R respectively
%I=y(1);
%S=y(2);
%R=y(3);
options = ddeset('Events',@myEventsFcn);
sol = dde23(@challenge213,4,@ddex1hist,xRange,options)
plot(sol.x,sol.y(:,1),'k')
hold on
plot(sol.x,sol.y(:,2),'b')
hold on
plot(sol.x,sol.y(:,3),'r')
xlabel('Days')
ylabel('Population')
xlim([-inf xfinal])
ylim([0 inf])
legend('Infected', 'Susceptible', 'Recovered')
title('Solution to Ordinary Differential Equation Model 3')
function h = ddex1hist(x)
h = [0;1;0];
end
function dydx = challenge213(x,y,Z)
k = 4;
m = 0.8;
ylag1 = Z(:,1);
%ylag2 = Z(:,2);
dydx = [m*y(1)*y(2) - m*ylag1(1)*ylag1(2); -m*y(1)*y(2); m*ylag1(1)*ylag1(2)];
end
function [position,isterminal,direction] = myEventsFcn(x,y,Z)
position = [y(1)-(10^-5); y(2)-(10^-5); y(3)]; % the function I is unknown
isterminal = [1; 1; 1]; %terminate when I(t) drops below 10^-5
direction = [0; 0; 0];
end
thank you

  0 Comments

Sign in to comment.

1 Answer

Answer by Torsten
on 15 Apr 2019
 Accepted Answer

plot(sol.x,sol.y(1,:),'k')
hold on
plot(sol.x,sol.y(2,:),'b')
hold on
plot(sol.x,sol.y(3,:),'r')

  1 Comment

It worked. Thank you Torsten.

Sign in to comment.