function [Year, ActNumber, PasNumber, ActualNumber, R_0sq] = WarwickHATmodel(Year,NumberPeopleScreened,meff,epsilon,p_V,r,Ratio1,Ratio2,Ratio3,Ratio4,k_A,f_A)

% It is an ODE, SEIIR HAT model simulator developed by Kat S Rock and Matt
% J Keeling, Warwick University at part of the NTD Modelling Consortium
% Project.

% This programme takes the intervention years and number of people screened from data to
% compute the disease dynamics and expected number of reported active and 
% passive incidence based on the proportion of the population screened and
% the diagnostic test accuracy. 

% There are (assumed to be) up to 10 unknown parameters: meff, p_V, epsilon, 
% r, Ratio1, Raito2, Ratio3, Ratio4, k_A and f_A 

% This code outputs: years, corresponding numbers of actively detected, passively detected cases
% and new infections numbers and the full cycle basic reproductive number
% pre-active control

N_H0=291567;                                                        %Average size of two health zones in Bandundu province in 2009/2012 ACF International

% Sets up default parameters if necessary.
% These parameters are for Model 7, described in the paper.
% All rates are in days.
if nargin == 0
   
   meff=6.7;        %Ratio of vectors to humans x probability of transmission from tsetse to hosts
   epsilon = 0.05;  %Reduced susceptibility of non-teneral vectors
   p_V=0.065;       %Probability of transmission from host to tsetse
   r=5.9;            %Relative risk for "high-risk humans
   Ratio1=0.91;     %Proportion of low-risk, randomly participating humans
   Ratio2=0;        %Proportion of high-risk, randomly participating humans
   Ratio3=0;        %Proportion of low-risk, non-participating humans
   Ratio4=0.09;     %Proportion of high-risk, non-participating humans
   k_A=4.7;         %Ratio of reservoir animals to humans
   f_A=0.04;        %Proportion of blood-meals taken on reservoir animals     
   
   Year=2000:1:2012;                %Active screening years
   ApproxPercentageScreened=[31    20    17    26    24    28    30    39    37    53    41    15    26];
   NumberPeopleScreened=ApproxPercentageScreened*N_H0/100; %Active screening levels
  
end


%Fixed parameters

%Hosts are  (1) low-risk, random participants
%           (2) high-risk, random participants
%           (3) low-risk, non-participants
%           (4) high-risk, non-participants
%           (5) animals (non-reservoir)

N_A=N_H0*k_A;
N=N_H0*[Ratio1 Ratio2 Ratio3 Ratio4 k_A 1];
relprob=1;                                                          %p_A/p_H 

b_H = [5.4795e-5  5.4795e-5 5.4795e-5 5.4795e-5 0.0014 0.0027];     %Birth rate of hosts
mu_H = [5.4795e-5  5.4795e-5 5.4795e-5 5.4795e-5 0.0014 0.0027];    %Natural death rate of hosts
sigma_H = [0.0833  0.0833 0.0833 0.0833 0.0833 0];                  %E -> I1 rate of becoming infectious from infected of hosts (latency period)
phi_H = [0.0019 0.0019 0.0019 0.0019 0 0];                          %Inverse Stage I duration, I1 -> I2 
gamma_H = [0.006 0.006 0.006 0.006 0 0];                            %Passive Treatment rate of hosts I2->R
eta_H = [0 0 0 0 0 0];                                              %I1 -> R
delta_H = [0 0 0 0 0 0];                                            %disease death rate of hosts
omega_H = [0.006 0.006 0.006 0.006 0 0];                            %Rate of full recovery R -> S
p_H=[meff meff meff meff meff 0];                                   %Ratio of vectors to humans x probability of transmission from tsetse to hosts
u=0.26;                                                           %proportion of passive cases reported

