Fibonacci Numbers

[Graphics:fibonaccigr1.gif]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 [Graphics:fibonaccigr2.gif], then we want to find or calculate [Graphics:fibonaccigr3.gif]. The first few Fibonacci numbers are easy to calculate by following the generations of our rabbit colony month-by-month; we find that [Graphics:fibonaccigr4.gif]= 0, [Graphics:fibonaccigr5.gif] = 1, [Graphics:fibonaccigr6.gif] = 1, [Graphics:fibonaccigr7.gif] = 2, and [Graphics:fibonaccigr8.gif] = 3. We would like, however, to have a simple, systematic way to find [Graphics:fibonaccigr9.gif] for any n; we will, in fact, discuss several ways to calculate [Graphics:fibonaccigr10.gif].

The Built-in Function Fibonacci

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

?Fib*
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr11.gif]

So we see that Fibonacci[n] gives [Graphics:fibonaccigr13.gif] We illustrate the use of this command.

Fibonacci[0]
Fibonacci[1]
Fibonacci[2]
Fibonacci[3]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr14.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr15.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr16.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr17.gif]

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

Fibonacci[5]
Fibonacci[30]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr18.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr19.gif]

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}]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr20.gif]

TableForm[fibtable, TableDirections -> {Row, Column}, TableHeadings -> {None, {"n", "F[n]"}}, TableSpacing ->{1,1}]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr21.gif]

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 [Graphics:fibonaccigr22.gif]'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 [Graphics:fibonaccigr23.gif]denote the number of adult pairs after n months, we can state this principle as
(1a) [Graphics:fibonaccigr24.gif] = [Graphics:fibonaccigr25.gif] + [Graphics:fibonaccigr26.gif], for all n >= 2.
This formula, which is called a recurrence relation, together with the values
(1b) [Graphics:fibonaccigr27.gif] = 0, [Graphics:fibonaccigr28.gif] = 1,
which are called initial values for the sequence [Graphics:fibonaccigr29.gif], provides a procedure for calculating [Graphics:fibonaccigr30.gif]for any n: [Graphics:fibonaccigr31.gif]= 3, [Graphics:fibonaccigr32.gif]= 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 [Graphics:fibonaccigr33.gif] 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`

[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr34.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr35.gif]

sol = RSolve[{F[n] == F[n-1] + F[n-2], F[0] == 0, F[1] == 1}, F[n], n]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr36.gif]

fibtemp[n_] = F[n] /. First[sol]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr37.gif]

fibtemp[2]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr38.gif]

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:

[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr39.gif]

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

fib[0]
fib[3]
fib[5]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr40.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr41.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr42.gif]

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 =[Graphics:fibonaccigr43.gif][Graphics:fibonaccigr44.gif] and the vector [Graphics:fibonaccigr45.gif] = [Graphics:fibonaccigr46.gif] , and consider the vector-matrix recurrence relation
(2a) [Graphics:fibonaccigr47.gif] = [Graphics:fibonaccigr48.gif] = A[Graphics:fibonaccigr49.gif] =[Graphics:fibonaccigr50.gif] [Graphics:fibonaccigr51.gif], for all n >= 1.
Carrying out the matrix multiplication, we see that (2a) reduces to [Graphics:fibonaccigr52.gif] = [Graphics:fibonaccigr53.gif] + [Graphics:fibonaccigr54.gif] and [Graphics:fibonaccigr55.gif] = [Graphics:fibonaccigr56.gif]. 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) [Graphics:fibonaccigr57.gif] = [Graphics:fibonaccigr58.gif] = [Graphics:fibonaccigr59.gif].
Using (2a) repeatedly, together with (2b), we see that [Graphics:fibonaccigr60.gif] = A[Graphics:fibonaccigr61.gif][Graphics:fibonaccigr62.gif] = A[Graphics:fibonaccigr63.gif] = A(A[Graphics:fibonaccigr64.gif]) = [Graphics:fibonaccigr65.gif][Graphics:fibonaccigr66.gif], and in general
(3) [Graphics:fibonaccigr67.gif] =[Graphics:fibonaccigr68.gif][Graphics:fibonaccigr69.gif], for all n >= 1.
That is, [Graphics:fibonaccigr70.gif] = [Graphics:fibonaccigr71.gif][Graphics:fibonaccigr72.gif] is the solution of the vector-matrix recurrence relation (2a, b). So, we can calculate [Graphics:fibonaccigr73.gif] by using (3) to calculate [Graphics:fibonaccigr74.gif]and then [Graphics:fibonaccigr75.gif] in the second component of [Graphics:fibonaccigr76.gif]. Our problem has been reduced to calculating powers of A, and multiplying [Graphics:fibonaccigr77.gif]. Let's carry out this procedure.

A = {{1,1},{1,0}}  
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr78.gif]

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

MatrixForm[A]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr79.gif]

[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr80.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr81.gif]

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
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr82.gif]

Clear[fib]

[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr83.gif]

The use of Last extracts the second component of the vector [Graphics:fibonaccigr84.gif][Graphics:fibonaccigr85.gif]. Note that matrix-vector multiplication is indicated by . rather than by *.

fib[1]
fib[5]
fib[30]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr86.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr87.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr88.gif]

Using Eigenpairs to Calculate [Graphics:fibonaccigr89.gif]

Powers of a matrix can be readily calculated if we know the eigenpairs of the matrix. Recall that a pair (&lgr;, x) consisting of a number &lgr; and a vector x is an eigenpair of A if Ax = &lgr;x and x ~= 0. The number &lgr; 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]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr90.gif]

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

&lgr; = First[First[vals]]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr91.gif]

and

&mgr; = Last[First[vals]]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr92.gif]

The corresponding eigenvectors are

x = First[Last[vals]]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr93.gif]

and

y = Last[Last[vals]]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr94.gif]

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 = &lgr;x, and from this we learn that [Graphics:fibonaccigr95.gif]x =A(&lgr;x) = &lgr;(Ax) = [Graphics:fibonaccigr96.gif]x. In general, we have [Graphics:fibonaccigr97.gif]x = [Graphics:fibonaccigr98.gif]x. Likewise we have [Graphics:fibonaccigr99.gif]y = [Graphics:fibonaccigr100.gif]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 [Graphics:fibonaccigr101.gif][Graphics:fibonaccigr102.gif] = [Graphics:fibonaccigr103.gif][Graphics:fibonaccigr104.gif]. This can also be calculated, by first expressing the vector [Graphics:fibonaccigr105.gif]as a linear combination of x and y (the eigenvectors are a basis for two dimensional vectors), and applying [Graphics:fibonaccigr106.gif] to this linear combination. To express [Graphics:fibonaccigr107.gif]s a linear combination of x and y we find coefficients a and b [Graphics:fibonaccigr108.gif]so that
[Graphics:fibonaccigr109.gif] = ax + by.
It is easily seen that c = [Graphics:fibonaccigr110.gif] is the solution of the linear system mc = [Graphics:fibonaccigr111.gif], where

m = {{&lgr;, &mgr;}, {1, 1}} 
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr112.gif]

So

[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr113.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr114.gif]

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

m.c 
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr115.gif]

Simplify[%]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr116.gif]

So [Graphics:fibonaccigr117.gif][Graphics:fibonaccigr118.gif] = (-1/[Graphics:fibonaccigr119.gif])x + (1/[Graphics:fibonaccigr120.gif])y, and thus
[Graphics:fibonaccigr121.gif] = [Graphics:fibonaccigr122.gif][Graphics:fibonaccigr123.gif] = (-1/[Graphics:fibonaccigr124.gif]) [Graphics:fibonaccigr125.gif]x + (1/[Graphics:fibonaccigr126.gif])[Graphics:fibonaccigr127.gif]y.
We have reduced the calculation of [Graphics:fibonaccigr128.gif][Graphics:fibonaccigr129.gif]to the calculation of [Graphics:fibonaccigr130.gif]and [Graphics:fibonaccigr131.gif]. Since [Graphics:fibonaccigr132.gif]is the second component of [Graphics:fibonaccigr133.gif] we see that
(4) [Graphics:fibonaccigr134.gif] = (-1/[Graphics:fibonaccigr135.gif]) [Graphics:fibonaccigr136.gif] + (1/[Graphics:fibonaccigr137.gif])[Graphics:fibonaccigr138.gif]
= (-1/[Graphics:fibonaccigr139.gif])[Graphics:fibonaccigr140.gif] + (1/[Graphics:fibonaccigr141.gif])[Graphics:fibonaccigr142.gif].
We have thus derived the formula produced by RSolve.
We have one further formula. Note that

N[&lgr;]
N[&mgr;]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr143.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr144.gif]

Since ö&lgr;ö < 1, we see that [Graphics:fibonaccigr145.gif] is small for n large, and hence that the first term in formula (4) is small. Thus [Graphics:fibonaccigr146.gif] is approximately equal to the second term in (4). But, since [Graphics:fibonaccigr147.gif] 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])&lgr;^3]   
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr148.gif]

N[(1/Sqrt[5])&mgr;^3]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr149.gif]

We see that [Graphics:fibonaccigr150.gif] 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]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr151.gif]
[Graphics:fibonaccigr12.gif][Graphics:fibonaccigr152.gif]

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