# RecursionLimit error when Plotting a recursive function

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 := 1;
fib := 1;
fib[n_] := fib[n - 1] + fib[n - 2];

• Use this code DiscretePlot[fib[n], {n, 0, 20}] or ListPlot[Table[fib[n], {n, 0, 20}]] rather than Plot – Junho Lee Oct 8 '14 at 3:35
• Use Plot[] when you need to use continuous functions, like in Plot[Sin[x], {x, 0, 2 Pi}] – Dr. belisarius Oct 8 '14 at 3:45

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 := 1;
fib := 1;
fib[n_] := fib[n - 1] + fib[n - 2];

DiscretePlot[fib[n], {n, 0, 10}] ListPlot[Table[fib[n], {n, 0, 10}]] 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 == f == 1}, f[n], n][]


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

Plot[fb, {n, 0, 10},
Prolog -> DiscretePlot[fib[n], {n, 0, 10}][]] • thanks you guys have been so helpful – Colby the Engineer Oct 8 '14 at 5:08