1
$\begingroup$

I am using Manipulate to change a parameter in a system of differential equations solved with NDSolve in Mathematica. The system of equations is as follows: Differential equation

I am using Manipulate to allow interactive changes to the parameter 𝑑. While the plot is correctly generated when first run, changing the value of 𝑑 using the slider in Manipulate does not update the plot. This is the code that I used:

ClearAll[S, Ih, Y];
ClearAll[r, \[Gamma], p, c, \[Alpha], k, \[Delta], \[Xi], \[Lambda], 
  d, A1, A2, A3, A4, Jacobi, jacobian[S_, Ih_, Y_]];
r = 1.5;
\[Gamma] = 0.2;
p = 0.9;
c = 3;
\[Alpha] = 6;
k = 50;
\[Delta] = 0.1;
\[Eta] = 0.01;
\[Lambda] = 1;
d = 0.022;

Manipulate[
 Module[{plt2, plt3, plt4, sol, S0 = SS0, Ih0 = IhIh0, Y0 = YY0}, sol =
   NDSolve[{S'[t] == 
      1.5*S[t]*(1 - (S[t] + Ih[t])/50) - 1*S[t]*Ih[t] - (
       0.9*(S[t])^2*Y[t])/(S[t] + 6*Ih[t]),
     Ih'[t] == 
      1*S[t]*Ih[t] - (3*(Ih[t])^2*Y[t])/(S[t] + 6*Ih[t]) - 0.2*Ih[t],
     Y'[t] == (0.1*0.9*(S[t])^2*Y[t])/(S[t] + 6*Ih[t]) - (
       0.01*3*(Ih[t])^2*Y[t])/(S[t] + 6*Ih[t]) - 0.022*Y[t],
     S[t /; t <= 0] == S0, Ih[t /; t <= 0] == Ih0, 
     Y[t /; t <= 0] == Y0}, {S[t], Ih[t], Y[t]}, {t, 0, 500}];
  plt2 = 
   ParametricPlot[{t, S[t]} /. sol, {t, 0, 500}, PlotRange -> All, 
    AspectRatio -> 1, PlotStyle -> {Red, Thick}, Axes -> {t, S}];
  plt3 = 
   ParametricPlot[{t, Ih[t]} /. sol, {t, 0, 500}, PlotRange -> All, 
    AspectRatio -> 1, PlotStyle -> {Green, Thick}, Axes -> {t, Ih}];
  plt4 = 
   ParametricPlot[{t, Y[t]} /. sol, {t, 0, 500}, PlotRange -> All, 
    AspectRatio -> 1, PlotStyle -> {Blue, Thick}, Axes -> {t, Y}];
  Show[plt2, plt3, plt4, ImageSize -> {300, 300}]],
 Style["Persamaan diferensial :", Bold],
 Style["S'=rS(1-\!\(\*FractionBox[\(S + I\), \(K\)]\))-\[Lambda]SI-\!\
\(\*FractionBox[\(\*SuperscriptBox[\(pS\), \(2\)] Y\), \(S + \
\[Alpha]I\)]\)", Bold],
 Style["I'=\[Lambda]SI-\!\(\*FractionBox[\(\*SuperscriptBox[\(cI\), \
\(2\)] Y\), \(S + \[Alpha]I\)]\)-\[Gamma]I", Bold],
 Style["Y'=\!\(\*FractionBox[\(\*SuperscriptBox[\(\[Delta]pS\), \
\(2\)] Y\), \(S + \
\[Alpha]I\)]\)-\!\(\*FractionBox[\(\*SuperscriptBox[\(\[Eta]cI\), \(2\
\)] Y\), \(S + \[Alpha]I\)]\)-dY", Bold],
 Delimiter,
 Style["parameters", Bold, 10], {{d, 0.022, "d"}, 0, 3, 0.1, 
  ImageSize -> Small, Appearance -> "Labeled"},
 Delimiter,
 Style["initial conditions", Bold, 10],
 {{SS0, 4, "S0"}, 0, 20, .01, ImageSize -> Small, 
  Appearance -> "Labeled"},
 {{IhIh0, 1, "Ih0"}, 0, 20, .01, ImageSize -> Small, 
  Appearance -> "Labeled"},
 {{YY0, 1, "Y0"}, 0, 20, .01, ImageSize -> Small, 
  Appearance -> "Labeled"},
 ControlPlacement -> Left, SynchronousUpdating -> False]```

However if I manually change the value of 𝑑 within the code, the plot updates as expected.

This is the code where I have manually changed the value of d: manually changed the value of d within the code

Here are the graphs for comparison between the three cases. image A shows the initial graph where 𝑑=0.022. image A image B is the graph after I adjusted the slider in Manipulate to set 𝑑=1.6 (as can be seen, there is no change in the graph). image B image C is the graph obtained by manually changing the value of 𝑑 in the code to d=1.6. image C

$\endgroup$
2
  • 2
    $\begingroup$ I can't see any parameter "d" in your DE. $\endgroup$ Commented Mar 5 at 14:30
  • 1
    $\begingroup$ You haven't used d in your code but instead have hardcoded the value of 0.022 in your code: Y'[t] == (0.1*0.9*(S[t])^2*Y[t])/(S[t] + 6*Ih[t]) - (0.01*3*(Ih[t])^2* Y[t])/(S[t] + 6*Ih[t]) - 0.022*Y[t]. Change it to Y'[t] == (0.1*0.9*(S[t])^2*Y[t])/(S[t] + 6*Ih[t]) - (0.01*3*(Ih[t])^2* Y[t])/(S[t] + 6*Ih[t]) - d*Y[t] $\endgroup$ Commented Mar 5 at 16:07

1 Answer 1

1
$\begingroup$

Use ParametricNDSolve

$Version

(* "14.2.0 for Mac OS X ARM (64-bit) (December 26, 2024)" *)

Clear["Global`*"]

