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