Here is a .m file what I made for Fibonacci Series:
function F = Fibonacci(n)%Fibonacci Series: Calculate the nth term
%The Fibonacci numbers are the sequence 0, 1, 1, 2, 3, 5, 8, 13, 21….
%Given that the first two numbers are 0 and 1, the nth Fibonacci number is
%Fn = Fn–1 + Fn–2.
%Applying this formula repeatedly generates the Fibonacci numbers.
F(1) = 0;F(2) = 1;i = 3;while i <= n F(i) = F(i-1)+F(i-2); i = i+1;endF = F(n);end
If I input n is single number such as:
n=10;Fibonacci(n)
It can give me the correct answer:
ans = 34
However, if I input n is an array, such as:
n=1:10;Fibonacci(n)
It gives the error warning:
Index exceeds the number of array elements (2).Error in Fibonacci (line 15)F = F(n);
I just wonder why it occurs this information?
Best Answer