%Vector 
b_V = 0.03;                   %Birth rate of vectors
mu_V = 0.03;                  %Death rate of vectors
alpha = 0.333;                %Total bite rate
sigma_V = 0.034;              %Inverse of extrinsic incubation period
f_H =0.09;                    %Percent blood-meals on humans
Phigh=Ratio2+Ratio4;          %Proportion of high-risk humans
s_A=f_A*((Ratio1+Ratio3)+r*(Ratio2+Ratio4))*(1+(1-f_A-f_H)/(f_A+f_H))/(k_A*(1-f_A)*(1-f_A*(1-f_A-f_H)/((1-f_A)*(f_A+f_H)))); %Innate biting preference on reservoir animals
s_N=(1-f_A-f_H)*((Ratio1+Ratio3)+r*(Ratio2+Ratio4)+k_A*s_A)/(f_A+f_H); %Innate biting preference on non-reservoir animals
s=[1 r 1 r s_A s_N];          %Inate biting preference on hosts  
k=N./N(1);                    %Relative host population sizes
f=(s.*k)/sum(s.*k);           %Proportion of feeding on hosts


%Compute R_0 using NGM approach
T=zeros(12,12);
S=zeros(12,12);
S_Vstar=mu_V*N_H0/(alpha+mu_V);     %equilibrium S_V 
G_Vfrom0=alpha*N_H0/(alpha+mu_V);   %equilibrium (no infection) G_V

%Matrix T is transmissions
T(1,12)=alpha*meff*(f(1)+f(3));                                             %I_V infects E_Hlow
T(4,12)=alpha*meff*(f(2)+f(4));                                             %I_V infects E_Hhigh
T(7,12)=alpha*p_H(5)*f(5);                                                  %I_V infects E_A
T(9,2)=alpha*p_V*(f(1)+f(3))*(S_Vstar+epsilon*G_Vfrom0)/((1-Phigh)*N_H0);   %I_1Hlow infects E_1V
T(9,3)=T(9,2);                                                              %I_2Hlow infects E_1V
    if Phigh~=0
    T(9,5)=alpha*p_V*(f(2)+f(4))*(S_Vstar+epsilon*G_Vfrom0)/(Phigh*N_H0);   %I_1Hhigh infects E_1V
    T(9,6)=T(9,5);                                                          %I_2Hhigh infects E_1V
    end
    if N_A~=0
        T(9,8)=alpha*p_V*f(5)*(S_Vstar+epsilon*G_Vfrom0)/N_A;
    end
%Matrix S is transissions
S(1,1)=-sigma_H(1)-mu_H(1);
S(2,2)=-phi_H(1)-mu_H(1);
S(3,3)=-gamma_H(1)-mu_H(1);
S(4,4)=-sigma_H(1)-mu_H(1);
S(5,5)=-phi_H(1)-mu_H(1);
S(6,6)=-gamma_H(1)-mu_H(1);
S(7,7)=-sigma_H(5)-mu_H(5);
S(8,8)=-mu_H(5);
S(9,9)=-3*sigma_V-mu_V;
S(10,10)=-3*sigma_V-mu_V;
S(11,11)=-3*sigma_V-mu_V;
S(12,12)=-mu_V;
S(2,1)=sigma_H(1);
S(3,2)=phi_H(1);
S(5,4)=sigma_H(1);
S(6,5)=phi_H(1);
S(8,7)=sigma_H(5);
S(10,9)=3*sigma_V;
S(11,10)=3*sigma_V;
S(12,11)=3*sigma_V;

K=-T*inv(S);
R_0=max(abs(eig(K)));
R_0sq=R_0^2

%Compute equilibrium prevalence
ff=[f(1)+f(3) 0 0 f(2)+f(4) f(5)];
NN=[N(1)+N(3) 0 0 N(2)+N(4) N(5)];

