function sirnonlinear1
%SIRNONLINEAR1: MATLAB function M-file that takes an initial 
%approximation of parameter values and carries out nonlinear
%regression to obtain best-fit parameter values for the SIR
%system and the British Medical Journal influenza data.
global days S I w1 w2;
S=[762 740 650 400 250 120 80 50 20 18 15 13 10]; 
I=[1 20 80 220 300 260 240 190 120 80 20 5 2]; 
days=[0 3 4 5 6 7 8 9 10 11 12 13 14]; 
%Weights
w1 = std(S(2:end));
w2 = std(I(2:end));
guess = [.0036 .9395];
[p,error]=fminsearch(@sirerr, guess);
a = p(1)
b = p(2)
q = 2*(length(S)-1)-length(p);
s = sqrt(error/q) 
s1 = w1*s
s2 = w2*s
%
[t,y]=ode45(@sirpe,[0,14],[S(1); I(1)],[],p);
subplot(2,1,1)
plot(t,y(:,1),days,S,'o')
title('Susceptible population, model and data','Interpreter','latex','FontSize',16)
subplot(2,1,2)
plot(t,y(:,2),days,I,'o')
title('Infected population, model and data','Interpreter','latex','FontSize',16)
xlabel('Days','Interpreter','latex','Fontsize',14)
%
function error = sirerr(p)
%LVERR: Function defining error function for 
%example with SIR equations.
global days S I w1 w2;
[t,y] = ode45(@sirpe,days,[S(1);I(1)],[],p);    %Notice that we pass
                                                %a parameter vector
error = norm(y(:,1)-S')^2/w1^2+norm(y(:,2)-I')^2/w2^2;
%
function value = sirpe(t,y,p)
%LVPE: ODE for example SIR paramter
%estimation example. p(1)=a, p(2) = b.
value=[-p(1)*y(1)*y(2);p(1)*y(1)*y(2)-p(2)*y(2)];