MATLAB: I have been faced with the ‘Index exceeds array bounds’. How to solve it

if statementwhile loop

function f = fibonacci2(max)
f(1)=0;
f(2)=1;
k=3;
while f < max
f(k)=f(k-1)+f(k-2);
if f (k) > max
f(k)=[]
end
k = k + 1;
end

Best Answer

function f=fibonacci2(max)
f(1)=0;
f(2)=1;
k=3;
while f < max
f(k)=f(k-1)+f(k-2);
if f (k) > max
f(k)=[]
else
f(k)=f(k-1)+f(k-2);
end
k = k + 1;
end
Related Question