A=omega_H(1)*gamma_H(1)*phi_H(1)*sigma_H(1)/(mu_H(1)*(omega_H(1)+mu_H(1))*(gamma_H(1)+mu_H(1))*(phi_H(1)+mu_H(1))) - sigma_H(1)/mu_H(1) - 1;
B=27*sigma_V^3/(mu_V*(3*sigma_V+mu_V)^2);
C=(sigma_H(1)/(phi_H(1)+mu_H(1)))*(1+(phi_H(1)/(gamma_H(1)+mu_H(1))));
D=sigma_H(1)+mu_H(1);
E=3*sigma_V+mu_V;
F=alpha*p_V*epsilon*(1+3*sigma_V/mu_V)*C;
G=alpha*p_V*N_H0*C*(mu_V+epsilon*alpha)/(alpha+mu_V);
H=(1+sigma_H(5)/mu_H(5));
J=sigma_H(5)+mu_H(5);
K=alpha*meff*B*H;
L=alpha*p_V*epsilon*(1+3*sigma_V/mu_V)*sigma_H(5)/mu_H(5);
M=alpha*p_V*N_H0*sigma_H(5)*(mu_V+epsilon*alpha)/((alpha+mu_V)*mu_H(5));

a=alpha*meff*B;
b=alpha*meff*A*B;

%Coefficients of cubic in E_V
for i=[1 4 5]
    if NN(4)==0
        NN(4)=1;
    end
end 

Z_1=b*ff(1)*ff(4)*ff(5)*relprob*(-E*K*b  +F*a*K*(ff(1)+ff(4)) - L*a*b*ff(5))/(NN(1)*NN(4)*NN(5));

Z_2=J*b*ff(1)*ff(4)*(-E*b + F*a*(ff(1)+ff(4)))/(NN(1)*NN(4)) + ...
    ff(5)*relprob*( D*b*(ff(1)/NN(1) + ff(4)/NN(4))*(E*K + a*L*ff(5)) - a*F*D*K*(ff(1)^2/NN(1) + ff(4)^2/NN(4)))/NN(5)...
    + a*b*ff(1)*ff(4)*ff(5)*relprob*(b*M*ff(5) - G*K*(ff(1)+ff(4)))/(NN(1)*NN(4)*NN(5));

Z_3=ff(5)*relprob*D^2*(-E*K-a*L*ff(5))/NN(5) + D*b*(ff(1)/NN(1)+ff(4)/NN(4))*(E*J - M*a*ff(5)^2*relprob/NN(5))...
     + D*a*(ff(1)^2/NN(1) + ff(4)^2/NN(4))*(-F*J + G*K*ff(5)*relprob/NN(5)) - G*J*b*a*ff(4)*ff(1)*(ff(1)+ff(4))/(NN(1)*NN(4));

Z_4= -E*J*D^2 +a*G*D*J*(ff(1)^2/NN(1)+ff(4)^2/NN(4)) + a*M*D^2*relprob*ff(5)^2/NN(5);



E_1Vstar=max(roots([Z_1 Z_2 Z_3 Z_4]));
if E_1Vstar<0
    E_1Vstar=0;
end
E_Hstar_comp=a*ff.*E_1Vstar./(D-b*ff.*E_1Vstar./NN);

%Splits low risk/high risk groups by participation
E_Hstar(1)=Ratio1/(Ratio1+Ratio3)*E_Hstar_comp(1);
E_Hstar(3)=Ratio3/(Ratio1+Ratio3)*E_Hstar_comp(1);
E_Hstar(2)=Ratio2/(Ratio2+Ratio4)*E_Hstar_comp(4);
E_Hstar(4)=Ratio4/(Ratio2+Ratio4)*E_Hstar_comp(4);
E_Hstar(5)=a*relprob*f(5)*E_1Vstar/(J+K*relprob*f(5)*E_1Vstar/N(5));
E_Hstar(6)=0;
E_Hstar(isnan(E_Hstar))=0;

%Computes other equilibria from E's
S_Hstar=N+A*E_Hstar;
I_1Hstar=(sigma_H(1)/(phi_H(1)+mu_H(1)))*E_Hstar;
I_2Hstar=(phi_H(1)/(gamma_H(1)+mu_H(1)))*I_1Hstar;
R_Hstar=(gamma_H(1)/(omega_H(1)+mu_H(1)))*I_2Hstar;

