Fibonacci function displaying issues
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Image Analyst
2012년 10월 19일
Ashlee, I suggest you add this line at the beginning of the function to help you debug it:
fprintf('n = %d\n', n);
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!