Parameters

r = 3/2;
\[Gamma] = 1/5;
p = 9/10;
c = 3;
\[Alpha] = 6;
k = 50;
\[Delta] = 1/10;
\[Eta] = 1/100;
\[Lambda] = 1;

Equations

eqns = {
   S'[t] == 
    r*S[t]*(1 - (S[t] + Ih[t])/k) - \[Lambda]*S[t]*
      Ih[t] - (p*S[t]^2*Y[t])/(S[t] + \[Alpha]*Ih[t]),
   Ih'[t] == \[Lambda]*S[t]*
      Ih[t] - (c*Ih[t]^2*Y[t])/(S[t] + \[Alpha]*Ih[t]) - \[Gamma]*
      Ih[t], Y'[
     t] == (\[Delta]*p*S[t]^2*Y[t])/(S[t] + \[Alpha]*Ih[t]) - (\[Eta]*
        c*Ih[t]^2*Y[t])/(S[t] + \[Alpha]*Ih[t]) - d*Y[t],
   S[0] == S0, Ih[0] == Ih0, Y[0] == Y0};

sol =
  ParametricNDSolve[eqns, {S, Ih, Y}, {t, 0, 500}, {d, S0, Ih0, Y0}];

Plots

Manipulate[Plot[Evaluate[{
     S[d, S0, Ih0, Y0][t],
     Ih[d, S0, Ih0, Y0][t],
     Y[d, S0, Ih0, Y0][t]} /. sol], {t, 0, 500},
  PlotRange -> All, AspectRatio -> 1,
  PlotStyle -> {{Red, Thick}, {Opacity[0.75, Green], Thick}, {Blue, 
     Thick}},
  AxesLabel -> {Style[t, 14], None},
  ImageSize -> {300, 300},
  PlotLegends -> Placed[{"S", "I", "Y"}, {0.9, 0.8}]],
 Style["Persamaan diferensial :", Bold],
 Style["S'=rS(1-\!\(\*FractionBox[\(S + I\), \
\(K\)]\))-\[Lambda]SI-\!\(\*FractionBox[\(\*SuperscriptBox[\(pS\), \
\(2\)] Y\), \(S + \[Alpha]I\)]\)", Bold], 
 Style["I'=\[Lambda]SI-\!\(\*FractionBox[\(\*SuperscriptBox[\(cI\), \
\(2\)] Y\), \(S + \[Alpha]I\)]\)-\[Gamma]I", Bold], 
 Style["Y'=\!\(\*FractionBox[\(\*SuperscriptBox[\(\[Delta]pS\), \
\(2\)] Y\), \(S + \[Alpha]I\)]\)-\!\(\*FractionBox[\(\*SuperscriptBox\
[\(\[Eta]cI\), \(2\)] Y\), \(S + \[Alpha]I\)]\)-dY", Bold], Delimiter,
 Style["parameters", Bold, 10],
 Style["r = 3/2, \[Gamma] = 1/5, p = 9/10\nc = 3, \[Alpha] = 6, k = \
50\n\[Delta] = 1/10, \[Eta] = 1/100, \[Lambda] = 1", 10],
 {{d, 0.022}, 0, 3, 0.1, ImageSize -> Small,
  Appearance -> "Labeled"}, Delimiter,
 Style["initial conditions", Bold, 10], 
 {{S0, 4, Subscript[S, 0]}, 0, 20, .01, ImageSize -> Small,
  Appearance -> "Labeled"},
 {{Ih0, 1, Subscript["I", 0]}, 0, 20, .01, ImageSize -> Small,
  Appearance -> "Labeled"},
 {{Y0, 1, Subscript[Y, 0]}, 0, 20, .01, ImageSize -> Small,
  Appearance -> "Labeled"},
 SynchronousUpdating -> False,
 TrackedSymbols :> {d, S0, Ih0, Y0}]

enter image description here

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.