For loop for fibonacci series
古いコメントを表示
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 件のコメント
Yixiang Guice
2018 年 11 月 18 日
Lanrewaju Ibrahim Fajimi (Launchi)
2022 年 11 月 23 日
% 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
2022 年 12 月 28 日
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.
採用された回答
その他の回答 (4 件)
Syed Shahed
2020 年 4 月 24 日
%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 件のコメント
JAYANTHI SANKARALINGAM
2020 年 10 月 9 日
I've a doubt in above program,it shows variable size must be -[2 1], current is[1 1]
John D'Errico
2020 年 10 月 9 日
@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
2022 年 8 月 15 日
編集済み: DGM
2024 年 5 月 11 日
function fib=fiboI(n)
fib = [0,1];
for i = 3:n
fib = [fib, fib(i-1) + fib(i-2)];
end
end
1 件のコメント
Lanrewaju Ibrahim Fajimi (Launchi)
2022 年 11 月 23 日
Line 2;
fib = [1,1], % this will give the ideal sequence.
Nice code by the way
Zaki
2023 年 5 月 21 日
0 投票
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 件のコメント
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)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
