# For loop for fibonacci series

2,487 views (last 30 days)
Yixiang Guice on 18 Nov 2018
Answered: IOANNIS KIPIOTIS on 15 Aug 2022
i am supposed to write a fibonacci series that calculate up to n term and (n-1) term but i am stuck at calculating the (n-1)term. can anyone help? ( i am new to matlab)
a = 0;
b = 1;
x = n-1;
n = input('Enter number of term desired');
for i = 1:n %term for n
fprintf('\t')
fprintf('%d',a);
c = a + b;
a = b;
b = c;
end
for i = n:x %term for n-1
fprintf('\t')
fprintf('%d',a);
c = a + b;
a = b;
b = c;
end
##### 1 CommentShowHide None
Yixiang Guice on 18 Nov 2018
I can include the equation given for this question if required

Aquatris on 18 Nov 2018
Edited: Aquatris on 18 Nov 2018
In case of series, it is better to store each value. Please see below code that generatesa fibonacci sequence, and stores all the values in the variable"a";
a(1) = 0;
a(2) = 1;
n = input('Enter number of term desired ');
for i = 3:n
a(i) = a(i-1)+a(i-2);
end
From here, if you want the n'th term, you do a(n), if you want the n-1, then you do a(n-1).
You method of trying to find the n-1 term can work with the following modification;
a = 0;
b = 1;
n = input('Enter number of term desired');
for i = 1:n-2 %term for n
c = a + b;
a = b;
b = c;
end
a_n = c; % nth term
a_n1 = a; %(n-1) term
John D'Errico on 25 Apr 2020
Edited: John D'Errico on 25 Apr 2020
One thing that people seem not to appreciate is that the standard definition of a Fibonacci sequence has the ZERO'TH index element of that series as 0.
So, yes, you can start the sequence at an index of 0. In which case the sequence will run
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...
However if you look, that starts from an index of 0. This means F_0 = 0, F_1 = 1, F_2 = 1, F_3 = 2, etc.
We can even see this mirrored in the symbolic toolbox code to produce the nth fibonacci number.
fibonacci(sym(0))
ans =
0
fibonacci(sym(1))
ans =
1
But be careful. If you are asked to write code to produce the nth member of that sequence, for n==10, you should still arguably return 55. If your code produces 34, then you will be off by an index.
As we see from the symbolic toolbox code to generate the Fibonacci numbers, it understands that.
fibonacci(sym(10))
ans =
55
Also, yes, you can start the sequence off with OTHER numbers, but that will NOT be the Fibonacci sequence any more. In fact, you can also extend the Fibonacci sequence to negative indices, just by running that recurrence relation backwards.
Next, had you started the sequence off with the numbers 2 and 1? This does NOT generate the Fibonacci sequence, but a sequence known as the Lucas numbers.
The Lucas sequence has the same 3 term recurrence relation, but they start differently. So if we start with the 0'th index element, the terms with index 0:9 are:
2, 1, 3, 4, 7, 11, 18, 29, 47, 76, ...
With a generating recurrence relation of:
L(n) = L(n-1) + L(n-2)
Which as you should see, is the same as for the Fibonacci sequence. So they act very much like the Fibonacci numbers, almost. In fact, you can go more deeply into this rabbit hole, and define a general such sequence with the same 3 term recurrence relation, but based on the first two terms of the sequence. So given two co-prime numbers (integers) p and q, if we start out the sequence with p and q, then we will generate a corresponding sequence of numbers using the same three term recurrence. Co-prime is important, since otherwise all terms of the sequence will have a common factor.

### More Answers (3)

Syeda Atiya on 24 Jan 2020
a = 0;
b = 1;
n = input('Enter number of term desired');
for i = 1:n-2 %term for n
c = a + b;
a = b;
b = c;
end
a_n = c; % nth term
a_n1 = a; %(n-1) term

Syed Shahed on 24 Apr 2020
%I tried to rewrite and genaralized the code and it was successfully run.
%Check the code
function a=fibonacciseries(n)
a(1)=1; %First number in the sequence
for i =3:n %Execution starts from n=3 as the first two numbers in the fibonacci sequence are 1
a(2)=1; %Second number in the sequence
a(i) = a(i-1)+a(i-2); %Thats the fibonacci series
%If you want the series only then avoid the if portion of the code
if(i==n) %Thats the indexing of n th number
a=a(i); %Its the n th number
end
end
##### 2 CommentsShowHide 1 older comment
John D'Errico on 9 Oct 2020
@JAYANTHI SANKARALINGAM -
You would be wrong in your doubt. While this code is relatively poor, it will work.
Why will it work? Because the author has started the loop at the THIRD element, predefining the first and second elements of the vector a.
Why is this really poorly written code?
1. a(2) =1 is defined INSIDE the loop. So every iteration of the loop redefines a(2), as 1. When you will predefine something like this, put it BEFORE the loop starts. Not inside the loop.
2. The vector a is not preallocated. That forces MATLAB to grow the vector in length every pass through the loop. That in turn means MATLAB needs to reallocate a new vector of length one element longer than the last, at EVERY iteration. And then it needs to copy over all previous elements each pass through the loop. This process gets longer and longer to execute every pass through.
3. Finally, Again, INSIDE the loop, the author has put a test on the number i. This is again poor code. Put one final line of code after the loop terminates, a = a(end); There is no need even for a test on the value of i.
4. What happens if n is 1 or 2? Will this loop return anything, or will it generate a garbage result? In fact, the loop will generate the correct result of 1, which is what F(1) and F(2) are for the Fibonacci sequence. But I would almost argue this is a lucky happenstance, not by good program design.
But, will it work? Well, yes. It will just work poorly.

IOANNIS KIPIOTIS on 15 Aug 2022
function fib=fiboI(n)
fib = [0,1];
for i = 3:n
fib = [fib, fib(i-1) + fib(i-2)];
end
end