MATLAB: What is wrong with the following while loop

fibonacci numbermatlab function

function [F] = get_fib(k)
get_fib(1) = 1;
get_fib(2) = 1;
get_fib(3) = 2;
i = 3;
k >= 0;
while i<=k;
[F] = get_fib(i-1) + get_fib(i-2);
i = i + 1;
end

Best Answer

As soon as you did
get_fib(1) = 1;
then you redefined "get_fib" inside the function so that it no longer refers to the function and instead refers to an array of double.
If you are intending to work recursively then you should not have the loop. If you are intending to not work recursively then you need the output, F, to be assigned to get_fib(k)
Related Question