My code is not adding up in the way that it should

1 回表示 (過去 30 日間)
Michael Eugene Carter
Michael Eugene Carter 2022 年 1 月 27 日
回答済み: Reshma Nerella 2022 年 2 月 2 日
When I try to run this code, for whatever reason it just returns a bunch of zeros, even though I set the initial value to 2, I don't understand what I am doing wrong here. The code is supposed multiply each number of the array by the result of the previous array number, the function is 3x+1
% initialize the array as zeros
x = zeros(1,1000);
% fill in the first entry (doesn't follow the pattern)
x(1)=2;
% use a for loop to fill in the rest of the entries (that follow the pattern)
for i = 2:1000;
x(i) = 3*(x(i-1))+1
end
  1 件のコメント
Torsten
Torsten 2022 年 1 月 27 日
For me, it works fine after removing the ; after the line "for i=2:1000;"

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

回答 (1 件)

Reshma Nerella
Reshma Nerella 2022 年 2 月 2 日
Hi,
The values are not zeros, it has a muliplying factor of 1.0e+308, which can be seen when the variable is displayed in command window. Open the variable in the variable editor, to get the exact values at indices.
The semicolon after 'for' statement doesn't effect the result, but it is unnecessary.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by