Matlab recursion (error in book?)

In summary, the conversation discusses the use of recursion in coding the Fibonacci sequence in MATLAB. The code provided uses the terms 0 and 1 as the first two values, while the other person suggests using 1 and 1 instead. Both produce the same sequence after a certain point, but it is a matter of personal interpretation.
  • #1
Nano-Passion
1,291
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
  • #2
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.
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 

FAQ: Matlab recursion (error in book?)

What is Matlab recursion and how does it work?

Matlab recursion is a programming technique where a function calls itself repeatedly until a certain condition is met. It is used to solve problems that can be broken down into smaller and simpler versions of the same problem. The function will continue to call itself until it reaches a base case, which is a simple version of the problem that can be easily solved.

What are the advantages of using recursion in Matlab?

One advantage of using recursion in Matlab is that it can simplify complex problems and make them easier to solve. It also allows for more concise and elegant code compared to using iterative methods. Additionally, recursion can save memory space as it does not require the use of loops.

What is tail recursion and how is it different from regular recursion?

Tail recursion is a type of recursion where the recursive call is the last statement in the function. This means that there is no additional code to be executed after the recursive call. In regular recursion, there may be additional code that needs to be executed after the recursive call. Tail recursion can be more efficient as it does not require the use of a stack to store function calls.

What are some common errors that can occur when using recursion in Matlab?

Some common errors that can occur when using recursion in Matlab include infinite recursion, where the base case is never reached and the function keeps calling itself indefinitely. Another error is stack overflow, which happens when there are too many function calls and the memory stack runs out of space. It is important to carefully plan and test recursive functions to avoid these errors.

Is there a limit to the number of recursive calls that can be made in Matlab?

Yes, there is a limit to the number of recursive calls that can be made in Matlab. This limit is determined by the available memory space on the computer. As the number of recursive calls increases, so does the memory usage. If the memory limit is reached, a stack overflow error will occur. It is important to consider memory usage when designing and implementing recursive functions.

Similar threads

Replies
4
Views
4K
Replies
6
Views
6K
Replies
10
Views
2K
Replies
5
Views
2K
Replies
2
Views
3K
Replies
5
Views
2K
Replies
2
Views
1K
Back
Top