Fibonacci function displaying issues
Show older comments
I created a function for a fibonacci number. I want it to display the largest fibonacci number that is less than or equal to the input. I currently have it set up to run and it will display the last number in the series. Any suggestions as to getting it to display the largest number less than or equal to n?
function f=lastfibonacci(n)
% Returns the largest fibonacci number that is less than or equal to n.
if n<=1;
f=1;
else
f=(lastfibonacci(n-1)+lastfibonacci(n-2));
end
end
Wanting:
Ex.: lastfibonacci(7)
ans= 5
Accepted Answer
More Answers (1)
Image Analyst
on 19 Oct 2012
Ashlee, I suggest you add this line at the beginning of the function to help you debug it:
fprintf('n = %d\n', n);
Categories
Find more on Creating and Concatenating Matrices 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!