How do I display the first 20 numbers of the Fibonacci sequence?

6 ビュー (過去 30 日間)
Seth Broberg
Seth Broberg 2020 年 9 月 27 日
コメント済み: Sindar 2020 年 9 月 28 日
Need a for loop that displays the first 20 numbers of the Fibonacci sequence.
Here is my code so far
a=1
i=3
for a=1:20
f(i)=f(i-1)+f(i-2);
a+1
i+1
end
disp(f)
it currently displays as the output.
function_handle with value:
@(x,y)x.^2+y.^2

回答 (1 件)

Sindar
Sindar 2020 年 9 月 28 日
you need to define the first two numbers manually, then start on the third
f(1) = 0;
f(2) = 1;
for a=3:20
...
Also, it's unclear what you are doing with 'i'
  2 件のコメント
Seth Broberg
Seth Broberg 2020 年 9 月 28 日
so I have this code written
a=1
f(1)=0
f(2)=1
for a=3:20
f(i)=f(i-1)+f(i-2);
a+1
end
disp(f)
and am now getting this error
Array indices must be positive integers or logical values.
Error in Week2 (line 56)
f(i)=f(i-1)+f(i-2);
Sindar
Sindar 2020 年 9 月 28 日
  • what value is i taking here?
  • do you understand what for a=3:20 does?
(also, you might want to check out the Matlab Onramp)

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

カテゴリ

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