function dx=divpara3_rhs(x,p)
% function dx=divpara3_rhs(x,p)
% variables x
% . x(1): density of miracidia
% . x(2): density of susceptible snails
% . x(3:np+2): density of infested snails,
%              with 1, 2, ... np infesting trematodes
% . x(np+3:np+4): density of cercariae
%                 Schistosoma, non-Schistosoma
% parameters p
% . p.np: number of trematode species
% . p.g: input rate of miracidia
% . p.e: infestation rate of snails
% . p.fS: fitness of susceptible snails
% . p.K: carrying capacity of snails
% . p.s: shedding rate of snails
% . p.u: uptake rate by final host
% . p.dM: loss rate of miracidia
% . p.dI: loss rate of infested snails
% . p.dC: loss rate of cercariae

np=p.np;

if np==1
    x=x(:);
    %
    M=x(1);
    S=x(2);
    I=x(3);
    C=x(4);
    %
    dM=p.g-p.e*S*M-p.dM*M;
    dS=-p.e*S*M+p.fS*S*(1-(S+I)/p.K);
    dI=p.e*S*M-p.dI*I;
    dC=p.s*I-p.dC*C-p.u*C;
    %
    dx=[dM;dS;dI;dC];
else
    x=x(:);
    %
    M=x(1);
    S=x(2);
    I=x(3:(np+2));
    C=x((np+3):(np+4));
    %
    dM=p.g*np-p.e*S*M-p.dM*M;
    dS=-p.e*S*M+p.fS*S*(1-(S+sum(I))/p.K);
    dI=-p.dI*I;
    dI(1)=dI(1)+p.e*S*M;
    dI(1:(np-1))=dI(1:(np-1))-p.e*M*I(1:(np-1)).*(np-1:-1:1)'/np;
    dI(2:np)=dI(2:np)+p.e*M*I(1:(np-1)).*(np-1:-1:1)'/np;
    dC=-p.dC*C-p.u*C;
    dC(1)=dC(1)+p.s*I(1)/np;
    dC(2)=dC(2)+p.s*(sum(I)-I(1)/np);
    %
    dx=[dM;dS;dI;dC];
end

end