Output argument 'answers' (and maybe others) not assigned during call to "..."

I am trying to create a function that sums up all the values of the fibonacci series using only recursion, but I keep getting this error message...
Output argument "answer" (and maybe others) not
assigned during call to "sum_of_fibo>fibo".
Error in sum_of_fibo (line 2)
summa = sum(fibo(n:-1:0));
Here's my code...can someone help?
function summa = sum_of_fibo(n)
summa = sum(fibo(n:-1:0));
end
function answer = fibo(n)
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 1
answer = fibo(n-1) + fibo(n-2);
end
end

 Accepted Answer

If you pass in something like -1 or 0.4, answer will never get set to anything. You should set it to something, even if it's null, to avoid that error. Better yet, make an "else" for your if to handle cases where you're passing in values that are not positive integers or zero.
function answer = fibo(n)
answer = []; % empty
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 1
answer = fibo(n-1) + fibo(n-2);
end
end

More Answers (1)

Any time you write if... elseif... statements you should always include a final else statement, even if you think you've covered every possibility. The final else statement can either define a default value for answer or in can throw an error.
Example 1
function answer = fibo(n)
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 1
answer = fibo(n-1) + fibo(n-2);
else
answer = -1; % Default value
end
Example 2
function answer = fibo(n)
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 1
answer = fibo(n-1) + fibo(n-2);
else
error('''n'' must be greater or equal to 0.') % throw error
end

9 Comments

So I should just do
...
else
error('positive integers only')
Would this fix it, or would I still need to initalize 'answer' to zero?
But 0 is neither positive nor negative and many users would interpret that error message as "any value >0".
Also, your 2nd condition allows for any positive number including 0.1111111111. If you want to limit the user to integers greater or equal to 0, you could do two things.
1) at the beginning of the function,
assert(isscalar(n) & mod(n,1)==0 & n>=0, '''n'' must be an integer greater or equal to 0.')
2) In this case, the else statement would never be reaced but just for safe measures, you could copy the error message there, too.
else
error('''n'' must be an integer greater or equal to 0.') % throw error
Gotcha, but it still is not quite working.
This is just the fibonacci series code:
function answer = fibo(n)
if n == 0
answer = 1;
elseif n == 1
answer = 1;
elseif n > 0
answer = fibo(n-1) + fibo(n-2);
else
fprintf(' "n" must be greater than or equal to 0')
end
end
When I do
3:-1:0
ans =
3 2 1 0
Then
fibo(ans)
Gives... 'n' must be greater than or equal to 0? Why is it not finding the value of fibonacci series for each n=3, n=2, n=1, and n=0?
First, the fprintf() statement does not solve the problem and it will actually throw an error because answer is still not defined. Take a moment to think about the response from Image Analyst and myself and why the fprintf solution doesn't fix the problem. The statement in your fprintf is also misleading since it tells users that they can enter non-integer values (unless you want that).
Second, if n can be a vector, all of the function needs changed. Conditional statements are not designed to test elements of an array. Instead, you want to use indexing which is the main power of Matlab.
All of your conditions can be replaced by:
answer = nan(size(n));
answer(n==0 | n==1) = 1;
answer(n > 1) = fibo(n(n > 1)-1) + fibo(n(n > 1)-2);
assuming the fibo function works properly.
Update: I just saw that the fibo() function is recursive so this won't due.
Ok, so I understand that the fprintf statement does not actually do anything, haha (now at least). I'm pretty new to MATLAB. Anyways, I fixed my sum_of_fibo function to incorporate recursion instead of trying to input a scalar into the original fibo() function because obviously that wasn't working.
Here's the updated version:
function summa = sum_of_fibo(n)
n = mod(n,1);
if n < 0
summa = 0;
else
summa = fibo(n) + sum_of_fibo(n-1);
end
end
function answer = fibo(n)
if n < 0
answer = [];
elseif n == 0
answer = 1;
elseif n == 1
answer = 1;
else
answer = fibo(n-1) + fibo(n-2);
end
end
But now it's saying that there's infinite recursion due to line 6 in sum_of_fibo(). I'm assuming what's happening now is that even though summa of n<0 is set to equal zero, the function will just add and infinite amount of zeros?
I understand this could probably be fixed with a while loop, but I'm trying not to use loops. I'm studying for an exam, and my professor often gives us questions that limit our ability to use built in functions or loops, haha.
have you tested this: n = mod(n,1)? That's not what you want to do.
Here's a hint for a simple solution that allows for vector input. This all goes in 1 m-file.
function answer = fibo(n)
answer = nan(size(n));
for i = 1:numel(n)
answer(i) = fibfcn( . . .);
end
function out = fibfcn(n)
% insert code
Thanks for all your help!
I was able to get it using this code:
function summa = sum_of_fibo(n)
if n == 0
summa = 1;
else
summa = fibo(n) + sum_of_fibo(n-1);
end
end
function answer = fibo(n)
if n < 0
answer = [];
elseif n == 0
answer = 1;
elseif n == 1
answer = 1;
else
answer = fibo(n-1) + fibo(n-2);
end
end
I don't think it's the most effective/efficient way to get to the right answer because it takes quite a while to run, but it uses recursion which is what I was going for. Thanks again for all your help!
Using the input [3 2 1 0] provided in a previous comment, the function above fails,
Out of memory. The likely cause is an infinite recursion within the program.
Error in sum_of_fibo (line 5)
summa = fibo(n) + jff(n-1);

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!