0
votes

I'm currently going back through old MATLAB university assignments. It's been a while since I used MATLAB, and I'm having trouble figuring out the error with this script that presumably ran OK when I wrote it and submitted it ~4 years ago-- my submission has the correct plots below the code from the output. Maybe MATLAB syntax has changed slightly?

The assignment was running SIR (infectious disease) simulations using the ODE solver. Here's the code below:

%%FUNCTION:
function rhs = SIR_rhs_seasonal(t,u,p,D,G)
%Right-hand side of SIR model
%Assumes 52 weeks in a year

B=((15*(1/7))/3000000)*(1+p*cos(2*pi*t/365));

S = u(1);
E = u(2);
I = u(3);
R = u(4);

rhs = [ -B*S*I; (B*S*I)-(D*E); (D*E)-(G*I); G*I ];

%%ODE SOLVER:
%% parameters
tend = 700;
D=1/7;
G=1/7;
p=.1;
N=3000000

%% initial condition
u = [ N*.99; N*0; N*.01; N*0 ];

%% solve ODE
[T,U] = ode45(@(t,u) SIR_rhs_seasonal(t,u,p,D,G),[0:0.1:tend],u);

%% plot results
figure(1);
plot(T,U(:,1),'b',T,U(:,2),'r',T,U(:,3),'k',T,U(:,4),'g');
axis([0 10 0 3000000]);
xlabel('time = weeks');
ylabel('population');
title('susceptibles (blue), latent (red), infectious (black), recovered/removed (green)');

figure(2);
plot(T,U(:,2)./(U(:,2)+U(:,3)),'b')
axis([0 tend -.25 1]);
xlabel('time = days');
ylabel('E(t)/(E(t)+I(t))');
title('Latent proportion of infectives');

The error currently get is:

SIRmodel2 Not enough input arguments.

Error in SIRmodel2 (line 6) B=((15*(1/7))/3000000)*(1+p*cos(2*pi*t/365));

Any insight is appreciated!

1
Why does your error say pcos(2*pi*t/365)) and not p*cos(2*pi*t/365))? Is this right? - qbzenker
have you redefined cos somehow? - tmpearce
My error says p*cos... this is how it's written in in my text box, but somehow the * isn't showing up in the final post. - Davigor
Fixed (that * needed escaping as \*) - nekomatic
This function takes a bunch of arguments. Are you supplying them when you call the function? - Peter

1 Answers

0
votes

One thing to verify first:

  • The error message indicates you use a function SIRmodel2, however the function printed in your post is called SIR_rhs_seasonal. Is the file named SIRmodel2.m, i.e. is this really the function that is executing?

If this is the function that is executing, then the error is thrown at line 6 since it is the first line some of the input arguments, namely p and t, of the function are used. Likely you called the function without specifying these inputs.

How do you call the function?

You can also verify the situation by putting a break point at line 6 (move cursor to line 6 and hit F12) and running the code. It will stop before executing the line. Now hover with the mouse over the variables p and t. At least one of them will not be defined.