S_Hstar(5)=N(5)-H*E_Hstar(5);
I_1Hstar(5)=sigma_H(5)/mu_H(5)*E_Hstar(5);
I_2Hstar(5)=0;
R_Hstar(5)=0;

E_2Vstar=(3*sigma_V/(3*sigma_V+mu_V))*E_1Vstar;
E_3Vstar=(3*sigma_V/(3*sigma_V+mu_V))*E_2Vstar;
I_Vstar=(3*sigma_V/mu_V)*E_3Vstar;
G_Vstar=alpha*N_H0/(alpha+mu_V)-((3*sigma_V+mu_V)/mu_V)*E_1Vstar;

%ICs (N.B. these are now effective population sizes for vectors)
S_H0 = S_Hstar;     %Suspectible hosts (N.B. none of these should be zero otherwise there are problems)
E_H0 = E_Hstar;     %Exposed hosts
I_1H0 = I_1Hstar;   %Infecious hosts (stage I)
I_2H0 = I_2Hstar;   %Infecious hosts (stage II)
R_H0 = R_Hstar;     %Hospitalised/resting hosts
x=length(S_H0);     %Number of host types

   
S_V0 = S_Vstar;     %Fully Suspectible vectors
G_V0 = G_Vstar;     %Non-teneral susceptibles
E_1V0 = E_1Vstar;   %Exposed vectors (these are Erlang distributed with n=3, so three compartments)
E_2V0 = E_2Vstar;   %Exposed vectors
E_3V0 = E_3Vstar;   %Exposed vectors
I_V0 = I_Vstar;     %Infected vectors


MaxTime = 10*365;   %Duration of simulation

%Intervention
Year = [Year(1)-3 Year(1)-2 Year(1)-1 Year];                                                     %Goes 3 years before data and 1 year before intervention
NumberPeopleScreened = [0 NumberPeopleScreened(1) NumberPeopleScreened(1) NumberPeopleScreened]; %Missing 2 years intervention at same as first with data
TurnOut = NumberPeopleScreened./(N(1)+N(2));                                                     %Proportion of population screened (from total number in health zones, N_H)
NumberScreenings=length(Year);                                                                   %Number of years screening
Frequency=365;                                                                                   %Frequency of active screening campaign in days
Compliance=1;                                                                                    %Compliance of HAT positive stage I patients to treatment
Sensitivity=0.91;                                                                                %Diagnostic algorithm sensitivity
Specificity=0.999;                                                                               %Diagnostic algorithm specificity

for j=1:NumberScreenings
    DandT(j,:)=[TurnOut(j)*Sensitivity*Compliance TurnOut(j)*Sensitivity*Compliance 0 0 0 0];   %Total efficacy of active detection and treatment (humans only!)
    
    %Reporting rates
    TruePos(j,:) = [TurnOut(j)*Sensitivity TurnOut(j)*Sensitivity 0 0 0 0];                     %From EH IH1 and IH2
    FalsePos(j,:) = [TurnOut(j)*(1-Specificity) TurnOut(j)*(1-Specificity) 0 0 0 0];            %From SH
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The main iteration 

parameter=[b_H mu_H gamma_H sigma_H delta_H eta_H omega_H phi_H epsilon sigma_V b_V mu_V alpha p_H p_V s];

[t, pop]=ode45(@diffHATmodel,[0 MaxTime],[S_H0 E_H0 I_1H0 I_2H0 R_H0 S_V0 E_1V0 E_2V0 E_3V0 I_V0 G_V0],[],parameter);


T=length(t);
S_H=pop(:,1:x)';
E_H=pop(:,x+1:2*x)'; I_1H=pop(:,2*x+1:3*x)'; I_2H=pop(:,3*x+1:4*x)'; R_H=pop(:,4*x+1:5*x)';
S_V=pop(:,5*x+1); E_1V=pop(:,5*x+2); E_2V=pop(:,5*x+3); E_3V=pop(:,5*x+4); I_V=pop(:,5*x+5); G_V=pop(:,5*x+6);

