Message Boards Message Boards


How create function for Fibonacci series?

Posted 9 years ago
4 Replies
11 Total Likes

I know Mathematica has the default function for Fibonacci Serie but I need do a function that calculate any number that I introduce, for example:

n=2 fib=[n_]... So the result for the Fibonacci Serie it's 1 (1,1,2,3,5,8...)

Please anyone help me to generate this function and please explain why and the stepwise

POSTED BY: Camilo Pacheco
4 Replies

Of course, you could also use the built in function (which means that you do not need to define anything new!):

Fibonacci[#] & /@ Range[20]

This is quite a bit faster than defining your own function.



POSTED BY: Marco Thiel

Hi Camilo,

programmig the Fibonacci Serie is a very standard exercise - and that is probably the origin of your question. There are basically two ways to do it:

One can use the recursion directly:

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

This is elegant but in no way effective! (The double definition "recFib[n_] := recFib[n] =..." is a nice trick to give memory to a function, see "Functions That Remember Values They Have Found" in the documentation.)

A better way is implementing the general formula:

fib[n_] := With[{goldenRatio = (Sqrt[5] + 1)/2}, 
  Simplify[1/Sqrt[5] (goldenRatio^n - (1 - goldenRatio)^n)]]

(Yes, I know, there is a constant "GoldenRatio" predefined, but when using that the simplification to integers does not work ... An idea anybody?) From this formula it is obvious that the term "(1 - goldenRatio)^n" vanishes for large n (its absolute value is smaller than 1). Consequently the expression

fibApprox[x_]:= GoldenRatio^x/Sqrt[5.]

serves as a good approximation - and one can see that the growth is exponential (as it should be with rabbits ...)

Ciao Henrik

POSTED BY: Henrik Schachner
Posted 9 years ago

Thank you very mucho Henrik and Marco.

Henrik I prefer the first option, because in the class we're learning recursion from the functions, for example to generate the Euclidean algorithm:

Euclides[n_, 0] := n;

Euclides[n, r] := Euclides[r, Mod[n, r]];

In the exercise we have the plant case and the other cases that will probably generate, but Henrik in your recursion I don't understand the part of: recFib[n_] := recFib[n] = recFib[n - 1] + recFib[n - 2]

Can you explain me more deep please. If I do recFib[4] the function do: [4-1]+[4-2] or how? I really learn more about recursion functions. Thank you.

POSTED BY: Camilo Pacheco

Can you explain me more deep please. If I do recFib[4] the function do: [4-1]+[4-2] or how?

Well, that is the very basic idea of any recursion: In a first step fib[4] will be evaluated to fib[3]+fib[2]. With this the problem is not solved but reduced in its size. The documentation ("Principles of Evaluation") says:

The Wolfram Language is an infinite evaluation system. When you enter an expression, the Wolfram Language will keep on using definitions it knows until it gets a result to which no definitions apply.

Consequently in further steps the reduction of the problem continues until a trivial case (fib[0] = 0; fib[1] = 1;) is reached.

One can try to see this with:

fib[0] = 0; fib[1] = 1;
fib[n_] := fib[n - 1] + fib[n - 2]
Framed //@ Trace[fib[4]]

or with

fib1[0] = 0; fib1[1] = 1;
fib1[n_] := (Sow[n -> {n - 1, n - 2}]; fib1[n - 1] + fib1[n - 2])
Reap[fib1[4]][[2, 1]]

Ciao Henrik

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract