%this function uses Euler's method to solve the SIR Model
function [S,I,R,t] = SIREulers(tmax,N,a,b,S0,I0,R0)
%when calling this function, you need to pass the maximum time to run the
%simulation (tmax),
%the number of time steps (N), the parameters a and b, and the initial 
%populations S0,I0, and R0.
%Important Note: stepsize = tmax/N
%To run this program for the first time (in mini-project 1), you can simply
%cut and paste the following into the MATLAB Command Window
%SIREulers(24,120,.00001,1/14,45400,2100,2500);
dt = tmax/N; %this determines the size of the time step h
S = zeros(1,N+1); %define an empty susceptible vector
I = zeros(1,N+1); %define an empty infectives vector
R = zeros(1,N+1); %define an empty recovered vector
S(1) = S0; %first value in the Susceptible vector is the inital cond.
I(1) = I0; %first value in the Infectives vector is the inital cond.
R(1) = R0; %first value in the Recovered vector is the inital cond.
for n = 1:N %this is the iterative loop for Euler's Method
    S(n+1) = S(n) +(.1*R(n) - a*S(n)*I(n))*dt;%D.E. for Susceptibles
    I(n+1) = I(n) + (a*S(n)*I(n) - b*I(n))*dt;%D.E. for Infectives
    R(n+1) = R(n) + (b*I(n)-.1*R(n))*dt;%D.E. for Recovered
end %end the for loop
t = linspace(0,tmax,N+1); %this is used only for the plotting
h1 = figure;%provide a handle for first figure (Susceptibles)
h2 = figure;%provide a handle for second figure (Infectives)
h3 = figure;%provide a handle for third figure (Recovered)
h4 = figure;%provides a handle for a plot containing all three
%the code below is strictly used for plotting
figure(h1);
plot(t,S)
ylabel('Susceptibles'), xlabel('Days'), title('Euler''s Method Example')
figure(h2);
plot(t,I)
ylabel('Infectives'), xlabel('Days'), title('Euler''s Method Example')
figure(h3)
plot(t,R)
ylabel('Recovered'), xlabel('Days'), title('Euler''s Method Example')
%the commands below create a single plot with all three graphs
figure(h4)
plot(t,S, 'blue', t,I, 'red', t,R,'green')
hold on
title('Plot of SIR model behavior')
ylabel('Individuals'), xlabel('Days')
legend('Susceptibles','Infected','Recovered')
