ENE 2017
Engineering Programming
Prof. Keonwook Kim
Division of Electronics and Electrical Engineering
Dongguk University, Seoul, Korea
Ch.2 Fibonacci Numbers
Fibonacci numbers introduce vectors, functions and recursion.
Experiments with MATALB – Cleve Moler
Fibonacci
• Problem statement
• A man puts a pair of rabbits in a place surrounded on all sides by a
wall. How many pairs of rabbits can be produced from that pair in a
year if it is supposed that every month each pair begets a new pair
which from the second month on becomes productive?
• Mathematical model
• 𝑓 : denote the number of pairs of rabbits after n months.
𝑓 𝑓 𝑓
• Initial conditions
𝑓 1, 𝑓 2
Experiments with MATALB – Cleve Moler
Fibonacci
• Function
Experiments with MATALB – Cleve Moler
Fibonacci
• In command window
• The answer is 233 pairs of rabbits.
• It would be 4096 pairs if the number doubled every month for 12
months.
• Function
• This particular function produces one output result, f, and takes one
input argument, n.
• The next two lines are comments that provide the text displayed
when you ask for help.
Experiments with MATALB – Cleve Moler
Fibonacci
• Another Fibonacci function, fibnum.m. Its output is simply the
nth Fibonacci number.
• The fibnum function is recursive. Not recommend.
Experiments with MATALB – Cleve Moler
Golden ratio
• The golden ratio ϕ can be expressed as an infinite continued
fraction.
Experiments with MATALB – Cleve Moler
Golden ratio
• goldfract.m
• output from goldfract(n) when n = 7
Experiments with MATALB – Cleve Moler
Golden ratio
• 𝜙 denote the continued fraction truncated after n terms.
Experiments with MATALB – Cleve Moler
Golden ratio
Experiments with MATALB – Cleve Moler
Golden ratio
Experiments with MATALB – Cleve Moler
An analytic expression
• Closed-form solution to the Fibonacci number
• Possible solution are 𝜙 and 1 𝜙
• The constants c1 and c2 are determined by initial conditions
Experiments with MATALB – Cleve Moler
An analytic expression
• Use the MATLAB backslash operator (actually by hand)
• The .^ operator is an element-by-element power operator.
Experiments with MATALB – Cleve Moler
Recap
• Do by yourself