2
$\begingroup$

Why am I getting the error $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>

when I try to plot this fib Fibonacci function in Mathematica?

Plot[fib[n], {n, 0, 20}]

Where fib is defined as:

Clear[fib];
fib[0] := 1;
fib[1] := 1;
fib[n_] := fib[n - 1] + fib[n - 2];
$\endgroup$
2
  • $\begingroup$ Use this code DiscretePlot[fib[n], {n, 0, 20}] or ListPlot[Table[fib[n], {n, 0, 20}]] rather than Plot $\endgroup$
    – Junho Lee
    Commented Oct 8, 2014 at 3:35
  • $\begingroup$ Use Plot[] when you need to use continuous functions, like in Plot[Sin[x], {x, 0, 2 Pi}] $\endgroup$ Commented Oct 8, 2014 at 3:45

1 Answer 1

3
$\begingroup$

Original

Use this code DiscretePlot[fib[n], {n, 0, 20}] or ListPlot[Table[fib[n], {n, 0, 20}]] rather than Plot. Plot is intended for continuous-valued functions, not functions that are explicitly intended only to be defined over non-negative integers, as is the case here.

Clear[fib];
fib[0] := 1;
fib[1] := 1;
fib[n_] := fib[n - 1] + fib[n - 2];

DiscretePlot[fib[n], {n, 0, 10}]

Blockquote

ListPlot[Table[fib[n], {n, 0, 10}]]

Blockquote


Edit

If you use recurrence relation with RSolve like this, you would get the general formula that is applied to Plot as continuous function.

fb = f[n] /. 
  RSolve[{f[n] == f[n - 1] + f[n - 2], f[1] == f[0] == 1}, f[n], n][[1]]

1/2 (Fibonacci[n] + LucasL[n])

Plot[fb, {n, 0, 10},
 Prolog -> DiscretePlot[fib[n], {n, 0, 10}][[1]]]

Blockquote


$\endgroup$
1
  • $\begingroup$ thanks you guys have been so helpful $\endgroup$ Commented Oct 8, 2014 at 5:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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