For loop for fibonacci series
548 views (last 30 days)
Show older comments
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
3 Comments
Lanrewaju Ibrahim Fajimi (Launchi)
on 23 Nov 2022
% I guess it should be n+1 term, once you run it as a function, you should get exactly what your need.
function fib5(n)
a = 0;
b = 1;
x = n+1;
% n = input('Enter number of term desired');
for i = 1:x %term for n
fprintf('\t')
fprintf('%d',a);
c = a + b;
a = b;
b = c;
end
Charles
on 28 Dec 2022
You will need to label your n parameter, It will be n = Fy (generate y). This will enable the program to run since for the sequence also, n = 0. In the ("enter the number of term desired"), generate y will be a better term.
Accepted Answer
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
7 Comments
Walter Roberson
on 25 Apr 2020
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
a(n+1:end) = []; %in case 1 term was requested, remove the second 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 (4)
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 Comments
JAYANTHI SANKARALINGAM
on 9 Oct 2020
I've a doubt in above program,it shows variable size must be -[2 1], current is[1 1]
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?
- 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.
- 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.
- 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.
- 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
Edited: DGM
on 11 May 2024
function fib=fiboI(n)
fib = [0,1];
for i = 3:n
fib = [fib, fib(i-1) + fib(i-2)];
end
end
1 Comment
Lanrewaju Ibrahim Fajimi (Launchi)
on 23 Nov 2022
Line 2;
fib = [1,1], % this will give the ideal sequence.
Nice code by the way
Zaki
on 21 May 2023
Write MATLAB program to generate the following series until the product of its terms exceeds 200,000,000, what is the number of terms of the
series in this case? 112 3 5 8 13 21 34 55 89 144 233 377
1 Comment
DGM
on 23 May 2023
limit = 200E6;
V = [1 1]; % we're necessarily starting at 1
k = 2; % the number of terms in V
while prod(V)<=limit
k = k+1;
V(k) = V(k-1)+V(k-2);
end
k
See Also
Categories
Find more on Loops and Conditional Statements 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!