%this function uses Euler's method to solve the SIR Model
function [S,A,Y,t] = FermentEulers3(tmax,N,S0,A0,Y0)
%when calling this function, you need to pass the maximum time to run the
%simulation (tmax),
%the number of time steps (N), and the initial 
%populations S0, A0, and Y0 of sugar, alcohol and yeast.
%Important Note: stepsize = tmax/N
dt = tmax/N; %this determines the size of the time step h
S = zeros(1,N+1); %define an empty sugar vector
A = zeros(1,N+1); %define an empty alcohol vector
Y = zeros(1,N+1); %define an empty yeast vector
S(1) = S0; %first value in the Sugar vector is the inital cond.
Y(1) = Y0; %first value in the Yeast vector is the inital cond.
A(1) = A0; %first value in the Alcohol vector is the inital cond.
for n = 1:N %this is the iterative loop for Euler's Method
S(n+1) = S(n) - 0.15*Y(n)*dt;%D.E. for Sugar
A(n+1) = A(n) + 0.05*Y(n)*dt;%D.E. for Alcohol
Y(n+1) = Y(n) + (0.2*Y(n)*(1-(2.5*Y(n)/S(n)))-0.1*A(n)*Y(n))*dt %D.E. for Yeast
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 (Sugar)
h2 = figure;%provide a handle for second figure (Alcohol)
h3 = figure;%provide a handle for third figure (Yeast)
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('Sugar'), xlabel('Hours'), title('Euler''s Method Example')
figure(h2);
plot(t,A)
ylabel('Alcohol'), xlabel('Hours'), title('Euler''s Method Example')
figure(h3)
plot(t,Y)
ylabel('Yeast'), xlabel('Hours'), title('Euler''s Method Example')
%the commands below create a single plot with all three graphs
figure(h4)
plot(t,S, 'blue', t,A, 'red', t,Y,'green')
hold on
title('Plot of Fermentation model behavior')
ylabel('Pounds'), xlabel('Days')
legend('Sugar','Alcohol','Yeast')
