Building the Fibonacci using recursive

조회 수: 75 (최근 30일)
surendra kumar Aralapura mariyappa
댓글: Vishnu V 2023년 9월 10일
Help needed in displaying the fibonacci series as a row or column vector, instead of all number.
Thia is my code: I need to display all the numbers: But getting some unwanted numbers.
function y = my_recursive3(n)
% y = zeros(1,n);
y = zeros(1,n);
ca = 1;
if n == 0
y = n ;
disp(y);
elseif n == 1
y = n;
disp(y);
else
y = my_recursive3(n-1)+ my_recursive3(n-2);
%y(ca) = y;
%ca = ca + 1;
end
%disp(y);
Thanks in advance

답변 (4개)

Stephen23
Stephen23 2019년 6월 12일
편집: Stephen23 2019년 6월 12일
I doubt that a recursive function is a very efficient approach for this task, but here is one anyway:
function v = myfib(n,v)
if nargin==1
v = myfib(n-1,[0,1]);
elseif n>1
v = myfib(n-1,[v,v(end-1)+v(end)]);
end
end
and tested:
>> myfib(8)
ans =
0 1 1 2 3 5 8 13
>> myfib(10)
ans =
0 1 1 2 3 5 8 13 21 34
  댓글 수: 3
Alwin Varghese
Alwin Varghese 2020년 8월 21일
you can add two lines to the above code by Stephen Cobeldick to get solution for myfib(1)
function v = myfib(n,v)
if nargin==1
v = myfib(n-1,[0,1]);
elseif n>1
v = myfib(n-1,[v,v(end-1)+v(end)]);
end
if n<1
v=0;
end
Stephen23
Stephen23 2020년 8월 24일
@Akhila M : you could do something like Alwin Varghese suggested, but I recommend a more efficient elseif rather than defining a separate if statement:
function v = myfib(n,v)
if nargin==1
v = myfib(n-1,[0,1]);
elseif n>1
v = myfib(n-1,[v,v(end-1)+v(end)]);
elseif n<1
v = 0;
end

댓글을 달려면 로그인하십시오.


Soumya Sinha
Soumya Sinha 2019년 6월 17일
The code for generating the fabonacci series numbers is given as -
function [n] = abcd(x)
if (x == 1 || x==0)
n = x;
return
else
n = abcd(x-1) + abcd(x-2);
end
end
However you can use a simpler approach using dynamic programming technique -
fibonacci = [0 1];
for i = 1:n-2
fibonacci = [fibonacci fibonacci(end)+fibonacci(end-1)];
end
This is a more efficient approach for this since recursion is exponential in complexity.

Dhiraj Darji
Dhiraj Darji 2022년 3월 30일
function f= fibor(n)
if n<=2
f=1;
else
f=fibor(n-1)+fibor(n-2);
end
This is working very well for small numbers but for large numbers it will take a long time
  댓글 수: 2
MEERA MATHEW
MEERA MATHEW 2023년 4월 28일
What is the solution for larger inputs ?
Vishnu V
Vishnu V 2023년 9월 10일
The code you provided is producing the sum of the elements in the fibonacci series but not the fibonacci series

댓글을 달려면 로그인하십시오.


Iyad Khuder
Iyad Khuder 2022년 10월 2일
Although this is resolved above, but I'd like to know how to fix my own solution:
function FiboSec = Fibo_Recursive( a,b,n )
k = n;
if n == 2
FiboSec(k) = b;
return;
end
if n == 1
FiboSec(k) = a;
return;
end
while k <= n
FiboSec(k) = Fibo_Recursive(a,b,k-1) + Fibo_Recursive(a,b,k-2);
k = k + 1;
end
end
The algorithm is to start the formula from the top (for n), decompose it to F(n-1) + F(n-2), then find the formula for each of the 2 terms, and so on, untul reaching the basic terms F(2) and F(1).
I tried to debug it by running the code step-by-step. I noticed that the error occurs when it starts calculating Fibosec(3), giving the error:
"Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
Could you please help me fixing this error?
Thanks
  댓글 수: 2
John D'Errico
John D'Errico 2022년 10월 2일
편집: John D'Errico 2022년 10월 2일
Please don't learn to add an answer as a question! Your answer does not actually solve the question asked, so it is not really an answer. At best, I suppose it is an attempt at an answer though.
As far as the question of what you did wrong, Why do you have a while loop in there???????? You have written the code as a recursive one. That completely eliminates the need for a loop of any form.
1. What do you ant to happen when n == 1? It should return a. So you go that part correct.
2. What do you want it to do when n == 2? Agin, it should return b. Again, correct.
3. What should happen when n is GREATER than 2? It should use the recursive formula. NO LOOP NEEDED.
Next, why do you rename k to n?
Next, learn how to use the (if, elsef, else) form properly.
Finally, IF you want to return the ENTIRE sequence, from 1 to n, then using the recursive form is insane. Sorry, but it is. If you already have the first parts of the sequence, then you would just build them up from 1, to 2, to 3, all the way up to n. As such a fully recursive code is crazy IF that is your goal.
Below is your code, as corrected. As a test...
Fibo_Recursive(1,1,3)
ans = 2
Fibo_Recursive(1,1,4)
ans = 3
Fibo_Recursive(1,1,5)
ans = 5
Fibo_Recursive(1,1,10)
ans = 55
function FiboSec = Fibo_Recursive( a,b,n)
if n == 2
FiboSec = b;
return;
elseif n == 1
FiboSec = a;
return;
else
% All other cases have n > 2
FiboSec = Fibo_Recursive(a,b,n-1) + Fibo_Recursive(a,b,n-2);
end
end
Again, IF your desire is to generate and store the entire sequence, then start from the beginning. A for loop would be appropriate then. Note that the above code is also insanely ineqfficient, if n is at all large. And n need not be even too large for that inefficiency to become apparent.
Anyway, a simple looped code, generating the entire sequence would look like that below:
function Fibseq = loopedfib(a,b,n)
Fibseq = zeros(1,n)
Fibseq(1) = a;
if n == 2;
Fibseq(2) = b;
for k = 3:n
Fibseq(k) = Fibseq(k-1) + Fibseq(k-2);
end
end
This code starts at the beginning, and works upwards. A recursive code tries to start at the end, and then looks backwards, using recursive calls. There is then no loop needed, as I said.
Do you see that the code you wrote was an amalgam of both the looped versions I wrote, and the recursive codes I wrote, but that it was incorrect to solve the problem in either form?
Iyad Khuder
Iyad Khuder 2022년 10월 2일
  • Apologies. I'm new to this forum.
  • I first wanted to post this as a separate question, but I was afraid it'd be repetitive, as there's already this post, which discusses the same point.
  • Much apprecuated!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2014a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by