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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by