Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am new to Mathematica and now I could use some help.

Trying to plot any of these functions generates a $RecursionLimit exception and I am wondering why.

G[y_] := 0.7 G[y - 1]
G[0] = 100;

J[y_] := 0.1*G[y - 1] + 0.6 J[y - 1]

H[y_] := 0.2*J[y] + 0.8*H[y - 1]
H[0] = 0

Plot[ G[x], {x, 0, 10}]

$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>

Evaluating any of G[2], H[4], J[3] works fine.

Any ideas would be much appreciated :)

share|improve this question
2  
Use DiscretePlot[G[x], {x, 0, 10}] . You are trying to plot for all x (Reals) between 0 and 10. –  belisarius May 5 at 14:56
    
@belisarius You reply or Community Wiki or something? :) –  Öskå May 5 at 15:59
    
The problem is that the implicit assumption is that recursion stops when the input reaches zero. This only works when the starting input in the call of G is a positive integer, which generally won't be the case in a Plot. You could use DiscretePlot as belisarius suggests or define the second part of the function as G[y_/;y<=0]=100. –  Sjoerd C. de Vries May 5 at 16:21
    
@Öskå Still pondering about this question meta.mathematica.stackexchange.com/q/1244/193 :) –  belisarius May 5 at 16:21
    
@belisarius so am I :D But when I answer in comments sometimes someone answers the same in a proper answer :) So I did ask what was your plans :) –  Öskå May 5 at 17:07

1 Answer 1

Here are two reasonable answers to your question.

  • (Belisarius)

    Use DiscretePlot[G[x], {x, 0, 10}] . You are trying to plot for all x (Reals) between 0 and 10

  • (Sjoerd C. de Vries)

    The problem is that the implicit assumption is that recursion stops when the input reaches zero. This only works when the starting input in the call of G is a positive integer, which generally won't be the case in a Plot. You could add G[y_]/;y <= 0] = 100 to the definition of G.

Thus, by combining each of them you have:

G[y_] := 0.7 G[y - 1]
G[y_] /; y <= 0 = 100
G[0] = 100;

J[y_] := 0.1*G[y - 1] + 0.6 J[y - 1]

H[y_] := 0.2*J[y] + 0.8*H[y - 1]
H[0] = 0;

Show[{DiscretePlot[G @ x, {x, 0, 10}, PlotStyle -> Red], Plot[G @ x, {x, 0, 10}]}]

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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