MATLAB Matlab recursion (error in book?)

AI Thread Summary
The discussion centers on a MATLAB implementation of the Fibonacci sequence, where the initial values are defined as fib(1) = 0 and fib(2) = 1. A user points out that this leads to an incorrect result for fib(3), which should equal 2, not 1. The conversation reveals that the discrepancy arises from differing interpretations of the Fibonacci sequence's starting values, with some preferring the sequence to begin with 1, 1 instead. Both definitions ultimately yield the same series after the initial terms, but the choice of starting values affects the output for the first few terms. The discussion emphasizes the importance of clarity in defining initial conditions in recursive algorithms.
Nano-Passion
Messages
1,291
Reaction score
0
This is what my book gives for recursion of fibonacci sequence (matlab coding).

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 news on Phys.org
Hey Nano-Passion.

I think this is an issue with semantics and definition as opposed to the algorithm itself being wrong.

It seems that this code is defining the sequence as the fibonacci sum of n individual terms with the first term being 0 and the second term being 1, so f(1) = 0, f(2) = 1 and f(n) = f(n-2) + f(n-1) for n > 2.

I've seen the series start with the terms 1 and 1 for the value of the first two terms as well which doesn't affect the rest of the series, and personally I think it makes more sense to do it this way (instead of 0,1) so in this case, you would have your definition of f(3) = 2 instead of f(3) = 1.

So while I side with you, it is just a matter of interpretation of what the initial values should be.
 
chiro said:
I've seen the series start with the terms 1 and 1 for the value of the first two terms as well which doesn't affect the rest of the series, and personally I think it makes more sense to do it this way (instead of 0,1) so in this case, you would have your definition of f(3) = 2 instead of f(3) = 1.

Hey Chiro, thanks for the reply.

I'm a bit unclear here, can you elaborate or reword what you have stated please.
 
Basically you can have a series that goes 0,1,1,2,3,5,8,... or you can have 1,1,2,3,5,8.

The first one starts with 0,1 as two initial values and the second starts with 1,1 as two initial values.

Both produce the same sequence after the 1,1 case but the 2nd one is a subset of the 1st.

To me the 1st one doesn't make much sense anyway, but algorithmically they do produce the same series after the 1,1 point.
 
chiro said:
Basically you can have a series that goes 0,1,1,2,3,5,8,... or you can have 1,1,2,3,5,8.

The first one starts with 0,1 as two initial values and the second starts with 1,1 as two initial values.

Both produce the same sequence after the 1,1 case but the 2nd one is a subset of the 1st.

To me the 1st one doesn't make much sense anyway, but algorithmically they do produce the same series after the 1,1 point.

Thanks for your time.
 

Similar threads

Back
Top