Fibonacci Numbers

We are going to study a sequence of numbers known as the Fibonacci numbers.  These numbers arise in many situations, one of which we now describe.
Suppose on a certain day we have one pair of baby rabbits, and we know that in one month they will have matured to adulthood, and then one month later will produce a pair of baby rabbits, then another pair of babies two months later, another pair three months later, and so on.  Furthermore, each new pair of baby rabbits will reach adulthood in one month and then produce a pair of babies one month later and each month thereafter.  How many adult pairs will there be after 5 months, after 30 months, after any specific number of months?  The numbers that answer these questions are called the Fibonacci numbers.  If we denote the number of adult pairs after n months by (Subscript[F, n]), then we want to find or calculate (Subscript[F, n]).  The first few Fibonacci numbers are easy to calculate by following the generations of our rabbit colony month-by-month; we find that (Subscript[F, 0 ])= 0, (Subscript[F, 1]) = 1, (Subscript[F, 2]) = 1, (Subscript[F, 3]) = 2, and (Subscript[F, 3]) = 3.  We would like, however, to have a simple, systematic way to find (Subscript[F, n]) for any n; we will, in fact, discuss several ways to calculate (Subscript[F, n]).

The Built-in Function Fibonacci

Let's see if Mathematica has a built-in function for computing Fibonacci numbers.

?Fib*

So we see that Fibonacci[n] gives (Subscript[F, n.]) We illustrate the use of this command.

Fibonacci[0]
Fibonacci[1]
Fibonacci[2]
Fibonacci[3]

These results confirm the values previously calculated.  And the following results answer the specific questions asked above.

Fibonacci[5]
Fibonacci[30]

So there are 5 pairs of adult rabbits after 5 months, and 832,040 adult pairs after 30 months.  We can also make a table of Fibonacci numbers with the commands Table and TableForm.  Here is a table of the first 16 Fibonacci numbers.

fibtable = Table[{n, Fibonacci[n]}, {n, 0, 15}]

TableForm[fibtable, TableDirections -> {Row, Column}, TableHeadings -> {None, {"n", "F[n]"}}, TableSpacing ->{1,1}]

The Fundamental Recurrence Relation

We have used the Fibonacci command as a "black box", with no idea how the calculations are done.  We now turn to a discussion of the computation of Fibonacci numbers.  In addition to explaining methods of computation, this discussion will introduce several useful Mathematica commands. In this section we show that the (Subscript[F, n])'s satisfy a recurrence relation.  From the earlier month-by-month calculation we see that the number of adult pairs after n months equals the number of adult pairs after n - 1 months plus the number of baby pairs after n - 1 months. Thus, since the number of baby pairs after n - 1 months equals the number of adult pairs after n - 2 months, we see that the number of adult pairs after n months equals the number of adult pairs after n - 1 months plus the number of adult pairs after n - 2 months.
If we let (Subscript[F, n]) denote the number of adult pairs after n months,  we can state this principle as
(1a)      (Subscript[F, n]) = (Subscript[F, n - 1]) + (Subscript[F, n - 2]), for all n ≥ 2.
This formula, which is called a recurrence relation, together with the values 
(1b)      (Subscript[F, 0]) = 0, (Subscript[F, 1]) = 1,
which are called initial values for the sequence (Subscript[F, n]), provides a procedure for calculating (Subscript[F, n])for any n:  (Subscript[F, 4])= 3,  (Subscript[F, 5]) = 5, and so on.  We can thus calculate any Fibonacci number, and hence the number of adult pairs of rabbits after any number of months.  
It would be useful, however, to have a formula for (Subscript[F, n]) that depends only on n, and not on the two previous Fibonacci numbers.  Such a formula can be obtained by solving the recurrence relation (1a) with initial values (1b), and this can be  done with the RSolve command, which is part of the DiscreteMath package.  We first load the command and then use it to solve (1a, b).

<<DiscreteMath`RSolve`

?RSolve

sol = RSolve[{F[n] == F[n-1] + F[n-2], F[0] == 0, F[1] == 1}, F[n], n]

fibtemp[n_] = F[n] /. First[sol]

fibtemp[2]

Though fibtemp[n] is the correct formula, it does not present its value in simplified form. We thus modify fibtemp by incorporating the Simplify command using delayed evaluation:

fib[n_]  :=  Simplify[fibtemp[n]]

We illustrate the use of fib by computing a few values.

fib[0]
fib[3]
fib[5]

We see that fib produces the same values as the Fibonacci command.

Calculation Via Powers of a Matrix

The recurrence relation (1a, b) can be conveniently expressed in vector-matrix form.  Introduce the matrix A = (({{1, 1}, {1, 0}}))   and the vector (Subscript[u, n]) = (({{(Subscript[F, n + 1])}, {(Subscript[F, n])}})) , and consider the vector-matrix recurrence relation
(2a)      (({{(Subscript[F, n + 1])}, {(Subscript[F, n])}})) =   (Subscript[u, n]) = A(Subscript[u, n - 1]) = (({{1, 1}, {1, 0}})) (({{(Subscript[F, n])}, {(Subscript[F, n - 1])}})), for all n ≥ 1.
Carrying out the matrix multiplication, we see that (2a) reduces to the two equations (Subscript[F, n + 1]) = (Subscript[F, n])  + (Subscript[F, n - 1]) and (Subscript[F, n]) = (Subscript[F, n]).  The first of these equations is the recurrence relation (1a) and the second is automatically true.  So (2a) is just a restatement of (1a). The vector form of the initial values (1b) is
 (2b)      (Subscript[u, 0]) = (({{(Subscript[F, 1])}, {(Subscript[F, 0])}})) = (({{1}, {0}})).
 Using (2a) repeatedly,  together with (2b), we see that (Subscript[u, 1]) = A(Subscript[u, 0]),  (Subscript[u, 2]) = A(Subscript[u, 1]) = A(A(Subscript[u, 0])) = (A^2)(Subscript[u, 0]), and in general
 (3)       (Subscript[u, n]) =(A^n)(Subscript[u, 0]), for all n ≥ 1.
