Using dde23 with events option

조회 수: 2 (최근 30일)
SA
SA 2019년 4월 15일
댓글: SA 2019년 4월 16일
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

채택된 답변

Torsten
Torsten 2019년 4월 15일
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
SA
SA 2019년 4월 16일
It worked. Thank you Torsten.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Delay Differential Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by