1
$\begingroup$

I'm trying to use Manipulate to change some parameters in these differential equations, as follows:

 sol[\[Alpha]_, \[Beta]_] := 
  sol[\[Alpha], \[Beta]] = 
   NDSolve[{D[S[t], t] + \[Alpha]*S[t]*Inf[t] == 0, 
     D[Inf[t], t] + \[Beta]*Inf[t] - \[Alpha]*S[t]*Inf[t] == 0, 
     D[R[t], t] - \[Beta]*Inf[t] == 0, S[0] == 0.88, Inf[0] == 0.11, 
     R[0] == 0}, {S[t], Inf[t], R[t]}, {t, 0, 60}];


Manipulate[
 Plot[{S[t] /. sol[\[Alpha], \[Beta]], R[t] /. sol[\[Alpha], \[Beta]],
    Inf[t] /. sol[\[Alpha], \[Beta]]}, {t, 0, 40}, 
  PlotLegends -> {"Sanos", "Recuperados", "Infectados"}, 
  PlotStyle -> {Blue, Green, Red}, PlotStyle -> Thick, 
  GridLines -> Automatic, AxesLabel -> {"Días", "Población"}, 
  AxesStyle -> Directive[Black, 12]], {\[Alpha], 0.1, 0.9}, {\[Beta], 
  0.1, 0.9}]

But I get the following errors: ReplaceAll::reps: {NDSolve[{!(*SubscriptBox[([PartialD]), (0)](S[0]))+0.1 Inf[0] S[0]==0,!(*SubscriptBox[([PartialD]), (0)](Inf[0]))+0.1 Inf[0]-0.1 Inf[0] S[0]==0,!(*SubscriptBox[([PartialD]), (0)](R[0]))-0.1 Inf[0]==0,S[0]==0.88,Inf[0]==0.11,R[0]==0},{S[0],Inf[0],R[0]},{0,0,60}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

General::ivar: 0.` is not a valid variable.

General::ivar: 0.` is not a valid variable.

General::ivar: 0.` is not a valid variable.

General::stop: Further output of General::ivar will be suppressed during this calculation.

NDSolve::dsvar: 0.` cannot be used as a variable.

ReplaceAll::reps: {NDSolve[<<1>>]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

ReplaceAll::reps: {NDSolve[{\!\(\*SubscriptBox[\(\[PartialD]\), \(0\)]\(S[0]\)\)+0.1 Inf[0] S[0]==0,\!\(\*SubscriptBox[\(\[PartialD]\), \(0\)]\(Inf[0]\)\)+0.1 Inf[0]-0.1 Inf[0] S[0]==0,\!\(\*SubscriptBox[\(\[PartialD]\), \(0\)]\(R[0]\)\)-0.1 Inf[0]==0,S[0]==0.88,Inf[0]==0.11,R[0]==0},{S[0],Inf[0],R[0]},{0,0,60}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

General::stop: Further output of ReplaceAll::reps will be suppressed during this calculation.

NDSolve::dsvar: 0.` cannot be used as a variable.

NDSolve::dsvar: 0.` cannot be used as a variable.

General::stop: Further output of NDSolve::dsvar will be suppressed during this calculation.

$\endgroup$

3 Answers 3

2
$\begingroup$

another option

Manipulate[

 Module[{t, S, Inf, R},
    {S, Inf, R} = NDSolveValue[{
       D[S[t], t] + α*S[t]*Inf[t] == 0,
       D[Inf[t], t] + β*Inf[t] - α*S[t]*Inf[t] == 0,
       D[R[t], t] - β*Inf[t] == 0,
       S[0]   == 0.88,
       Inf[0] == 0.11,
       R[0]   == 0},
      {S, Inf, R},{t, 0, 60}];

   Plot[{S[t], Inf[t], R[t]}, {t, 0, 40},
     PlotLegends -> {"Sanos", "Recuperados", "Infectados"},
     PlotStyle -> {Blue, Green, Red},
     PlotStyle -> Thick,
     GridLines -> Automatic,
     AxesLabel -> {"Días", "Población"},
     AxesStyle -> Directive[Black, 12]
     ]
  ],

 {α, 0.1, 0.9},
 {β, 0.1, 0.9},

 TrackedSymbols :> {α, β}
 ]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Great! it works perfectly. Thank you very much! $\endgroup$ May 3, 2020 at 19:00
1
$\begingroup$

Try

Manipulate[
  sol[a_,b_]:={S[t],Inf[t],R[t]}/.NDSolve[{D[S[t],t]+a*S[t]*Inf[t]==0,
    D[Inf[t],t]+b*Inf[t]-a*S[t]*Inf[t]==0,D[R[t],t]-b*Inf[t]==0,S[0]==0.88,
    Inf[0]==0.11,R[0]==0},{S[t],Inf[t],R[t]},{t,0,60}][[1]];
  s=sol[a,b];Plot[s,{t,0,60}],{a,0.1,0.9},{b,0.1,0.9}]

and then undo each of those changes from your original code, one at a time, and see how many of those break things. Then try to study the documentation to try to understand why.

$\endgroup$
1
$\begingroup$

You can use ParametricNDSolveValue to create a parametric function and use it inside Manipulate:

ClearAll[pndsv]

pndsv = ParametricNDSolveValue[{D[S[t], t] + α*S[t]*Inf[t] == 0, 
    D[Inf[t], t] + β*Inf[t] - α*S[t]*Inf[t] == 0, 
    D[R[t], t] - β*Inf[t] == 0, 
    S[0] == 0.88, Inf[0] == 0.11,  R[0] == 0}, 
  {S, Inf, R}, {t, 0, 60}, {α, β}];

Manipulate[Plot[Evaluate @ Through @ pndsv[α, β] @ t, {t, 0, 40}, 
  PlotLegends -> {"Sanos", "Recuperados", "Infectados"}, 
  PlotStyle -> {Blue, Green, Red}, PlotStyle -> Thick, 
  GridLines -> Automatic, AxesLabel -> {"Días", "Población"}, 
  AxesStyle -> Directive[Black, 12]], 
{α, 0.1, 0.9}, {β, 0.1, 0.9}]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.