That is,  (Subscript[u, n]) = (A^n)(Subscript[u, 0]) is the solution of the vector-matrix recurrence relation (2a, b).  So, we can calculate (Subscript[F, n]) by using (3) to calculate (Subscript[u, n]), and then finding (Subscript[F, n]) in the second component of (Subscript[u, n]).  Our problem has been reduced to calculating powers of A, and multiplying (Subscript[u, 0]) by  them. Let's carry out this procedure.

A = {{1,1},{1,0}}

To write A in usual matrix notation, use the MatrixForm command.

MatrixForm[A]

u0 = {1, 0}

Powers of a matrix are calculated with the MatrixPower command, and not with A^n.  We check the online help for MatrixPower, redefine fib using powers of A, and then compute a few Fibonacci numbers.

?MatrixPower

Clear[fib]

fib[n_]  :=  Last[MatrixPower[A, n] . u0]

The use of Last extracts the second component of the vector (A^n)(Subscript[u, 0]).  Note that matrix-vector multiplication is indicated by . rather than by *.

fib[1]
fib[5]
fib[30]

Using Eigenpairs to Calculate (A^n)

Powers of a matrix can be readily calculated if we know the eigenpairs of the matrix.  Recall that a pair (λ, x) consisting of a number λ and a vector x is an eigenpair of A if Ax = λx and x ≠ 0. The number λ is called an eigenvalue of A and the vector x is called a corresponding eigenvector.  The eigenpairs of A are found with the Eigensystem command.

vals = Eigensystem[A]

The output is a nested list containing first a list of the eigenvalues, and then a list of the corresponding eigenvectors. The eigenvalues are

λ = First[First[vals]]

and

μ = Last[First[vals]]

The corresponding eigenvectors are

x = First[Last[vals]]

and

y = Last[Last[vals]]

The importance of eigenvectors in our situation is that powers of A act on them in a very simple manner.  From the definition of eigenpairs we know that Ax = λx, and from this we learn that (A^2)x =A(λx) = λ(Ax) = (λ^2)x.  In general, we have (A^n)x = (λ^n)x.  Likewise we have (A^n)y = (μ^n)y.  So a power of A acts on an eigenvector by multiplying the eigenvector by the corresponding eigenvalue raised to the power.  But we are interested in (A^n)(Subscript[u, 0]) = (A^n)(({{1}, {0}})).  This can also be calculated, by first expressing the vector (Subscript[u, 0]) as a linear combination of x and y (the eigenvectors are a basis for two dimensional vectors), and applying (A^n) to this linear combination.  To express (Subscript[u, 0]) as a linear combination of x and y we find coefficients a and b \(TraditionalForm\`\(
 \)\)so that 
		(Subscript[u, 0]) = ax + by.
 It is easily seen that c = (({{a}, {b}})) is the solution of the linear system mc  = (Subscript[u, 0]), where

m = {{λ, μ}, {1, 1}}

So

c = LinearSolve[m, u0]

As a check, let's multiply m by c.

m.c

Simplify[%]

So (Subscript[u, 0])= (({{1}, {0}})) = (-1/(Sqrt[5]))x + (1/(Sqrt[5]))y,  and thus
 		(Subscript[u, n ]) = (A^n)(Subscript[u, 0]) = (-1/(Sqrt[5])) (λ^n)x + (1/(Sqrt[5]))(μ^n)y.  
We have reduced the calculation of (A^n)(Subscript[u, 0]) to the calculation of (λ^n)and (μ^n).  Since (Subscript[F, n])is the second component of (Subscript[u, n]) we see that
 (4)       (Subscript[F, n]) =  (-1/(Sqrt[5])) (λ^n) + (1/(Sqrt[5]))(μ^n)
                   =  (-1/(Sqrt[5]))((((1  -  (Sqrt[5]))/2 ))^n) + (1/(Sqrt[5]))((((1   +  (Sqrt[5]) )/2))^n).               
We have thus derived the formula produced by RSolve.               
We have one further formula.  Note that

N[λ]
N[μ]

Since |λ| < 1, we see that (λ^n) is small for n large, and hence that the first term in formula (4)  is small.  Thus (Subscript[F, n]) is approximately equal to the second term in (4).  But, since (Subscript[F, n]) is an integer, it must be the nearest integer to the second term.  Let's compute these terms for n = 3.

N[(-1/Sqrt[5])λ^3]

N[(1/Sqrt[5])μ^3]

We see that (Subscript[F, 3 ]) is 2, the closest integer to the second term in (4).  The closest integer can be obtained with the Round command.

Clear[fib]

fib[n_] := Round[(1/Sqrt[5])((1+Sqrt[5])/2)^n]

We illustrate the use of this formula by computing a couple of Fibonacci numbers.

fib[30]
fib[100]

For further information on Fibonacci numbers, see V. E. Hoggatt, Jr., Fibonacci and Lucas Numbers, Houghton Mifflin Company, 1969.