Matlab recursion (error in book?)

  • Context: MATLAB 
  • Thread starter Thread starter Nano-Passion
  • Start date Start date
  • Tags Tags
    Book Matlab Recursion
Click For Summary

Discussion Overview

The discussion revolves around the implementation of the Fibonacci sequence in MATLAB using recursion. Participants are examining the correctness of the provided code and the definitions of the initial terms in the Fibonacci sequence.

Discussion Character

  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant questions the correctness of the Fibonacci function provided in the book, asserting that it yields an incorrect result for fib(3).
  • Another participant suggests that the issue lies in the semantics and definition of the Fibonacci sequence rather than the algorithm itself, noting that the initial values can vary.
  • Some participants propose that the Fibonacci sequence can start with either (0, 1) or (1, 1), leading to different interpretations of the sequence's terms.
  • It is mentioned that both definitions lead to the same sequence after the initial terms, but there is a preference expressed for starting with (1, 1) as it seems more logical to some participants.

Areas of Agreement / Disagreement

Participants express differing views on the initial values of the Fibonacci sequence, with no consensus reached on which definition is preferable. The discussion remains unresolved regarding the implications of these definitions on the correctness of the code.

Contextual Notes

The discussion highlights the ambiguity in defining the Fibonacci sequence and how it affects the implementation in code. There are no clear resolutions on the definitions or the correctness of the algorithm.

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

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
11
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K