0

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Bohan Lu
  • 139
  • 4

2 Answers2

7

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • {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? – Jagra Mar 09 '15 at 22:35
  • 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) – Jagra Mar 09 '15 at 22:56
  • @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. – m_goldberg Mar 10 '15 at 00:17
  • 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. – Jagra Mar 10 '15 at 00:30
5

Change:

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

result:

Mathematica graphics

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • Thanks, and could you explain to me what First@NDSolve means and why it works in this situation when NDSolve doesn't work? – Bohan Lu Mar 06 '15 at 03:42
  • 9
    @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. – Dr. belisarius Mar 06 '15 at 04:01
  • 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. – Bohan Lu Mar 06 '15 at 04:54
  • 1
    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. – djp Mar 06 '15 at 05:20