2
$\begingroup$

I have troubles to understand how to use Manipulate and ParametricNDSolve to have modulable parameters inside my models. I checked numerous questions on this forum and couldn't find one that satisfies my needs (or, more likely, I didn't understand what to do):

In these examples, the parameters are defined in an equation that isn't a differential one. I don't know if it changes anything in my case but its seems to be at the origin of my problem.

Anyway, here is what I did:

First of all, I've got this model and these constants:

dEc = p1*Ec[t]*(1 - (Ec[t]/Emax)) - d1*Ec[t] - i1*Ic[t]*Ec[t] - r*Vu[t]*Ec[t];
dIc = i1*Ic[t]*Ec[t] + r*Vu[t]*Ec[t] - u1*Ic[t];
dVp = is[t]*b1*u1*Ic[t] - c1*Vp[t];
dUc = p2*Uc[t]*(1 - (Uc[t]/Umax)) - d2*Uc[t] - i2*Yc[t]*Uc[t] - e*Vp[t]*Uc[t];
dYc = i2*Yc[t]*Uc[t] + e*Vp[t]*Uc[t] - u2*Yc[t];
dVu = is[t]*b2*u2*Yc[t] - c2*Vu[t] + s*Vp[t];

parameters = {Emax ->   10000, Umax ->   10000000, p1 ->   0.6, d1 ->   0.003, d2 ->  0.006, u1 ->   0.33, u2 ->   0.33, c1 ->   10, c2 ->   1, b1 ->   6000, b2 ->  6000, i1 ->   0.0000002, i2 ->   0.0000001, e ->   0.000000001, r ->   0.0000001};

Please note that the parameters p2 and sare not defined in the parameters list. It's because that's the ones I want to modify using Manipulate! These parameters are found in equations dUc and dVu

Then, I do a ParametricNDSolve on these equations:

dynamicsmodels =
 ParametricNDSolve[
  Evaluate[{
     Ec'[t] == dEc ,
      Ic'[t] == dIc ,
     Vp'[t] == dVp ,
     Uc'[t] == dUc ,
     Yc'[t] == dYc ,
     Vu'[t] == dVu , 
     Ec[0] == 10000, Ic[0] == 0, Vp[0] == 10, Uc[0] == 10000000, 
     Yc[0] == 0, Vu[0] == 0, is[0] == 0.0005,
      WhenEvent[t == 200, is[t] -> 0.0001]
     } /. parameters],
  {Ec[t], Ic[t], Vp[t], Uc[t], Yc[t], Vu[t]},
  {t, 0, 300},
  {p2, s},
  DiscreteVariables -> {is}[[1]]]

Please note that is[t] is a variable that evolves in the model following time (see my previous question: Having trouble using WhenEvent with NDSolve).

Then, I do a Manipulate:

Manipulate[
 Plot[
  Evaluate[
   {
     Ec[p2, s][t],
     Ic[p2, s][t],
     Vp[p2, s][t],
     Uc[p2, s][t],
     Yc[p2, s][t],
     Vu[p2, s][t]
     } /. dynamicsmodels
   ], {t, 0, 300},
  PlotRange -> {{0, 300}, {0, 10000}},
  PlotStyle -> {Black, Red, Green, Blue, Purple, Orange},
  PlotLegends -> {"Ec", "Ic", "Vp", "Uc", "Yc", "Vu"}], 
 {p2, 1.2, 1.5, Appearance -> "Labeled"},
 {s , 1, 100, Appearance -> "Labeled"} 
 ]

I don't have any error, but no lines either... Am I doing something wrong?

enter image description here

Thanks!

$\endgroup$
1

1 Answer 1

2
$\begingroup$

Using ParametricNDSolveValue:

pndsval = ParametricNDSolveValue[
  Evaluate[{Ec'[t] == dEc, Ic'[t] == dIc, Vp'[t] == dVp, 
     Uc'[t] == dUc, Yc'[t] == dYc, Vu'[t] == dVu, Ec[0] == 10000, 
     Ic[0] == 0, Vp[0] == 10, Uc[0] == 10000000, Yc[0] == 0, 
     Vu[0] == 0, is[0] == 0.0005, 
     WhenEvent[t == 200, is[t] -> 0.0001]} /. parameters], {Ec[t], 
   Ic[t], Vp[t], Uc[t], Yc[t], Vu[t]}, {t, 0, 300}, {p2, s}, 
  DiscreteVariables -> {is}[[1]]]; 

 Manipulate[Plot[Evaluate[pndsval[p2, s] /. t -> v], {v, 0, 300}, 
  PlotRange -> {{0, 300}, {0, 10000}}, 
  PlotStyle -> {Black, Red, Green, Blue, Purple, Orange}, 
  PlotLegends -> {"Ec", "Ic", "Vp", "Uc", "Yc", "Vu"}], {p2, 1.2, 1.5,
   Appearance -> "Labeled"}, {s, 1, 100, Appearance -> "Labeled"}]

enter image description here

$\endgroup$
2
  • $\begingroup$ This works pefectly well. Thanks a lot. I see that I need to improve my maths now! $\endgroup$
    – Jerobou
    Commented Mar 12, 2019 at 18:12
  • 1
    $\begingroup$ @Jerobou, you are welcome. Thank you for the accept. $\endgroup$
    – kglr
    Commented Mar 12, 2019 at 18:13

Your Answer

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

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