o=ones(1,x);

%Runs ODE for every year of screening
if NumberScreenings>0
    for i=1:NumberScreenings


            [t2, pop2]=ode45(@diffHATmodel,[MaxTime+Frequency*(i-1) MaxTime+Frequency*i],[S_H(:,end)' (o-DandT(i,:)).*E_H(:,end)' (o-DandT(i,:)).*I_1H(:,end)' (o-DandT(i,:)).*I_2H(:,end)' R_H(:,end)'+DandT(i,:).*(E_H(:,end)'+I_1H(:,end)'+I_2H(:,end)') S_V(end) E_1V(end) E_2V(end) E_3V(end) I_V(end) G_V(end)],[],parameter);
            
            T=[T; T(end)+length(t2)];    %Gives all the time-points when intervention occurs
            
            t=[t; t2];
            S_H=[S_H pop2(:,1:x)'];
            E_H=[E_H pop2(:,x+1:2*x)'];
            I_1H=[I_1H pop2(:,2*x+1:3*x)'];
            I_2H=[I_2H pop2(:,3*x+1:4*x)'];
            R_H=[R_H pop2(:,4*x+1:5*x)'];
            S_V=[S_V; pop2(:,5*x+1)];
            E_1V=[E_1V; pop2(:,5*x+2)];
            E_2V=[E_2V; pop2(:,5*x+3)];
            E_3V=[E_3V; pop2(:,5*x+4)];
            I_V=[I_V; pop2(:,5*x+5)];
            G_V=[G_V; pop2(:,5*x+6)];
            

    end
end


 TotalN_H=sum(S_H(1:4,:)+E_H(1:4,:)+I_1H(1:4,:)+I_2H(1:4,:)+R_H(1:4,:));


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Calculate incidences per year per 10 000 population

%Pre-active surveillence (equilibrium)
%find the point where t is closest to MaxTime-365 (a year before active screening)
[c index] = min(abs(t-(MaxTime-365)));


%Find area under phi_H*I_H2 for number of passive cases in one year
%Times by 10,000/N_H to give incidence per 10,000 per year
PasIncPre=u*365*(gamma_H(1))*sum(I_2H(1:4,T(1))).*(10000./TotalN_H(T(1)))

%Compute the feeding choice on hosts given host abundance at each time-step
dNH=S_H+E_H+I_1H+I_2H+R_H;
dNH(dNH==0)=1;
k=bsxfun(@rdivide, dNH, dNH(1,:));
f=bsxfun(@rdivide,repmat(s',1,length(t)).*k,sum(repmat(s',1,length(t)).*k));
        
%New infection (actual) incidence pre-screening
ActualIncPre=365*alpha*I_V(T(1))*p_H(1:4)*(f(1:4,T(1)).*S_H(1:4,T(1))./dNH(1:4,T(1))).*(10000./TotalN_H(T(1)));


%Post-active surveillence (incidence per year per 10,000 people and number per year)
if NumberScreenings>0
    for i=1:NumberScreenings
        
        %New infection (actual) human incidence (influx into I_1)
        FOI=alpha*I_V(T(i):T(i+1))'.*(p_H(1:4)*(f(1:4,T(i):T(i+1)).*S_H(1:4,T(i):T(i+1))./dNH(1:4,T(i):T(i+1))));
        
        ActualInc(i)=trapz(t(T(i):T(i+1)),FOI)*(10000./TotalN_H(T(i)));
        
        ActualNumber(i)=trapz(t(T(i):T(i+1)),FOI);
        
        %Active
        ActInc(i)=( TruePos(i,:)*(E_H(:,T(i))+I_1H(:,T(i))+I_2H(:,T(i)))...
                        + FalsePos(i,:)*(S_H(:,T(i))) )*(10000/TotalN_H(T(i))); %Here T(i) is timepoint just before active screen
        ActNumber(i) = TruePos(i,:)*(E_H(:,T(i))+I_1H(:,T(i))+I_2H(:,T(i)))...
                        + FalsePos(i,:)*(S_H(:,T(i)));

        %Passive 
        PasInc(i)=u*(gamma_H(1))*trapz(t(T(i):T(i+1)),sum(I_2H(:,T(i):T(i+1)))).*(10000./TotalN_H(T(i)));
        
        PasNumber(i) = u*gamma_H(1)*trapz(t(T(i):T(i+1)),sum(I_2H(:,T(i):T(i+1))));
       
    end

end



if NumberScreenings>0
    
    %Convert to years with Year(1) being the first year of screening
    tYear=(t-MaxTime)./365+Year(1);
    
    set(0,'DefaultAxesFontSize',14)
    set(0,'DefaultLineLinewidth',1.2)

    figure(1)
    clf;
    
    %%%%%%%%%%%%%%
    subplot(4,1,1)
    hold on
    
    h=plot(tYear,100*sum(I_1H(1:4,:))./TotalN_H,'Color',[1 0.55 0]);
    plot(tYear,100*sum(I_2H(1:4,:))./TotalN_H,'-r');
    plot(tYear,100*sum(R_H(1:4,:))./TotalN_H,'-b');
 
    oldx=xlim;
    oldy=ylim;
    axis([(Year(1)-1) Year(NumberScreenings)+1 0 Inf])
    yl=ylabel('% humans');
    pos=get(yl,'Pos');
    xpos=1.9946e3;
    set(yl,'Pos',[xpos pos(2) pos(3)]);
    legend('Stage 1','Stage 2','Hospital','Location',[0.742 0.89 0.15 0.02])
    hold off

    %%%%%%%%%%%%%%
    subplot(4,1,2)
    hold on
    
    h=plot(tYear,100*(I_1H(5,:))./(S_H(5,:)+E_H(5,:)+I_1H(5,:)),'r');
    oldx=xlim;
    oldy=ylim;
    axis([(Year(1)-1) Year(NumberScreenings)+1 0 oldy(2)])
    yl=ylabel('% animals');
    pos=get(yl,'Pos');
    set(yl,'Pos',[xpos pos(2) pos(3)]);
    legend('Infectious','Location',[0.73 0.685 0.15 0.02])
    hold off
    
    %%%%%%%%%%%%%%
    subplot(4,1,3)
    hold on 
    
    TotalNV=S_V+G_V+E_1V+E_2V+E_3V+I_V;
    
    h=plot(tYear,100*E_1V./TotalNV,'Color',[1 0.55 0]);
    plot(tYear,100*E_2V./TotalNV,'Color',[1 0.55 0],'Linestyle','--');
    plot(tYear,100*E_3V./TotalNV,'Color',[1 0.55 0],'Linestyle','-.');
    plot(tYear,100*I_V./TotalNV,'-r');

    oldx=xlim;
    oldy=ylim;
    axis([(Year(1)-1) Year(NumberScreenings)+1 0 Inf])
    yl=ylabel('% vectors');
    pos=get(yl,'Pos');
    set(yl,'Pos',[xpos pos(2) pos(3)]);
    legend('','Exposed','','Infectious','Location',[0.73 0.43 0.15 0.05])
    hold off
    
    %%%%%%%%%%%%%%
    subplot(4,1,4)
    hold on
    
    t_plot=t(index);
    for i=1:NumberScreenings
        t_plot=[t_plot t(T(i)) t(T(i))];
    end
    t_plot=[t_plot t(T(NumberScreenings+1))];
    tYear_plot=(t_plot-MaxTime)./365+Year(1);
    
    %Plots active, passive and total incidence
    h=plot(tYear_plot,[ 0 0 reshape(repmat(ActInc, 2,1),1,[])],'Color','r'); 
    plot(tYear_plot,[ PasIncPre PasIncPre reshape(repmat(PasInc, 2,1),1,[])],'Color','b');
    plot(tYear_plot,[ PasIncPre PasIncPre reshape(repmat(ActInc+PasInc, 2,1),1,[])],'Color','k');
    plot(tYear_plot,[ ActualIncPre ActualIncPre reshape(repmat(ActualInc, 2,1),1,[])],'Color',[0 0.7 0]);
    %Plots 1 per 10,000 incidence goal
    plot([0 tYear(end)],[1 1],'--','Color',[0 0.7 0])
    xlabel 'Time';
    ylabel ({'Incidence per year' ' per 10 000'});
    oldx=xlim;
    oldy=ylim;
    axis([(Year(1)-1) Year(NumberScreenings)+1 0 Inf])

    legend(['Actively detected'],['Passively detected'],'Total detection','New infections','Location',[0.68 0.21 0.15 0.05])
    hold off

    set(figure(1),'Position',[0 0 500 1000]);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Calculates the differential rates used in the integration.
function dPop=diffHATmodel(t,pop, parameter)

x=(length(parameter)-6)/10;

b_H =   parameter(1:x)';
mu_H =   parameter(x+1:2*x)';
gamma_H=parameter(2*x+1:3*x)';
sigma_H=parameter(3*x+1:4*x)';
delta_H=    parameter(4*x+1:5*x)';
eta_H = parameter(5*x+1:6*x)';
omega_H = parameter(6*x+1:7*x)';
phi_H = parameter(7*x+1:8*x)';

epsilon=parameter(8*x+1);
sigma_V=parameter(8*x+2);
b_V=    parameter(8*x+3);
mu_V=    parameter(8*x+4);
alpha=parameter(8*x+5);
p_H=    parameter(8*x+6:9*x+5)';
p_V=    parameter(9*x+6);
s=      parameter(9*x+7:10*x+6)';

S_H=pop(1:x); E_H=pop(x+1:2*x); I_1H=pop(2*x+1:3*x); I_2H=pop(3*x+1:4*x);R_H=pop(4*x+1:5*x);
S_V=pop(5*x+1); E_1V=pop(5*x+2); E_2V=pop(5*x+3); E_3V=pop(5*x+4); I_V=pop(5*x+5); G_V=pop(5*x+6);

N_H=S_H+E_H+I_1H+I_2H+R_H;
N_V=S_V+E_1V+E_2V+E_3V+I_V+G_V;

k=N_H./N_H(1);
f=(s.*k)/sum(s.*k);

dNH=N_H; dNH(N_H==0)=1;

dPop=zeros(5*x+6,1);

dPop(1:x)= mu_H.*N_H + omega_H.*R_H - I_V*alpha*p_H.*f.*S_H./dNH - mu_H.*S_H;
dPop(x+1:2*x)= I_V*alpha*p_H.*f.*S_H./dNH - (sigma_H + mu_H).*E_H;
dPop(2*x+1:3*x)= sigma_H.*E_H - (phi_H + mu_H).*I_1H;
dPop(3*x+1:4*x)= phi_H.*I_1H -  (gamma_H + mu_H + delta_H).*I_2H;
dPop(4*x+1:5*x) =  gamma_H.*I_2H - (omega_H + mu_H).*R_H;


dPop(5*x+1)= mu_V*N_V  - alpha*S_V - mu_V*S_V;
dPop(5*x+6)= alpha*(1-sum(f.*(I_1H+I_2H)./dNH)*p_V)*S_V - alpha*(sum(f.*(I_1H+I_2H)./dNH)*p_V)*epsilon*G_V - mu_V*G_V;
dPop(5*x+2)= alpha*(sum(f.*(I_1H+I_2H)./dNH)*p_V)*(S_V+epsilon*G_V) - 3*sigma_V*E_1V - mu_V*E_1V;
dPop(5*x+3)= 3*sigma_V*E_1V  - (3*sigma_V + mu_V)*E_2V;
dPop(5*x+4)= 3*sigma_V*E_2V  - (3*sigma_V + mu_V)*E_3V;
dPop(5*x+5)= 3*sigma_V*E_3V  -  mu_V*I_V;

end





