MATLAB Answers

calculate summation of vectors

조회 수: 11(최근 30일)
Kha Le
Kha Le 2021년 9월 17일
댓글: Kha Le 2021년 9월 18일
write a code with no loop that will calculate the following sum:
b = 500 + 501^2 + 502 + 503^2 +...+ (N-1)^2 + N, where N = 1000

채택된 답변

William Rose
William Rose 2021년 9월 17일
@Kha Le, This sounds a lot like a homework question. Hints:
Break it into two parts: the sum of the unsquared terms plus the sum of the squared terms.
Read help for linspace.
Read the Matlab help for element-wise power, .^, especially example 1.
Read the Matlab help for sum.
If you still don't know, then show what you have tried that did not work, and show if you got an error message, or you got an answer that you know is the wrong answer, etc.
  댓글 수: 1
Kha Le
Kha Le 2021년 9월 18일
thanks for your hint. I figured it out :)

댓글을 달려면 로그인하십시오.

추가 답변(2개)

Hernia Baby
Hernia Baby 2021년 9월 17일
편집: Hernia Baby 2021년 9월 17일
I'll just write a hint.
Even = 0:2:10
Even = 1×6
0 2 4 6 8 10
Odd = 1:2:10
Odd = 1×5
1 3 5 7 9
Square = (1:10).^2
Square = 1×10
1 4 9 16 25 36 49 64 81 100
Sum = sum(1:10)
Sum = 55
  댓글 수: 3
Image Analyst
Image Analyst 2021년 9월 17일
If it wasn't tagged as homework, but you really suspect it is homework, you can give hints. You can ask them if it's their homework, and if they say no, then you can go on and give a full solution. If they say yes, I make sure it's tagged as homework (in case they didn't do it themselves) and I give them this link:

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2021년 9월 17일
Hint:
oddTerms = 501 : 2 : N;
evenTerms = 500 : 2 : N;
  댓글 수: 1
Kha Le
Kha Le 2021년 9월 18일
thanks for your hint. I figured it out

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by