Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm working on a class demo of recurrence equations and I want to show the impact of different parameter values using Manipulate. Here's my code:

Manipulate[
 ListPlot[
  RecurrenceTable[
   {s[n + 1] == 
     s[n] - \[Gamma]*(i[n]/PP) s[n] + \[Beta] (PP - s[n]) - \[Beta] PP x,
    i[n + 1] == i[n] + \[Gamma] (i[n]/PP) s[n] - \[Rho]*i[n] - \[Beta]*i[n],
    s[1] == 8000,
    i[1] == 2000
    },
   {s, i},
   {n, 1, 10},
   ],
  PlotRange -> All,
  Joined -> True
  ],
 {\[Gamma], 0.1, 0.5},
 {\[Beta], 0.01, 0.5},
 {\[Rho], 0.1, 0.5},
 {PP, 10000, 20000},
 {x, 0, 1}
 ]

This code shows the phase space plot of s vs. i. How do I produce a plot of the two series s and i as dependent variables of, say,n?

Bonus question: how do I fix the PlotRange of the ListPlot so that it can fit even the largest curve in phase space?

share|improve this question

closed as too localized by Mr.Wizard Feb 27 at 4:23

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

up vote 1 down vote accepted

First, you have an extra comma in {n, 1, 10}, that you must remove.

Second, simply set an explicit range with e.g. PlotRange -> {{-3000, 12000}, {0, 3000}}.
I did not try to determine the maximum extent; I leave that as an exercise for you.

And, finally, the following code will plot both parts as functions of n:

Manipulate[
 values = RecurrenceTable[
   {s[n + 1] == 
     s[n] - \[Gamma]*(i[n]/PP) s[n] + \[Beta] (PP - s[n]) - \[Beta] PP x,
    i[n + 1] == i[n] + \[Gamma] (i[n]/PP) s[n] - \[Rho]*i[n] - \[Beta]*i[n],
    s[1] == 8000,
    i[1] == 2000
    },
   {s, i},
   {n, 1, 10}
   ];
 ListPlot[
  MapIndexed[{#2[[2]],#}&,Transpose[values],{2}],
  PlotRange -> All,
  Joined -> True
  ],
 {\[Gamma], 0.1, 0.5},
 {\[Beta], 0.01, 0.5},
 {\[Rho], 0.1, 0.5},
 {PP, 10000, 20000},
 {x, 0, 1}
]

You can grab just the MapIndexed[{#2[[2]],#}&,Transpose[values],{2}], if you care to try to figure that out.

share|improve this answer
Thanks Mark; I overlooked that part. (Always remember you can vote to reopen if you wish.) – Mr.Wizard Feb 27 at 4:38
Wow, Mark, that's fantastic! Thank you for teaching me about MapIndexed. Programming inside a function call is always a challenge. – user6120 Feb 27 at 4:44
@Mr.Wizard No biggie - I'll save my reopen votes for the WolframAlpha questions. :) – Mark McClure Feb 27 at 5:25
@user6120 No problem! I hope your students like it. – Mark McClure Feb 27 at 5:25
Showing this tomorrow in a control theory class at MIT. Thanks again! – user6120 Feb 27 at 5:37
show 1 more comment

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