This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 2 Fibonacci Numbers Fibonacci numbers introduce vectors, functions and recursion. An exercise intro duces graphics user interfaces. Leonardo Pisano Fibonacci was born around 1170 and died around 1250 in Pisa in what is now Italy. He traveled extensively in Europe and Northern Africa. He wrote several mathematical texts that, among other things, introduced Europe to the HinduArabic notation for numbers. Even though his books had to be tran scribed by hand, they were widely circulated. In his best known book, Liber Abaci , published in 1202, he posed the following problem: A man put 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? Today the solution to this problem is known as the Fibonacci sequence , or Fibonacci numbers . There is a small mathematical industry based on Fibonacci numbers. A search of the Internet for “Fibonacci” will find dozens of Web sites and hundreds of pages of material. There is even a Fibonacci Association that publishes a scholarly journal, the Fibonacci Quarterly . If Fibonacci had not specified a month for the newborn pair to mature, he would not have a sequence named after him. The number of pairs would simply double each month. After n months there would be 2 n pairs of rabbits. That’s a lot of rabbits, but not distinctive mathematics. Let f n denote the number of pairs of rabbits after n months. The key fact is that the number of rabbits at the end of a month is the number at the beginning Copyright c 2009 Cleve Moler Matlab R is a registered trademark of The MathWorks, Inc. TM August 8, 2009 1 2 Chapter 2. Fibonacci Numbers of the month plus the number of births produced by the mature pairs: f n = f n 1 + f n 2 . The initial conditions are that in the first month there is one pair of rabbits and in the second there are two pairs: f 1 = 1 , f 2 = 2 . The following Matlab function, stored in the Mfile fibonacci.m , produces a vector containing the first n Fibonacci numbers. function f = fibonacci(n) % FIBONACCI Fibonacci sequence % f = FIBONACCI(n) generates the first n Fibonacci numbers. f = zeros(n,1); f(1) = 1; f(2) = 2; for k = 3:n f(k) = f(k1) + f(k2); end With these initial conditions, the answer to Fibonacci’s original question about the size of the rabbit population after one year is given by fibonacci(12) This produces 1 2 3 5 8 13 21 34 55 89 144 233 The answer is 233 pairs of rabbits. (It would be 4096 pairs if the number doubled every month for 12 months.) Let’s look carefully at fibonacci.m . It’s a good example of how to create a Matlab function. The first line is function f = fibonacci(n) The first word on the first line says fibonacci.m is a function , not a script. The remainder of the first line says this particular function produces one output result, 3 f , and takes one input argument,...
View
Full
Document
This note was uploaded on 01/21/2011 for the course PHYS 3 taught by Professor Freedman,r during the Spring '08 term at UCSB.
 Spring '08
 Freedman,R
 Physics

Click to edit the document details