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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by