1

I'm a total noob with Mathematica, and while trying to plot the following, I get several error messages. What am I doing wrong?

O[n_] := O[n] = 0.7*O[n - 1] + 0.002*O[n - 1]*M[n - 1] ;

M[n_] := M[n] = 1.2*M[n - 1] - 0.001*O[n - 1]*M[n - 1] ;

O[0] := 150;

M[0] := 200;

Show[{DiscretePlot[{O[i], M[i]}, {i, 1, 10}]}]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Walter U.
  • 297
  • 1
  • 8

2 Answers2

4

You can also use RecurrenceTable, NestList, and the memoized version of the method in @David's answer:

ClearAll[rt1, rt2, rt3, aa, bb]
rt1 = Transpose@RecurrenceTable[{a[n] == 0.7 a[n - 1] + 0.002 a[n - 1] b[n - 1], 
         b[n] == 1.2 b[n - 1] - 0.001 a[n - 1] b[n - 1], 
         a[0] == 150, b[0] == 200}, {a, b}, {n, 0, #}] &;

rt2 = Transpose[NestList[{0.7 #[[1]] + 0.002 #[[1]] #[[2]], 
                1.2 #[[2]] - 0.001 #[[1]] #[[2]]} &, {150, 200}, #]] &;

aa[n_] := aa[n] = 0.7 aa[n - 1] + 0.002 aa[n - 1] bb[n - 1];
bb[n_] := bb[n] = 1.2 bb[n - 1] - 0.001 aa[n - 1] bb[n - 1];
aa[0] = 150; bb[0] = 200;
rt3 = Transpose@Table[{aa[n], bb[n]}, {n, 1, #}] &;


Row[ListPlot[#@100, BaseStyle -> PointSize[Large], 
    ImageSize -> 300] & /@ {rt1, rt2, rt3}, Spacer[5]]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Thank you Kguler! Could you explain to me the difference between your code and the one above? Yours seems to run much faster. Why is that? – Walter U. Apr 10 '15 at 23:37
  • @JohnWayne360, my pleasure. Re how RecurrenceTable and NestList work please see the linked docs pages. – kglr Apr 11 '15 at 00:15
3
a[n_] := 0.7 a[n - 1] + 0.002 a[n - 1] b[n - 1];

b[n_] := 1.2 b[n - 1] - 0.001 a[n - 1] b[n - 1];

a[0] := 150;

b[0] := 200;

 ListPlot[
 Transpose@Table[{a[n], b[n]}, {n, 1, 10}],
 PlotLegends -> {"a", "b"}]

enter image description here

David G. Stork
  • 41,180
  • 3
  • 34
  • 96
  • That does it, Thank you Mr. Stork! – Walter U. Apr 10 '15 at 20:47
  • @JohnWayne360 You're welcome. Perhaps you could upvote my answer, and click on the check mark to accept it. – David G. Stork Apr 10 '15 at 20:52
  • 1
    I will up-vote once I reach the required reputation level. Promise. On a different note, it never occurred to me how computationally expensive the sequence could get. My moderately powerful laptop can't seem to get past 15 iterations. Once again, many thanks. – Walter U. Apr 10 '15 at 23:26