For loop for fibonacci series
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
Yixiang Guice
on 18 Nov 2018
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
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
0 votes
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
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
fib=[];
fib(1)=0;
fib(2)=1;
for i=3:10
fib(i)=fib(i-1)+fib(i-2);
end
disp(fib)
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!