フィルターのクリア

Fibonacci Sequence while loop.

1 回表示 (過去 30 日間)
Angel Martinez
Angel Martinez 2012 年 4 月 5 日
Trying to create a while loop and I'm having a hard time. I have one with the for loop but the while loop is giving me a heart ache. Thanks
%% Problem 9.6: Fibonacci Sequence
clc clear max =input('Enter the total number of elements: ');
n=zeros(max,1);
n(1)=input('Enter the first number: ');
n(2)=input('Enter the second number: ');
for i=3:max
n(i)=n(i)+n(i-1)+n(i-2)
end

回答 (3 件)

TheTaoism
TheTaoism 2012 年 4 月 5 日
How about this one?
function output = FibSeq (nelements,startingPoint)
output = zeros (1,nelements);
output(1,1:length(startingPoint)) = startingPoint;
i = length(startingPoint)+1;
while i <= nelements ,
output(i) = output(i-1) + output(i-2);
i=i+1;
end
The startingPoint can be a matrix that you input, for example [0,1], and nelements is the same as max in your program.
  1 件のコメント
Angel Valenzuela-Delgado
Angel Valenzuela-Delgado 2019 年 12 月 6 日
okay boomer

サインインしてコメントする。


Javier
Javier 2013 年 7 月 19 日
%s. de fibonacci
f = [1,1];
i1 = 1
while f(i1) + f(i1+1) <100;
f(i1+2) = f(i1) + f (i1+1);
end

Tasbiha Azeemi
Tasbiha Azeemi 2018 年 7 月 1 日
Try this one!
function a=fabonacciSeq(n)
a(1)=0;
a(2)=1;
i=1;
while i<=n
a(i+2)=a(i+1) +a(i);
i=i+1;
end
end
  1 件のコメント
Jan
Jan 2018 年 7 月 1 日
See How to format code. A pre-allocation is recommended.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by