home

Scientific Computing (Psychology 9040a)

Fall, 2021

nth Fibonacci Number


Each term in the Fibonacci sequence is generated by adding the previous two terms. By definition, the first Fibonacci number is 1 and the second Fibonacci number is 2. The rest of the sequence unfolds according to the rule of adding the previous two terms.

So the first 10 Fibonacci numbers are thus:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

We can use the notation Fb(n) to denote the nth Fibonacci number. So Fb(1)=1 and Fb(2)=2, and Fb(10)=89.

Write a function called Fib() that takes as input n, and produces as output Fb(n), the nth Fibonacci number.

It should look like this when you use it:

>> Fib(3)

ans =

     3

>> Fib(9)

ans =

    55

sample solution