MATLAB Guide To Fibonacci Numbers and The Golden Ratio: A Simplified Approach
MATLAB Guide To Fibonacci Numbers and The Golden Ratio: A Simplified Approach
net/publication/301358476
CITATION READS
1 6,478
1 author:
Peter I. Kattan
Researcher
183 PUBLICATIONS 2,471 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
All content following this page was uploaded by Peter I. Kattan on 27 April 2016.
A Simplified Approach
Peter I. Kattan
Petra Books
www.PetraBooks.com
Peter I. Kattan, PhD
Contents
Part I: Review of MATLAB 9
1. Introduction ……………………………………………… 11
2. Symbolic Computing 21
3. Solving Equations 27
4. MATLAB Functions 39
5. Graphs in MATLAB 53
6. Fibonacci Numbers 71
Part I
Review of MATLAB
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 10
1. Introduction
In this introductory chapter a short MATLAB tutorial is
provided. This tutorial describes the basic MATLAB commands
needed. Note that there are numerous free MATLAB tutorials on the
internet – check references [1-28]. Also, you may want to consult
many of the excellent books on the subject – check references [29-
47]. This chapter may be skipped on a first reading of the book.
>> 2*3+5
ans =
11
>> cos(60*pi/180)
ans =
0.5000
>> x = 4
x =
>> 3/sqrt(2+x)
ans =
1.2247
>> y = 30;
>> z = 8;
>> x = 2; y-z;
>> w = 4*y + 3*z
w =
144
>> x = 1
x =
1
>> X = 2
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 13
X =
>> x
x =
>> X
X =
>> x = [1 2 3 ; 4 5 6 ; 7 8 9]
x =
1 2 3
4 5 6
7 8 9
>> y = [1 ; 0 ; -4]
y =
1
0
-4
>> w = x*y
w =
-11
-20
-29
⎡ 3 5 − 1⎤ ⎧ x1 ⎫ ⎧ 2 ⎫
⎢ 0 4 2 ⎥⎪x ⎪ = ⎪ 1 ⎪ (1.1)
⎢ ⎥⎨ 2 ⎬ ⎨ ⎬
⎢⎣− 2 1 5 ⎥⎦ ⎪⎩ x3 ⎪⎭ ⎪⎩− 4⎪⎭
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 15
>> A= [3 5 -1 ; 0 4 2 ; -2 1 5]
A =
3 5 -1
0 4 2
-2 1 5
>> b = [2 ; 1 ; -4]
b =
2
1
-4
>> x = A\b
x =
-1.7692
1.1154
-1.7308
x =
-1.7692
1.1154
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 16
-1.7308
>> D = [1 2 3 4 ; 2 4 6 8 ; 3 6 9 12 ; -5 -3
-1 0]
D =
1 2 3 4
2 4 6 8
3 6 9 12
-5 -3 -1 0
E =
2 4 6
3 6 9
-5 -3 -1
F =
3
6
9
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 17
-1
G =
2 4 6 8
>> H = D(3,2)
H =
>> x = [1 2 3 4 5 6 7 8 9 10 11]
x =
Columns 1 through 10
1 2 3 4 5 6 7
8 9 10
Column 11
11
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 18
>> y = x.^2 + 3
y =
Columns 1 through 10
4 7 12 19 28 39 52
67 84 103
Column 11
124
>> plot(x,y)
>> magic(5)
ans =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
It is clear that the total of each row, column or diagonal in the above
matrix is 65.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 20
2. Symbolic Computing
In this chapter, the basics of symbolic computing are
illustrated with MATLAB. Symbolic computing means performing
algebraic and other mathematical operations without resorting to
numerical computations. This type of computing is also called
computer algebra. Computer programs and software that are used in
symbolic computations are called computer algebra systems. Many
such competing systems are available today such as MAPLE,
MATHEMATICA, MACZYMA, etc. It should be noted that
MATLAB is not basically a computer algebra system but can be used
for this purpose with the aid of the MATLAB Symbolic Math
Toolbox. This toolbox uses the MAPLE engine and helps in
converting MATLAB into a computer algebra system.
>> sym(1/2)
ans =
1Actually these are symbolic objects in MATLAB but objects are beyond the scope
of this book.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 22
1/2
>> 1/2+3/5
ans =
1.1000
>> (1/2)+(3/5)
ans =
1.1000
>> sym((1/2)+(3/5))
ans =
11/10
Notice in the above example how the final answer was cast in
the form of a fraction without decimal digits and without calculating
a numerical value. In the addition of the two fractions above,
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 23
>> x=1/2
x =
0.5000
>> y= 2/3
y =
0.6667
>> sym(x)
ans =
1/2
>> sym(y)
ans =
2/3
2Actually these are symbolic objects in MATLAB but objects are beyond the scope
of this book.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 24
>> z=sym(x-y)
z =
-1/6
>> r=15/7
r =
2.1429
>> sym(r)
ans =
15/7
volume =
4500*pi/343
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 25
>> double(volume)
ans =
41.2162
>> x = sym('x')
x =
this =
this
3. Solving Equations
In this chapter we discuss how to solve algebraic equations
using MATLAB. We will discuss both linear and nonlinear algebraic
equations. We will also discuss systems of simultaneous linear and
nonlinear algebraic equations. First, let us solve the following simple
linear equation for the variable x:
2x − 3 = 0
p ( x) = 2 x − 3 = 0
>> p = [2 -3]
p =
2 -3
>> roots(p)
ans =
1.5000
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 28
5 x 2 + 3x − 4 = 0
>> p = [5 3 -4]
p =
5 3 -4
>> roots(p)
ans =
-1.2434
0.6434
It is clear from the above that two real solutions are obtained
for x. Let us now solve a more complicated equation. Let us solve the
following equation for x:
2 x5 − 3x 4 − 5 x3 + x 2 − 1 = 0
>> p = [2 -3 -5 1 0 -1]
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 29
p =
2 -3 -5 1 0 -1
>> roots(p)
ans =
2.4507
-1.0000
-0.6391
0.3442 + 0.4480i
0.3442 - 0.4480i
Note from the above that five values are obtained as the
solution of the above equation. These values are the five roots of the
polynomial given above. Note that three of these roots are real while
two roots are complex conjugates of each other.
x − 3y = 5
4x + 6 y = 3
⎡1 − 3⎤ ⎧ x ⎫ ⎧5⎫
⎢4 6 ⎥ ⎨ y ⎬ = ⎨3⎬
⎣ ⎦⎩ ⎭ ⎩ ⎭
inverse of the coefficient matrix [A] and multiply it with the constant
vector {b} . Here are the needed MATLAB commands along with the
solution:
>> A = [1 -3 ; 4 6]
A =
1 -3
4 6
>> b = [5 ; 3]
b =
5
3
>> x = inv(A)*b
x =
2.1667
-0.9444
a 2x2 matrix. In this case, it was quick to find this inverse because the
coefficient matrix was small. But for larger matrices, finding the
inverse using MATLAB may take more time. Therefore, it is advised
to use another method to solve the above system. One such method
that is rather fast in execution is called Gaussian elimination. This
method is already implemented in MATLAB as matrix division using
the backslash operator “\”. Here is the solution of the above system
again using Gaussian elimination and the backslash operator:
>> A = [1 -3 ; 4 6]
A =
1 -3
4 6
>> b = [5 ; 3]
b =
5
3
>> x = A\b
x =
2.1667
-0.9444
2 x1 − 4 x2 − x3 + 3x4 − x5 = 3
x1 + x2 − 2 x3 + x5 = 6
− x1 − 3x2 + x4 + 3x5 = −4
3x1 − x2 − x3 + 4 x4 − x5 = 1
x1 + x2 − x3 + 2 x4 = 5
⎡ 2 − 4 − 1 3 − 1⎤ ⎧ x1 ⎫ ⎧ 3 ⎫
⎢1
⎢ 1 − 2 0 1 ⎥⎥ ⎪⎪ x2 ⎪⎪ ⎪⎪ 6 ⎪⎪
⎪ ⎪ ⎪ ⎪
⎢− 1 − 3 0 1 3 ⎥ ⎨ x3 ⎬ = ⎨− 4⎬
⎢ ⎥⎪ ⎪ ⎪ ⎪
⎢ 3 − 1 − 1 4 − 1⎥ ⎪ x4 ⎪ ⎪ 1 ⎪
⎢⎣ 1 1 − 1 2 0 ⎥⎦ ⎪⎩ x5 ⎪⎭ ⎪⎩ 5 ⎪⎭
>> A = [2 -4 -1 3 -1 ; 1 1 -2 0 1 ; -1 -3 0
1 3 ; 3 -1 -1 4 -1 ; 1 1 -1 2 0]
A =
2 -4 -1 3 -1
1 1 -2 0 1
-1 -3 0 1 3
3 -1 -1 4 -1
1 1 -1 2 0
>> b = [3 ; 6 ; -4 ; 1 ; 5]
b =
3
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 33
6
-4
1
5
>> x = A\b
x =
-4.3571
0.3571
-6.4286
1.2857
-2.8571
It is seen from the above result that five real solutions are
obtained for the above system of linear equations.
ax + b = 0
>> syms x
>> syms a
>> syms b
>> x = solve('a*x+b = 0')
x =
-b/a
ax 2 + bx + c = 0
>> syms x
>> syms a
>> syms b
>> syms c
>> x = solve('a*x^2 + b*x + c = 0')
x =
1/2/a*(-b+(b^2-4*a*c)^(1/2))
1/2/a*(-b-(b^2-4*a*c)^(1/2))
e x = sin x
>> syms x
>> x = solve('exp(x)-sin(x) = 0')
x =
.36270205612105111082838863639609-
1.1337459194137525371167081219057*i
It is clear from the above that we obtained one complex
solution of the above equation. The resulting solution is
5 We can use the MATLAB command pretty to display the solution in a nice
way but this will not be done here. The correct use of the pretty command in
the example above would be pretty(x).
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 36
ax − by = c
2ax + b = 9c
>> syms x
>> syms y
>> syms a
>> syms b
>> syms c
>> [x,y] = solve('a*x - b*y - c = 0', '2*a*x
+ b - 9*c = 0')
x =
-1/2/a*(b-9*c)
y =
-1/2*(-7*c+b)/b
x 3 − xy = −3
x2 − 2 y 2 = 5
>> syms x
>> syms y
>> [x,y] = solve('x^3 - x*y + 3 = 0', 'x^2 -
2*y^2 - 5 = 0')
x =
1.1146270688593392975541180081115+1.36934983
26300382040739329085074*i
.36979370624989001773301777958373+1.06182311
95136687011590573146631*i
-
1.4844207751092293152871357876952+.282946327
30294451032613534596558*i
-1.4844207751092293152871357876952-
.28294632730294451032613534596558*i
.36979370624989001773301777958373-
1.0618231195136687011590573146631*i
1.1146270688593392975541180081115-
1.3693498326300382040739329085074*i
y =
.43988651431894061086040911013399+1.73489563
84245835120290450101938*i
-.11319578028438869117106079077457-
1.7344087640028938716691238072308*i
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 38
.1733092659654480803106516806407-
1.2117396151602258910076501748134*i
.1733092659654480803106516806407+1.211739615
1602258910076501748134*i
-
.11319578028438869117106079077457+1.73440876
40028938716691238072308*i
.43988651431894061086040911013399-
1.7348956384245835120290450101938*i
4. MATLAB Functions
In this chapter we introduce the basics of programming in
MATLAB. The material presented in this chapter is not intended to
be comprehensive or exhaustive but merely an introduction6 to
programming in MATLAB. In MATLAB programming, the list of
commands and instructions are usually stored in a text file called an
M-file (short for MATLAB file). These M-files can be of two kinds –
either script files or function files. These files can be created or
opened from the File menu by clicking on New or Open, then
clicking on M-File. These files typically have the .m extension that
is associated with MATLAB.
% This is an example
cost = 50
profit = 10
sale_price = cost + profit
Note that the first line in the script above is a comment line –
this is optional. Store the above example in an M-file called
example1.m then run the example by typing example1 at the
command line to get:
>> example1
cost =
6The interested reader may consult the references listed at the end of this book for
more details about programming in MATLAB.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 40
50
profit =
10
sale_price =
60
% This is an example
cost = 50;
profit = 10;
sale_price = cost + profit
>> example2
sale_price =
60
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 41
function area(r)
% This is a function
area = pi*r^2
>> area(3)
area =
28.2743
7 This means that there are rules for writing functions in MATLAB.
8 The name of the function must be exactly the same as the name of the function
file it is stored in.
9 Consult a book on programming for more details about functions and return
>> area(5)
area =
78.5398
function perimeter(a,b)
% This is another example of a function
perimeter = 2*(a+b)
>> perimeter(2,3)
perimeter =
10
>> perimeter(3,7)
perimeter =
20
>> perimeter(1.2,5.3)
perimeter =
13
12Note that no units are specified for the dimensions used in this example.
13It should be noted that these programming constructs are also available in other
programming languages but they have a different structure, i.e. different rules for
their implementation.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 44
>> example3
> x
x =
1 4 9 16 25 36 49
64 81 100
It is seen from the above example that the For loop cycles
over the values of n from 1 to 10 – exactly 10 times. In each cycle,
the value of the vector x is calculated using the quadratic formula
inside the For loop. The final result for the vector x is displayed
above – it is seen that the vector x has exactly 10 elements.
Actually the above example contains two For loops that are
nested. Now, run the above script file by typing example4 at the
command prompt as follows, then type y to get the values of the
elements of the matrix y:
>> example4
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 45
>> y
y =
3 7 13
7 12 19
13 19 27
>> example5
>> n
n =
>> tol
tol =
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 46
10
It is seen from the above example that the loop will continue
to cycle as long as the value of the variable tol is less than14 10. As
soon as the value of tol becomes equal to or larger than 10, the
loop ends. The final values of the variables are then displayed by
MATLAB with the appropriate commands. Note also the use of the
semicolons in the script file.
>> example6
>> cost
14 The logical operator “less than” is represented in MATLAB by the symbol <. We
do not cover logical operators in this book but this is an instance where their use is
needed.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 47
cost =
25
function cost(boxes)
% This is an example of an If construct
cost = boxes*3
if boxes > 7
cost = boxes*2.5
end
>> cost(4)
cost =
12
>> cost(6)
cost =
18
>> cost(8)
cost =
24
cost =
20
It is seen that the values of the variable cost for the first
two executions were obtained directly using the first assignment
statement for cost – this is because the value of the variable
boxes is less than or equal to 7 in these two cases. However, for
the third execution, the value of cost was calculated initially to be
24 but changed to 20 finally and correctly because the value of the
variable boxes is larger than 7 in this last case.
15
Note that there other variations of the above constructs used for implementing
decisions in MATLAB, e.g. the If Else construct (see the last section in this
chapter on the Symbolic Math Toolbox to see an example of using this construct).
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 49
function cost2(boxes)
% This is an example of an If Elseif construct
if boxes < 7
cost = boxes*2.5
elseif boxes < 9
cost = boxes*2
elseif boxes > 9
cost = boxes*1.5
end
>> cost2(6)
cost =
15
>> cost2(8)
cost =
16
>> cost2(10)
cost =
15
function cost3(boxes)
% This is an example of the Switch Case construct
switch boxes
case 6
cost = boxes* 2.5
case 8
cost = boxes* 2
case 10
cost = boxes*1.5
otherwise
cost = 0
end
>> cost3(6)
cost =
15
>> cost3(8)
cost =
16
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 51
>> cost3(10)
cost =
15
5. Graphs in MATLAB
In this chapter we explain how to plot graphs in MATLAB. Both
two-dimensional and three-dimensional graphs are presented. A
graph in MATLAB appears in its own window (not on the command
line). First, we will consider two-dimensional or planar graphs. To
plot a two-dimensional graph, we need two vectors. Here is a simple
example using two vectors x and y along with the MATLAB
command plot:
>> x = [1 2 3 4 5]
x =
1 2 3 4 5
>> y = [3 9 12 10 6]
y =
3 9 12 10 6
>> plot(x,y)
keep the plotted graph in its window. For this we use the hold on
command as follows:
>> hold on
>> title('This is an example of a simple graph')
>> xlabel('x')
>> ylabel('y')
>> x = [-3 -2 -1 0 1 2 3]
x =
-3 -2 -1 0 1 2 3
>> y = x.^2 -3
y =
6 1 -2 -3 -2 1 6
>> plot(x,y)
>> hold on
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 56
It is seen from the above graphs that they are plotted with
solid lines. The line style may be changed from solid to dotted,
dashed, or dash-dot lines by changing the parameters in the plot
command. Use the command plot(x,y,’:’) to get a dotted
line, the command plot(x,y,’--‘) to get a dashed line, the
command plot(x,y,’-.’) to get a dash-dot line, and the
default command plot(x,y,’-‘) to get a solid line.
list of the color options. Note that colors are not displayed in this
book.
The above options for line style, color, and symbols can be
combined in the same command. For example use the command
plot(x,y,’rx:’) to get a red dotted line with cross symbols.
Here is the previous example with a red dotted line with cross
symbols but without the title and label information (note that no
colors appear in this book). See Figure 9.4 for the resulting graph.
>> plot(x,y,'rx:')
>> x = 0:pi/20:2*pi;
>> y = sin(x);
>> z = cos(x);
16
These commands may be entered individually on the command prompt or stored
as a script in a script file. See Chapter 4 for details about scripts and script files.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 58
>> plot(x,y,'-',x,z,'--')
>> hold on;
>> grid on;
>> title('Sine and Cosine Functions')
>> xlabel('x-axis')
>> ylabel('sine and cosine')
>> legend('sinx','cosx')
available from the menus of the graph window – the window where
the graph appears.
17 Several logarithmic plots can also be generated by using the MATLAB command
>> x = [ 1 2 3 4 5 6 7 8 9 10];
>> y = x.^2;
>> z = sqrt(x);
>> w = 2*x - 3;
>> v = x.^3;
>> subplot(2,2,1)
>> hold on;
>> plot(x,y)
>> subplot(2,2,2)
>> plot(x,z)
>> subplot(2,2,3)
>> plot(x,w)
>> subplot(2,2,4)
>> plot(x,v)
>> x = [1 2 3 4 5 6 7 8 9 10]
x =
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 62
1 2 3 4 5 6 7
8 9 10
>> y = [1 2 3 4 5 6 7 8 9 10]
y =
1 2 3 4 5 6 7
8 9 10
>> z = sin(x).*cos(y)
z =
Columns 1 through 6
Columns 7 through 10
>> plot3(x,y,z)
>> hold on;
>> grid on;
>> A = [10 14 20 37 5 17 11 ; 12 20 40 20 11
5 14 ; 30 51 12 17 20 30 2 ; 24 34 56 10 14
5 40 ; 34 12 33 12 26 10 15 ; 12 45 13 23 35
10 7 ; 10 20 13 34 32 10 7]
A =
10 14 20 37 5 17 11
12 20 40 20 11 5 14
30 51 12 17 20 30 2
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 64
24 34 56 10 14 5 40
34 12 33 12 26 10 15
12 45 13 23 35 10 7
10 20 13 34 32 10 7
>> mesh(A)
>> surf(A)
>> contour(A)
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 66
>> surfc(A)
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 67
Part II
Fibonacci Numbers
and
The Golden Ratio
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 70
6. Fibonacci Numbers
The Fibonacci sequence is a sequence of numbers where each
term of the sequence is obtained by adding the previous two terms.
In addition, this special sequence starts with the numbers 1 and 1.
Thus, the whole Fibonacci sequence can be generated starting with
these two numbers and the above special rule. The numbers in this
sequence are called Fibonacci numbers. These numbers were first
obtained by the Italian mathematician Leonardo of Pisa (also called
Leonardo Fibonacci) in the thirteenth century.
>> 1 + 1
ans =
>> 2 + 1
ans =
19
The Fibonacci sequence can also be started with the numbers 0 and 1 instead of
1 and 1. Similar results will be obtained in this case.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 72
>> 3 + 2
ans =
>> 5 + 3
ans =
>> 8 + 5
ans =
13
>> 13 + 8
ans =
21
>> 21 + 13
ans =
34
>> 34 + 21
ans =
55
>> 55 + 34
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 73
ans =
89
>> 89 + 55
ans =
144
>> 144 + 89
ans =
233
function f=fibonacci(n)
% This function generates the first n
% Fibonacci numbers
f = zeros(n,1);
f(1)=1;
f(2)=2;
for k = 3:n
f(k) = f(k-1) + f(k-2);
end
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 74
>> fibonacci(7)
ans =
1
2
3
5
8
13
21
>> fibonacci(15)
ans =
1
2
3
5
8
13
21
34
55
89
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 75
144
233
377
610
987
>> fibonacci(30)
ans =
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
28657
46368
75025
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 76
121393
196418
317811
514229
832040
1346269
>> y = fibonacci(15);
>> y(15)
ans =
987
F(1) = 1
F(2) = 1
n = 3, 4, 5, …..
>> x = 1:10
x =
1 2 3 4 5 6 7
8 9 10
>> y = fibonacci(10)
y =
1
2
3
5
8
13
21
34
55
89
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 79
>> plot(x,y)
>> hold on
>> xlabel('n')
>> ylabel('fibonacci(n)')
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 80
>> x = 1:30;
>> y = fibonacci(30);
>> plot(x,y)
>> hold on;
>> xlabel('n')
>> ylabel('fibonacci(n)')
its exact property that makes this sequence special – this would be its
relationship to what is called the Golden Ratio.
>> log(fibonacci(10))
ans =
0
0.6931
1.0986
1.6094
2.0794
2.5649
3.0445
3.5264
4.0073
4.4886
>> x = 1:10;
20The logarithms calculated here are the natural logarithms, which are logarithms
to the base e , where e = 2.71828….
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 82
>> y = log(fibonacci(10));
>> plot(x,y)
>> hold on
>> xlabel('n')
>> ylabel('log(F(n))')
>> m = (y(10)-y(5))/(x(10)-x(5))
m =
21The slope m of a line segment between two points (x1, y1) and (x2, y2) is
calculated using the formula m = (y2 – y1)/ (x2 – x1).
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 83
0.4818
Let us perform the above operations again but using the first
30 Fibonacci numbers. Here are the MATLAB commands to
generate the plot while the graph is shown in Figure 6.4.
>> x = 1:30;
>> y = log(fibonacci(30));
>> plot(x,y)
>> hold on
>> xlabel('n')
>> ylabel('log(F(n))')
>> m = (y(30)-y(10))/(x(30)-x(10))
m =
0.4812
25, … . This sequence follows the law of exponential growth. See the
books in references [48-56] and the web links [57-67].
Let us start with one rabbit. After one cycle, this rabbit
produces a new rabbit – the total becomes two. After the second
cycle, the old rabbit produces another new rabbit but the little rabbit
skips this cycle and does not produce any new rabbits. The total now
becomes three rabbits. After another cycle, the two old rabbits
produce two new rabbits but the little one skips this cycle and does
not produce. Thus the total now becomes five rabbits following the
Fibonacci sequence precisely. Thus, we obtain the numbers 1, 2, 3, 5,
8, 13, 21, …. for the total number of rabbits. This is exactly what
Leonardo Fibonacci originally observed and is what started the
interesting topic of Fibonacci numbers. See the books in references
[48-56] and the web links [57-67].
Let us start by calculating the ratio of the first two terms. The
first two terms are 1 and 1; their ratio is 1/1 = 1. The next two
consecutive terms are 1 and 2; their ratio is 2/1 = 2. Next are the
numbers 2 and 3; their ratio is 3/2 = 1.5. The ratio of 3 and 5 is 5/3
= 1.6667. Next are the numbers 5 and 8 whose ratio is 8/5 = 1.6.
This followed by the numbers 8 and 13 whose ratio is 13/8 = 1.625.
Next are 13 and 21 whose ratio is 21/13 = 1.6154. This is followed
by the numbers 21 ad 34; their ratio is 34/21 = 1.6190. Next are the
numbers 34 and 55 whose ratio is 55/34 = 1.6176. The ratio of the
next two numbers in the sequence is 89/55 = 1.6182. Finally, the last
two terms in the sequence above have the ratio 144/89 = 1.6180.
>> 1/1
ans =
1
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 88
>> 2/1
ans =
>> 3/2
ans =
1.5000
>> 5/3
ans =
1.6667
>> 8/5
ans =
1.6000
>> 13/8
ans =
1.6250
>> 21/13
ans =
1.6154
>> 34/21
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 89
ans =
1.6190
>> 55/34
ans =
1.6176
>> 89/55
ans =
1.6182
>> 144/89
ans =
1.6180
>> 233/144
ans =
1.6181
>> 377/233
ans =
1.6180
>> 610/377
ans =
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 90
1.6180
>> 987/610
ans =
1.6180
function r=ratio(n)
% This function calculates the ratio of two
% consecutive Fibonacci numbers
f = zeros(n,1);
r = zeros(n,1);
f(1)=1;
f(2)=2;
for k = 3:n
f(k) = f(k-1) + f(k-2);
r(k) = f(k)/f(k-1);
end
Let us execute the above functions for the first fifteen terms
of the Fibonacci sequence. Here is the output of this command:
>> ratio(15)
ans =
0
0
1.5000
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 91
1.6667
1.6000
1.6250
1.6154
1.6190
1.6176
1.6182
1.6180
1.6181
1.6180
1.6180
1.6180
ans =
0
0
1.50000000000000
1.66666666666667
1.60000000000000
1.62500000000000
1.61538461538462
1.61904761904762
1.61764705882353
1.61818181818182
1.61797752808989
1.61805555555556
1.61802575107296
1.61803713527851
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 92
1.61803278688525
1.61803444782168
1.61803381340013
1.61803405572755
1.61803396316671
1.61803399852180
1.61803398501736
1.61803399017560
1.61803398820533
1.61803398895790
1.61803398867044
1.61803398878024
1.61803398873830
1.61803398875432
1.61803398874820
1.61803398875054
>> x = 1:30;
>> y = ratio(30);
>> plot(x,y)
>> hold on;
>> xlabel('n');
>> ylabel('F(n)/F(n-1)')
A C B
AB AC
=
AC CB
AC + CB AC
=
AC CB
CB AC
1+ =
AC CB
1
1+ =x
x
x + 1 = x2
x2 − x − 1 = 0
ans =
1/2*5^(1/2)+1/2
1/2-1/2*5^(1/2)
1+ 5
Thus, it is seen that the two solutions obtained are
2
1− 5 1− 5
and . Clearly, the second solution is a negative
2 2
number and should be discarded with regard to our example.
1+ 5
Therefore, the positive solution obtained is the one that
2
is adopted.
>> (1+sqrt(5))/2
ans =
1.6180
22
The solve command is part of the MATLAB Symbolic Math Toolbox. You
need to have this toolbox installed along with MATLAB in order to use this
command.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 96
ans =
1.61803398874989
>> (1-sqrt(5))/2
ans =
-0.6180
>> (1-sqrt(5))/2
ans =
-0.61803398874989
φ2 − φ −1 = 0
(1 − φ )2 − (1 − φ ) − 1 = 0
The above two equations are checked with MATLAB as
follows:
phi =
1.6180
ans =
0
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 98
1+ 5
φ= = 1.61803398874989
2
1− 5
φ= = −0.61803398874989
2
phi =
1.6180
>> 1/phi
ans =
0.6180
1
It is seen from the above computation that the value of is
φ
surprisingly equal to φ − 1 which is 0.6180. Thus, we obtain the
following formula for φ :
1
= φ −1
φ
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 100
Next, let us calculate the square of the Golden Ratio, i.e. the
value of φ 2 . This operation is performed in MATLAB as follows:
phi =
1.6180
>> phi^2
ans =
2.6180
following relation:
φ2 = φ +1
We have found out that the Golden Ratio has some interesting
properties: its reciprocal is equal to φ − 1 and its square is equal to
φ + 1 . Let us see what other interesting properties this number has.
Let us now study the powers of φ , i.e. let us study the terms φ 2 ,
φ 3 , φ 4 , … and so on. These powers are given by the following set of
equations:
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 101
φ2 = φ =1
φ3 = φ2 + φ
φ4 = φ3 + φ2
………….
φ n + 2 = φ n +1 + φ n , n = 0,1,2,3,...
phi =
1.6180
>> phi^3
ans =
4.2361
ans =
4.2361
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 102
Now, let us show the MATLAB output to check the equation for
φ as follows:
4
phi =
1.6180
>> phi^4
ans =
6.8541
ans =
6.8541
φ2 = φ +1
φ 3 = 2φ + 1
φ 4 = 3φ + 2
φ 5 = 5φ + 3
φ 6 = 8φ + 5
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 103
φ 7 = 13φ + 8
phi =
1.6180
>> phi^3
ans =
4.2361
>> 2*phi + 1
ans =
4.2361
phi =
1.6180
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 104
>> phi^4
ans =
6.8541
>> 3*phi +2
ans =
6.8541
Next, let us study the inverse powers of φ , i.e. let us study the
1 1 1 1
expressions for , 2 , 3 , 4 , …. and so on. The following
φ φ φ φ
expressions will be shown to be true for the inverse powers of the
Golden Ratio:
1 1
1= +
φ φ2
1 1 1
= +
φ φ 2
φ3
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 105
1 1 1
= +
φ 2
φ 3
φ4
1 1 1
= +
φ 3
φ 4
φ5
…………….
1 1 1
= + , n = 0,1,2,3,.....
φ n
φ n +1
φ n+2
phi =
1.6180
ans =
1 1 1
Next, the second equation = + is checked as follows
φ φ 2
φ3
using MATLAB:
phi =
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 106
1.6180
ans =
0.6180
>> 1/phi
ans =
0.6180
1 1
=1−
φ 2
φ
1 2
= −1
φ 3
φ
1 3
= 2−
φ 4
φ
1 5
= −3
φ 5
φ
1 8
=5−
φ 6
φ
1 13
= −8
φ 7
φ
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 107
phi =
1.6180
>> 1/phi^3
ans =
0.2361
>> 2/phi -1
ans =
0.2361
1
Next, the expression for is verified using MATLAB as
φ4
follows:
1.6180
>> 1/phi^4
ans =
0.1459
>> 2 - 3/phi
ans =
0.1459
1 1 1 2 1
the expression 2 = 3 + 4 and substitute − 1 for 3 , and
φ φ φ φ φ
1 1 1 1 1 ⎛ 1⎞ ⎛2 ⎞ 3
1− for to obtain = − = ⎜⎜1 − ⎟⎟ − ⎜⎜ − 1⎟⎟ = 2 − .
φ φ 2
φ 4
φ 2
φ ⎝ φ ⎠ ⎝φ ⎠
3
φ
1 1
+ =1
φ φ2
1
φ+ =2
φ2
1
φ2 + =3
φ2
2
⎛ 1 ⎞
⎜⎜ φ + 2 ⎟⎟ = 4
⎝ φ ⎠
2
⎛ 1⎞
⎜⎜ φ + ⎟⎟ = 5
⎝ φ⎠
phi =
1.6180
ans =
1
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 110
ans =
ans =
ans =
ans =
5.0000
1
Another interesting property is that φ + = 5 . This property is
φ
verified using MATLAB as follows:
phi =
1.6180
ans =
2.2361
>> sqrt(5)
ans =
2.2361
1 1 1 1 1 1
φ= + + + + + + ......
φ φ 2
φ 3
φ 4
φ 5
φ6
phi =
1.6180
ans =
1.5279
1
φ =1+
1
1+
1
1+
1 + .....
x2 = x + 1
It was illustrated in the previous chapter that the two roots of the
above equation are φ and 1 − φ . Substituting each value in the above
quadratic equation separately, we obtain the following two equations:
φ2 = φ +1
(1 − φ )2 = (1 − φ ) + 1
Next, we multiply both sides of the first equation above by φ n ,
and multiply both sides of the second equation above by (1 − φ ) to
n
φ n + 2 = φ n +1 + φ n
(1 − φ )n + 2 = (1 − φ )n +1 + (1 − φ )n
where n = 0,1,2,3,.... . Next, we subtract the second equation
from the first equation above to obtain:
φ n + 2 − (1 − φ )n + 2 = φ n +1 − (1 − φ )n +1 + φ n − (1 − φ )n
φ n + 2 − (1 − φ )n + 2 φ n +1 − (1 − φ )n +1 φ n − (1 − φ )n
= +
φ − (1 − φ ) φ − (1 − φ ) φ − (1 − φ )
F (n + 2 ) = F (n + 1) + F (n )
φ n − (1 − φ )n
F (n ) = , n = 0,1,2,3,....
φ − (1 − φ )
φ n − (1 − φ )n
F (n ) = , n = 0,1,2,3,...
2φ − 1
F (n ) =
(1 + 5 ) − (1 − 5 )
n n
, n = 0,1,2,3,....
2n 5
function f=fib(n)
% This function generates the nth Fibonacci
% number
f = ((1+sqrt(5))^n -(1-sqrt(5))^n)
/(sqrt(5)*2^n);
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 115
>> fib(5)
ans =
>> fib(7)
ans =
13
>> fib(10)
ans =
55.0000
>> fib(15)
ans =
610.0000
φn
F (n ) ≈
5
1+ 5
Substituting the value of φ which is in the above
2
formula, we obtain the following explicit expression for the
approximate formula:
F (n ) ≈
(1 + 5 ) n
2n 5
function f=approximate_fib(n)
% This function generates the nth Fibonacci
% number using the approximate formula
f = ((1+sqrt(5))^n) /(sqrt(5)*2^n);
φ n − (1 − φ )n φ n
24 Note that lim = . You need to revise your calculus
n →∞ 2φ − 1 5
information in order to derive this limit.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 117
>> approximate_fib(1)
ans =
0.7236
>> approximate_fib(2)
ans =
1.1708
>> approximate_fib(3)
ans =
1.8944
>> approximate_fib(4)
ans =
3.0652
>> approximate_fib(5)
ans =
4.9597
>> fib(10)
ans =
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 118
55.0000
>> approximate_fib(10)
ans =
55.0036
>> fib(30)
ans =
8.3204e+005
>> approximate_fib(30)
ans =
8.3204e+005
9. Lucas Numbers
In this chapter we will study Lucas numbers and the Lucas
sequence of numbers. These numbers are closely associated with
Fibonacci numbers. Lucas numbers are generated using the same rule
as that of Fibonacci numbers, i.e. the next number is obtained by
adding the previous two numbers. However, instead of starting with
the numbers 1 and 1, we start with the numbers 2 and 1. Thus the
third Lucas number is 3. The fourth Lucas number is 4. The fifth
Lucas number is 7. Thus, the first few Lucas numbers are 2, 1, 3, 4, 7,
11, 18, 29, 47, 76, 123, …. This sequence of numbers is called the
Lucas sequence. These numbers are generated using MATLAB
interactively as follows:
>> 2+1
ans =
>> 1+3
ans =
>> 3+4
ans =
>> 4+7
ans =
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 120
11
>> 7+11
ans =
18
>> 11+18
ans =
29
>> 18+29
ans =
47
>> 29+47
ans =
76
>> 47+76
ans =
123
function f=lucas(n)
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 121
>> lucas(7)
ans =
1
3
4
7
11
18
29
>> lucas(20)
ans =
1
3
4
7
11
18
29
47
76
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 122
123
199
322
521
843
1364
2207
3571
5778
9349
15127
The above results show the first seven and twenty Lucas
numbers, respectively. Using mathematical notation, let us denote the
nth number in the Lucas sequence by L(n). Then, the Lucas sequence
of numbers can be generated by using the following recursive
relation:
L(1) = 2
L(2) = 1
n = 3, 4, 5, 6, …..
The above relations for generating Lucas numbers are exactly the
same as those of the Fibonacci numbers except the initial conditions,
i.e. the two starting numbers, are different.
>> x = 1:10
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 123
x =
1 2 3 4 5 6 7
8 9 10
The next step would be to generate the y-axis which would be the
first ten Lucas numbers. These are generated and stored in the
variable y as follows:
>> y = lucas(10)
y =
1
3
4
7
11
18
29
47
76
123
>> plot(x,y)
>> hold on
>> xlabel('n')
>> ylabel('lucas(n)')
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 124
Similarly, the following commands will generate and plot the first
30 Lucas numbers. The plot is generated after the commands are
executed sequentially and is shown in Figure 9.2. Each command is
followed by semi-colon in order to suppress the listed output:
>> x = 1:30;
>> y = lucas(30);
>> plot(x,y)
>> hold on;
>> xlabel('n')
>> ylabel('lucas(n)')
>> log(lucas(10))
ans =
0
1.0986
1.3863
1.9459
2.3979
2.8904
3.3673
25The logarithms calculated here are the natural logarithms, which are logarithms
to the base e, where e = 2.71828… .
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 126
3.8501
4.3307
4.8122
We need now to plot the above logarithms on the y-axis while
keeping the number of terms n on the x-axis. The type of plot
obtained is called a semi-log graph. We will perform the following
operations (while suppressing the output) in order to accomplish this.
The resulting graph is shown in Figure 9.3. The graph is called a
log(L(n)) vs. n graph. The MATLAB commands are shown below:
>> x = 1:10;
>> y = log(lucas(10));
>> plot(x,y)
>> hold on
>> xlabel('n')
>> ylabel('log(L(n))')
It is seen from Figure 9.3 that the graph is almost a straight line
for values of n larger than 3. Let us calculate the slope26 of this line.
Let us perform this calculation by taking the fifth and tenth Lucas
numbers and calculating the slope of the line segment between them.
This is implemented in MATLAB as follows:
>> m = (y(10)-y(5))/(x(10)-x(5))
m =
0.4829
Thus the slope of the straight line is 0.4829. In order to find the
angle, we calculate the inverse tangent value of this number. This is
obtained as tan-1(0.4829) = 25.8o.
Let us perform the above operations again but using the first 30
Lucas numbers. Here are the MATLAB commands to generate the
plot while the graph is shown in Figure 9.4.
>> x = 1:30;
>> y = log(lucas(30));
>> plot(x,y)
>> hold on
>> xlabel('n')
>> ylabel('log(L(n))')
>> m = (y(30)-y(10))/(x(30)-x(10))
26The slope m of a line segment between two points (x1,y1) and (x2,y2) is calculated
using the formula m = (y2-y1)/(x2-x1).
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 128
m =
0.4812
>> 1/2
ans =
0.5000
>> 3/1
ans =
>> 4/3
ans =
1.3333
>> 7/4
ans =
1.7500
>> 11/7
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 130
ans =
1.5714
>> 18/11
ans =
1.6364
>> 29/18
ans =
1.6111
>> 47/29
ans =
1.6207
>> 76/47
ans =
1.6170
>> 123/76
ans =
1.6184
>> 199/123
ans =
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 131
1.6179
>> 322/199
ans =
1.6181
>> 521/322
ans =
1.6180
>> 843/521
ans =
1.6180
function r=lucas_ratio(n)
% This function calculates the ratio of two
% consecutive Lucas numbers
f = zeros(n,1);
r = zeros(n,1);
f(1)=1;
f(2)=3;
for k = 3:n
f(k) = f(k-1) + f(k-2);
r(k) = f(k)/f(k-1);
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 132
end
Let us execute the above function for the first fifteen terms of
the Lucas sequence. Here is output of the command:
>> lucas_ratio(15)
ans =
0
0
1.3333
1.7500
1.5714
1.6364
1.6111
1.6207
1.6170
1.6184
1.6179
1.6181
1.6180
1.6180
1.6180
It is seen from the above output that the ratio of two consecutive
Lucas numbers approaches the Golden Ratio which is the number
1.6180. To get more precision, we will use the format long
command and execute the function again but with thirty terms as
follows:
ans =
0
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 133
0
1.33333333333333
1.75000000000000
1.57142857142857
1.63636363636364
1.61111111111111
1.62068965517241
1.61702127659574
1.61842105263158
1.61788617886179
1.61809045226131
1.61801242236025
1.61804222648752
1.61803084223013
1.61803519061584
1.61803352967830
1.61803416409969
1.61803392177224
1.61803401433308
1.61803397897799
1.61803399248243
1.61803398732419
1.61803398929446
1.61803398854189
1.61803398882935
1.61803398871955
1.61803398876149
1.61803398874547
1.61803398875159
It is seen from the above results that the limiting ratio approaches
the value 1.6180339887… (accurate to ten decimal digits). Let us plot
a graph of this ratio using the following commands where the output
is suppressed:
>> x = 1:30;
>> y = lucas_ratio(30);
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 134
>> plot(x,y)
>> hold on
>> xlabel('n')
>> ylabel('L(n)/L(n-1)')
The resulting graph is shown in Figure 9.5. It is seen that the ratio
L(n)/L(n-1) approaches the limiting value 1.61803…. which is the
Golden Ratio, exactly as in the case of the Fibonacci sequence (see
Chapter 7). The graph shown in the figure is know as the L(n)/L(n-1)
vs. n graph.
Figure 9.5: Graph of the limiting ratio of the Lucas sequence (shown for the
first thirty terms of the sequence). The limiting ratio clearly approaches the
value 1.61803…
Fibonacci number and L(n) to denote the nth Lucas number, let us
consider the following equation:
L(n+1) = F(n-1) + F(n+1) , n = 2, 3, 4, …..
27 This does not constitute a formal proof; we are just showing that the equation is
correct for some values of n. For a formal proof, we should use the method of
mathematical induction but this is beyond the scope of this work.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 136
Let us now show how to compute the value of the nth Lucas
number directly without listing the preceding Lucas numbers in the
sequence. We will show this in a way analogous to that used for the
Fibonacci sequence in Chapter 8. The nth Lucas number in the
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 137
L(n) = φ n −1 + (1 − φ )
n −1
, n = 1,2,3,.....
For example, let us calculate the eighth Lucas number using the
above equation. Here are the MATLAB commands for this
calculation using the formula given above:
>> phi = (1+sqrt(5))/2
phi =
1.6180
L8 =
29.0000
function f=luc(n)
% This function generates the nth Lucas number
f = ((1+sqrt(5))/2)^(n-1) + (1-(1+sqrt(5))/2)^(n-
1);
28 The derivation of this formula is left to the reader. The derivation can easily be
made in a way similar to the derivation of the formula for the nth Fibonacci
number that was detailed in Chapter 8.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 138
ans =
>> luc(8)
ans =
29.0000
>> luc(15)
ans =
843.0000
L(n) ≈ φ n −1
function f=approximate_luc(n)
% This function generates the nth Lucas number
% using the approximate formula
f = ((1+sqrt(5))/2)^(n-1);
>> approximate_luc(3)
ans =
2.6180
>> approximate_luc(8)
ans =
29.0344
>> approximate_luc(15)
ans =
842.9988
10. Generalizations of
Fibonacci Numbers
In this chapter we will study several methods to generalize
Fibonacci numbers. One way is to change the initial conditions, i.e.
change the first two numbers in the sequence. This method was
illustrated in Chapter 9 when we studied the Lucas sequence. The
Lucas sequence was obtained from the Fibonacci rule by changing
the first two terms of the Fibonacci sequence. Another method to
generalize Fibonacci numbers is to use the sum of the preceding
three terms instead of the preceding two terms in order to generate
the next term in the sequence. Both these methods will be illustrated
in this chapter.
function f=tribonacci(n)
% This function generates the first n
tribonacci numbers
f = zeros(n,1);
f(1)=0;
f(2)=0;
f(3)=1;
for k = 4:n
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 142
>> tribonacci(20)
ans =
0
0
1
1
2
4
7
13
24
44
81
149
274
504
927
1705
3136
5768
10609
19513
x3 − x 2 − x − 1 = 0
1 + 3 19 + 3 33 + 3 19 − 3 33
3
>> double(ans)
ans =
1.8393
-0.4196 + 0.6063i
-0.4196 - 0.6063i
x + x −3 = 2
1
(a+ + a− + a )n
T (n ) = 3b 3 2
b − 2b + 4
where the coefficients a+ , a− , and b are given by:
(
a± = 19 ± 3 33 )
1/ 3
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 144
(
b = 586 + 102 33 )
1/ 3
function f=tetranacci(n)
% This function generates the first n
tetranacci numbers
f = zeros(n,1);
f(1)=0;
f(2)=0;
f(3)=0;
f(4)=1;
for k = 5:n
f(k) = f(k-1) + f(k-2) + f(k-3) + f(k-
4);
end
>> tetranacci(20)
ans =
0
0
0
1
1
2
4
8
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 145
15
29
56
108
208
401
773
1490
2872
5536
10671
20569
x 4 − x3 − x 2 − x − 1 = 0
>> double(ans)
ans =
1.9276
-0.7748
-0.0764 + 0.8147i
-0.0764 - 0.8147i
function f=pentanacci(n)
% This function generates the first n
pentanacci numbers
f = zeros(n,1);
f(1)=0;
f(2)=0;
f(3)=0;
f(4)=0;
f(5)=1;
for k = 6:n
f(k) = f(k-1) + f(k-2) + f(k-3) + f(k-
4) +f(k-5);
end
>> pentanacci(20)
ans =
0
0
0
0
1
1
2
4
8
16
31
61
120
236
464
912
1793
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 147
3525
6930
13624
function f=hexanacci(n)
% This function generates the first n
hexanacci numbers
f = zeros(n,1);
f(1)=0;
f(2)=0;
f(3)=0;
f(4)=0;
f(5)=0;
f(6)=1;
for k = 7:n
f(k) = f(k-1) + f(k-2) + f(k-3) + f(k-
4) + f(k-5) +f(k-6);
end
>> hexanacci(20)
ans =
0
0
0
0
0
1
1
2
4
8
16
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 148
32
63
125
248
492
976
1936
3840
7617
function f=heptanacci(n)
% This function generates the first n
heptanacci numbers
f = zeros(n,1);
f(1)=0;
f(2)=0;
f(3)=0;
f(4)=0;
f(5)=0;
f(6)=0;
f(7)=1;
for k = 8:n
f(k) = f(k-1) + f(k-2) + f(k-3) + f(k-
4) + f(k-5) +f(k-6) +f(k-7);
end
>> heptanacci(20)
ans =
0
0
0
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 149
0
0
0
1
1
2
4
8
16
32
64
127
253
504
1004
2000
3984
r k −1 (r − 1)
Fk( n ) =
(n + 1)r − 2n
where r is the n-nacci constant which may be obtained by
solving the following general algebraic equation:
x + x −n = 2
k
F (n ) = ∑ F (n − i )
i =1
x k − x k −1 − x k − 2 − ........... − x − 1 = 0
Finally, we use the above general linear recurrence relation and
the associated algebraic equation to derive the following examples of
generalized Fibonacci sequences:
Next, we show further examples. For more details, see the books
in references [48-56] and the web links [57-67].
>> double(ans)
ans =
1.6180
-0.6180
>> double(ans)
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 151
ans =
1.3247
-0.6624 + 0.5623i
-0.6624 - 0.5623i
>> double(ans)
ans =
1.4656
-0.2328 + 0.7926i
-0.2328 - 0.7926i
>> double(ans)
ans =
1.2207
-0.7245
-0.2481 + 1.0340i
-0.2481 - 1.0340i
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 152
>> double(ans)
ans =
1.3803
-0.8192
0.2194 + 0.9145i
0.2194 - 0.9145i
>> double(ans)
ans =
1.1673
0.1812 + 1.0840i
-0.7649 + 0.3525i
-0.7649 - 0.3525i
0.1812 - 1.0840i
>> double(ans)
ans =
1.1939
0.1546 + 0.8281i
-0.7515 + 0.7846i
-0.7515 - 0.7846i
0.1546 - 0.8281i
>> double(ans)
ans =
1.2365
0.3408 + 0.7854i
-0.9590 + 0.4284i
-0.9590 - 0.4284i
0.3408 - 0.7854i
>> double(ans)
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 154
ans =
0.5000 + 0.8660i
0.5000 - 0.8660i
1.3247
-0.6624 + 0.5623i
-0.6624 - 0.5623i
For more details, see the books in references [48-56] and the web
links [57-67]. In the next chapter, we investigate random Fibonacci
sequences.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 155
F (n ) = F (n − 1) ± F (n − 2 )
function f=random_fibonacci(n)
% This function generates the first n
random Fibonacci numbers
f = zeros(n,1);
f(1)=1;
f(2)=2;
o = [-1,1];
s = o(ceil(2.0*rand(1,1)));
for k = 3:n
f(k) = f(k-1) + s*f(k-2);
end
>> random_fibonacci(20)
ans =
1
2
1
-1
-2
-1
1
2
1
-1
-2
-1
1
2
1
-1
-2
-1
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 157
1
2
References
MATLAB Tutorials
1. http://www.cyclismo.org/tutorial/matlab/
2. http://www.math.ufl.edu/help/matlab-tutorial/
3. http://www.mines.utah.edu/gg_computer_seminar/matlab/
matlab.html
4. http://web.mit.edu/afs/.athena/astaff/project/logos/olh/M
ath/Matlab/Matlab.html
5. http://www.mit.edu/people/abbe/matlab/main.html
6. http://texas.math.ttu.edu/~gilliam/ttu/m4330/m4330.html
7. http://www.engin.umich.edu/group/ctm/basic/basic.html
8. http://www.mathworks.com/academia/student_center/tutor
ials/launchpad.html
9. http://www.math.utah.edu/lab/ms/matlab/matlab.html#sta
rting
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 161
10. http://www.math.mtu.edu/~msgocken/intro/intro.html
11. http://www.eece.maine.edu/mm/matweb.html
12. http://web.cecs.pdx.edu/~gerry/MATLAB/
13. http://www.facstaff.bucknell.edu/maneval/help211/helpmai
n.html
14. http://www.indiana.edu/~statmath/math/matlab/gettingsta
rted/index.html
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 162
15. www.math.udel.edu/~driscoll/teaching/matlab_adv.pdf
16. www.maths.dundee.ac.uk/~ftp/na-
reports/MatlabNotes.pdf
17. www.geosci.uchicago.edu/~gidon/geosci236/organize/matl
abIntro.pdf
18. http://www.math.colostate.edu/~gerhard/classes/340/notes
/index.html
19. http://www.indiana.edu/~statmath/math/matlab/gettingsta
rted/index.html
20. http://www-
ccs.ucsd.edu/matlab/toolbox/symbolic/symbmath.html
21. http://www.phys.ufl.edu/docs/matlab/toolbox/symbolic/c
h1.html
22. http://en.wikipedia.org/wiki/Matlab
23. http://www.facstaff.bucknell.edu/maneval/help211/exercise
s.html
24. http://www.math.chalmers.se/~nilss/enm/INTRO_matlab
_I.html
25. http://college.hmco.com/mathematics/larson/elementary_li
near/5e/students/matlabs.html
26. http://www.math.umn.edu/~ROberts/math5385/matlabEx
1.html
27. www.kom.aau.dk/~borre/matlab7/exercise.pdf
28. www.chaos.swarthmore.edu/courses/Phys50L_2006/Matla
b/MatlabExercises.pdf
MATLAB Books:
29. Gilat, A., MATLAB: An Introduction with Applications, Second
Edition, John Wiley & Sons, 2004.
49. Vajda, S., Fibonacci and Lucas Numbers, and the Golden Section,
Dover Publications, 2007.
51. Dunlap, R. A., The Golden Ratio and Fibonacci Numbers, World
Scientific Publishing, 1997.
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 168
52. Olsen, S., The Golden Section: Nature’s Greatest Secret, Walker and
Company, 2006.
53. Livio, M., The Golden Ratio: The Story of PHI, the World’s Most
Astonishing Number, Broadway, 2003.
54. Koshy, T., Fibonacci and Lucas Numbers with Applications, Wiley-
Interscience, 2001.
Installation of MATLAB
In this book, it is assumed that you have already installed
MATLAB on your computer system. To go through the eleven
chapters of the book, you need to have MATLAB running on your
computer. For help on installing MATLAB on your computer, check
the following web links:
http://www.itc.virginia.edu/research/matlab/
install/installwin.html
http://www.freebsd.org/doc/zh_TW/books/handb
ook/linuxemu-matlab.html
http://www.itc.virginia.edu/research/matlab/
install/installmac.html
http://shum.huji.ac.il/cc/matlR13linuxhomein
st.html
MATLAB Guide to Fibonacci Numbers and the Golden Ratio 171