0
$\begingroup$

I am having trouble with generating a parametric plot with the following code:

Manipulate[
 DynamicModule[{s0, γ, sol, s, i, t},
  s0 = 1 - i0; γ = 1/l; 
  sol = NDSolve[
    {s'[t] == -β/(E^(k (t - τ)))*s[t]*i[t],
     i'[t] == β/(E^(k (t - τ)))*s[t]*i[t] - γ*i[t],
     s[0] == s0, i[0] == i0}, {s, i}, {t, 0, 150}]; 
  ParametricPlot[{s[t] /. sol, i[t] /. sol},
   {t, 0, 30}]],
 {{i0, 0.001, 
   "\!\(\*SubscriptBox[\(I\), \(0\)]\): infected at the beginning"},
  0, 0.1, 0.001, ImageSize -> Tiny, 
  Appearance -> "Labeled"}, {{β, 1.5, 
   "β: transmission rate"}, 0.5, 5, 0.5,
  ImageSize -> Tiny, 
  Appearance -> "Labeled"}, {{l, 3, 
   "1/γ: infectious period (in days)"}, 1, 7, 
  1, ImageSize -> Tiny, 
  Appearance -> "Labeled"}, {{k, 0.003, "k: control rate (per day)"}, 
  0, 0.01, 
  0.001, ImageSize -> Tiny, 
  Appearance -> "Labeled"}, {{τ, 0, "τ: delay (in days)"}, 
  0, 5, 
  1, ImageSize -> Tiny, Appearance -> "Labeled"}]

After I ran the code, nothing appeared in the graph box, like so: enter image description here

$\endgroup$
  • $\begingroup$ have you been able to run the NDSolve[]and/or the ParametricPlot[] outside the Manipulate (I mean as a stand-alone code)? $\endgroup$ – Dr. belisarius Mar 6 '15 at 2:50
  • $\begingroup$ Also, I don't believe you need DynamicModule[] for that $\endgroup$ – Dr. belisarius Mar 6 '15 at 3:02
5
$\begingroup$

Change:

  sol = NDSolve[ ___ ]  ... By  sol = First@NDSolve[ ___ ]

result:

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks, and could you explain to me what First@NDSolve means and why it works in this situation when NDSolve doesn't work? $\endgroup$ – Bohan Lu Mar 6 '15 at 3:42
  • 9
    $\begingroup$ @BohanLu To put it in simple words: you show us a piece of code displaying some mastery of Mathematica. We (the readers) in absence of a sentence stating that you aren't the author, answer in a straightforward way because you should be able to understand that. Then you ask a really first-day-with-Mathematica question. Something doesn't compute here. $\endgroup$ – Dr. belisarius Mar 6 '15 at 4:01
  • $\begingroup$ I appreciate your patience. I am indeed a novice in Mathematica and I learned the code above from a paper. I was trying to modify the code a little bit to solve my problem and after referring to the documentation I still wouldn't be able to work it out. I wonder if you have any advice in regard to asking appropriately on this site? Do I have to tell people that I am a novice and cite the code I use every time? I mean, I understand people usually post high-quality and well-thought-out questions here, yet I really don't know another place to find experts like you to help me. $\endgroup$ – Bohan Lu Mar 6 '15 at 4:54
  • 1
    $\begingroup$ For example this question is nothing to do with SIR models, but is a simple misunderstanding of some input/ouptut of a function. Reduce your code to the simplest possible and then post the question. Learn to identify exactly where the problem lies. $\endgroup$ – djp Mar 6 '15 at 5:20
7
$\begingroup$

Your code can be simplified. In particular, I recommend that you get rid of the embedded DynamicModule and, instead, use a common trick of defining invisible controls to localize temporary variables.

Manipulate[
  s0 = 1 - i0;
  γ = 1/l;
  sol = NDSolve[{
    s'[t] == -β/(E^(k (t - τ)))*s[t]*i[t], 
    i'[t] == β/(E^(k (t - τ)))*s[t]*i[t] - γ*i[t], 
    s[0] == s0, i[0] == i0}, 
    {s, i}, {t, 0, 150}];
  ParametricPlot[{sol[[1, 1, 2]][t], sol[[1, 2, 2]][t]}, {t, 0, 30}],
  {s0, None}, {γ, None}, {sol, None}, (* invisible controls *)
  {{i0, 0.001,"\!\(\*SubscriptBox[\(I\), \(0\)]\): infected at the beginning"}, 
    0.001, 0.1, 0.001, ImageSize -> Tiny, Appearance -> "Labeled"}, 
  {{β, 1.5, "β: transmission rate"}, 0.5, 5, 0.5, 
    ImageSize -> Tiny, Appearance -> "Labeled"}, 
  {{l, 3, "1/γ: infectious period (in days)"}, 1, 7, 1, 
    ImageSize -> Tiny, Appearance -> "Labeled"}, 
  {{k, 0.003, "k: control rate (per day)"}, 0, 0.01, 0.001, 
    ImageSize -> Tiny, Appearance -> "Labeled"}, 
  {{τ, 0, "τ: delay (in days)"}, 0, 5, 1, 
    ImageSize -> Tiny, Appearance -> "Labeled"}]

manipulate

$\endgroup$
  • $\begingroup$ {s0, None}, {γ, None}, {sol, None}, (* invisible controls *) Invisible Controls! This looks interesting, but I can't quite see what it does. Can you elaborate/explain? $\endgroup$ – Jagra Mar 9 '15 at 22:35
  • $\begingroup$ Thinking more about this. Does this just create additional variables within the Manipulate to which one can assign any value? Couldn't one achieve the same thing by wrapping the Manipulate in Module? (I do like this shortcut of yours) $\endgroup$ – Jagra Mar 9 '15 at 22:56
  • $\begingroup$ @Jagra. You are correct in thinking that specifying an invisible control creates a dynamic variable localized to the Manipulate that can be used in the same way a localized variable declared in the first argument of a DynamicModule can be used, As for wrapping a Manipulate in a Module to introduce localized variables, there are situations when that can cause serious problems. Read the comments to this question for further info. $\endgroup$ – m_goldberg Mar 10 '15 at 0:17
  • $\begingroup$ The comments to the question you referenced explain a number of funny behavior I've witnessed in the past. Thanks for the explanation and the warning. $\endgroup$ – Jagra Mar 10 '15 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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