1
$\begingroup$

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}]}]
$\endgroup$

closed as off-topic by Mr.Wizard Jan 19 '17 at 23:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Mr.Wizard
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ O is a protected symbol; try a different name, preferably starting with a lower case letter. $\endgroup$ – b.gates.you.know.what Apr 10 '15 at 20:34
  • $\begingroup$ Thank you, that fixed one of the problems, I'm still getting: Series::ivar: 1 is not a valid variable. >> $\endgroup$ – Lucif3r Apr 10 '15 at 20:36
  • 2
    $\begingroup$ Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. $\endgroup$ – bbgodfrey Apr 10 '15 at 20:46
  • $\begingroup$ Related: (63524) $\endgroup$ – Mr.Wizard Jan 19 '17 at 23:34
  • $\begingroup$ Your code works as written after replacing O with o and M with m. I am therefore closing this question as a simple mistake. $\endgroup$ – Mr.Wizard Jan 19 '17 at 23:37
3
$\begingroup$
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

$\endgroup$
  • $\begingroup$ That does it, Thank you Mr. Stork! $\endgroup$ – Lucif3r Apr 10 '15 at 20:47
  • $\begingroup$ @JohnWayne360 You're welcome. Perhaps you could upvote my answer, and click on the check mark to accept it. $\endgroup$ – David G. Stork Apr 10 '15 at 20:52
  • 1
    $\begingroup$ 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. $\endgroup$ – Lucif3r Apr 10 '15 at 23:26
4
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Lucif3r Apr 10 '15 at 23:37
  • $\begingroup$ @JohnWayne360, my pleasure. Re how RecurrenceTable and NestList work please see the linked docs pages. $\endgroup$ – kglr Apr 11 '15 at 0:15

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