MATLAB Answers

Help with my SIR MODEL

9 views (last 30 days)
Darren Tharby
Darren Tharby on 12 May 2021
Answered: William Rose on 12 May 2021
function sir_model
% Model parameters
beta = 5*10^-9; % rate of infection
gamma = 0.12; % rate of recovery (try also 0.07)
delta = 0.0; % rate of immunity loss
N = 6*10^7; % Total population N = S + I + R
I0 = 10; % initial number of infected
T = 300; % period of 300 days
dt = 1/4; % time interval of 6 hours (1/4 of a day)
fprintf('Value of parameter R0 is %.2f',N*beta/gamma)
% Calculate the model
[S,I,R] = sir_model(beta,gamma,delta,N,I0,T,dt);
% Plots that display the epidemic outbreak
tt = 0:dt:T-dt;
% Curve
plot(tt,S,'b',tt,I,'r',tt,R,'g','LineWidth',2); grid on;
xlabel('Days'); ylabel('Number of individuals');
This is my current code however whenever i run it
Error in SIR (line 13)
[S,I,R] = sir_model(beta,gamma,delta,N,I0,T,dt);
The above error occurs please help

Answers (3)

Steven Lord
Steven Lord on 12 May 2021
Your function is defined to accept zero input arguments and return zero output arguments.
function sir_model
You can't call it with more than zero input arguments and you can't call it with more than zero output arguments.
[S,I,R] = sir_model(beta,gamma,delta,N,I0,T,dt);

William Rose
William Rose on 12 May 2021
Edited: William Rose on 12 May 2021
You get an error at lline 13 because line 13 (which is in function sir_model()) calls itself. That is not OK.
Are you aware that there is a function called
in the Matlab File Exchange? It is here. Did you mean to call that function? It is not built-in to Matlab. You may call it from inside your function, if you download it, and subject to whatever usage condtions have been placed on it by the person who posted it to the File Exchange.
Also, your function declaration (line 1) does not follow the normal standard. See Matlab documentation on how to write a function. If your function has no argumnts then include empty parentheses. In this case I believe you want the funciton to return arrays S, I, R, so you funciton delaration (line 1) would be
function [S,I,R] = sir_model()
It is good practice to end a function with
Good luck.

William Rose
William Rose on 12 May 2021
I think you did intend to call sirs_model(), from the File Exchange, since the seven variables which you pass are identical. You spelled it wrong by leaving out the second s. You must download the file from the File Exchange. It should be in the same directory as your function, or at least in a directory that is on the Matlab path.
if your function has no outputs then you do not have to declare any in line 1. But the documentation and online discussion says you should include parentheses after the function name in line 1:
function sir_model()
[S,I,R] = sirs_model(beta,gamma,delta,N,I0,T,dt)
Good luck.

Community Treasure Hunt

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

Start Hunting!