This is what my book gives for recursion of fibonacci sequence (matlab coding).(adsbygoogle = window.adsbygoogle || []).push({});

function res = fib(n)

if n == 1;

res = 0;

elseif n == 2

res = 1;

else

res = fib(n-1) + fib(n-2);

end

Am I mistaken or this doesn't work for fib(3)? Keep in mind I am still learning about recursion.

>> fib(3)

res = fib(2)+fib(1)

fib(1) = 0. fib(2) = 1

therefore fib (3) = 1+0 = 1

fib(3) should be 2.

**Physics Forums | Science Articles, Homework Help, Discussion**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Matlab recursion (error in book?)

**Physics Forums | Science Articles, Homework Help, Discussion**