Clear Filters
Clear Filters

Fibonacci Series using while loop .

23 views (last 30 days)
Siddhesh
Siddhesh on 6 Jul 2023
Answered: Yash on 30 Jan 2024
This is the code I have written for the generation of Fibonnaci series for n < 200 using while loop .
%% Fibonacci series using while loop %%
n=200 ;
fibo = [1,1] ;
i = 3 ;
while i < n
fibo(i) = fibo(i-1)+fibo(i-2);
i=i+1 ;
end
But it gives me 0 for all values and gives garbage values for last few elements . What could be possible reason for this ?
  2 Comments
Torsten
Torsten on 6 Jul 2023
But it gives me 0 for all values and gives garbage values for last few elements . What could be possible reason for this ?
The reason is that n is too large.
Siddhesh
Siddhesh on 6 Jul 2023
Thank you for the help sir .

Sign in to comment.

Accepted Answer

Aditya Singh
Aditya Singh on 6 Jul 2023
Edited: Aditya Singh on 6 Jul 2023
Hi,
I understand you are getting garbage values. The issue in your code is that you have not initialized the fibo array with enough elements to store the Fibonacci series up to n. As a result, when you try to access elements beyond the initial two elements, you encounter garbage values. The following code works fine.
n = 200;
fibo = zeros(1, n); % Initialize the fibo array with zeros
fibo(1) = 1;
fibo(2) = 1;
i = 3;
while i <= n
fibo(i) = fibo(i-1) + fibo(i-2);
i = i + 1;
end
% Display the Fibonacci series
disp(fibo);
1.0e+41 * Columns 1 through 19 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 20 through 38 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 39 through 57 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 58 through 76 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 77 through 95 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 96 through 114 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 115 through 133 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 134 through 152 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 153 through 171 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 172 through 190 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0002 0.0003 0.0005 0.0008 0.0013 0.0021 0.0033 0.0054 0.0087 0.0141 0.0228 Columns 191 through 200 0.0369 0.0597 0.0966 0.1564 0.2530 0.4093 0.6623 1.0717 1.7340 2.8057
Also, just an additional information, keep in mind the range of integer or data type you are using. After a certain time, they would overflow and give you garbage values.
Hope it helps!

More Answers (2)

Mahesh Chilla
Mahesh Chilla on 6 Jul 2023
Edited: Mahesh Chilla on 6 Jul 2023
Hi Siddhesh!
To generate Fibonacci series for n < 200 using while loop, your code is correct, you might have missed noticing the mulitplying factor in the output that is 1.0e+41.
%% Fibonacci series using while loop %%
n=200 ;
fibo = [1,1] ;
i = 3 ;
while i < n
fibo(i) = fibo(i-1)+fibo(i-2);
i=i+1 ;
end
disp(fibo);
1.0e+41 * Columns 1 through 19 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 20 through 38 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 39 through 57 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 58 through 76 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 77 through 95 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 96 through 114 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 115 through 133 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 134 through 152 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 153 through 171 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 Columns 172 through 190 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0001 0.0001 0.0002 0.0003 0.0005 0.0008 0.0013 0.0021 0.0033 0.0054 0.0087 0.0141 0.0228 Columns 191 through 199 0.0369 0.0597 0.0966 0.1564 0.2530 0.4093 0.6623 1.0717 1.7340
You can also use the other method suggested by Aditya, which gives the same output as your method.
The following code verifies both methods
n = 200;
fib = [1, 1];
i = 3;
while i < n
fib(i) = fib(i-1) + fib(i-2);
i = i + 1;
end
% The resulting Fibonacci sequence is stored in the 'fib' array
n = 199; %changing n to 199, because the 'fib' array is of size 199
fibo = zeros(1, 199); % Initialize the 'fibo' array with zeros
fibo(1) = 1;
fibo(2) = 1;
i = 3;
while i <= n
fibo(i) = fibo(i-1) + fibo(i-2);
i = i + 1;
end
% The resulting Fibonacci sequence is stored in the 'fibo' array
% To check if 'fib' and 'fibo' are the same.
isequal(fib,fibo)
ans = logical
1
Hope this helps,
Thank you!!

Yash
Yash on 30 Jan 2024
I made this code by myself only and found it correct for every possible value :
N = 200;
a = zeros(1,N);
a(1) = 1;
a(2) = 2;
count = 3;
while (count<=N)
a(count) = a(count-1)+a(count-2);
count = count + 1;
end
disp(a(N));
4.5397e+41
I hope this code is helpful for everyone.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!