implement a script that calculates 1^3+2^3+...+10^3+11^3. a) use a for or a while loop. b)use vectors

1 回表示 (過去 30 日間)
thank you !

採用された回答

Kevin Phung
Kevin Phung 2019 年 1 月 24 日
編集済み: Kevin Phung 2019 年 1 月 24 日
x = 1:11;
%for loop
sum=0;
for i = 1:numel(x)
sum = sum + x(i)^3;
end
%vector method
vec = sum(x.^3);
  3 件のコメント
Jan
Jan 2019 年 1 月 28 日
編集済み: Jan 2019 年 1 月 28 日
@Kevin: Your code will fail, because you have defined "sum" as a variable at first and try to call the function sum() afterwards. Do not use the names of important built-in functions as names of variables.
This was a homework question obviously. Now the OP has removed the question, such that this threads becomes meaningless - the 3rd time. This is a counter-productive and impolite behaviour.

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

その他の回答 (0 件)

カテゴリ

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