How to compute the sum of the squares of all integers from 1 to 50 using for-loop?

57 views (last 30 days)
hi guys i am new to matlab so how to perform code this function
a=1:1:50
b=50:1:1
c=(a+b)^2
  2 Comments
SURENTHIRAN C
SURENTHIRAN C on 8 Apr 2014
no sir i don't need this i need here i hav given 2 input a and b then find c value........
for example a=1 means b=50(1+50)^2.....and a=2 then b=49(2+49)^2 like that i need

Sign in to comment.

Answers (3)

Mischa Kim
Mischa Kim on 8 Apr 2014
Edited: Mischa Kim on 8 Apr 2014
OK, use
mysum = 0;
for ii = 50:-1:1
mysum = mysum + ii*((51-ii) + ii)^2;
end
Of course you could simplify, since the term in the brackets always equals 51.

shivangi pawar
shivangi pawar on 18 Oct 2016
how to form a loop of average of square of 1 upto 512

Diyana  Abdulahad
Diyana Abdulahad on 15 Mar 2022
Output the sum of the squares of the integers from 1 to 10.
  3 Comments
Walter Roberson
Walter Roberson on 28 Mar 2022
sum_of_squares = @(n) (n*(2*n + 1)*(n + 1))/6
sum_of_squares = function_handle with value:
@(n)(n*(2*n+1)*(n+1))/6
N = 1;
while N <= 50
[N, sum_of_squares(N)]
N = N + 1;
end
ans = 1×2
1 1
ans = 1×2
2 5
ans = 1×2
3 14
ans = 1×2
4 30
ans = 1×2
5 55
ans = 1×2
6 91
ans = 1×2
7 140
ans = 1×2
8 204
ans = 1×2
9 285
ans = 1×2
10 385
ans = 1×2
11 506
ans = 1×2
12 650
ans = 1×2
13 819
ans = 1×2
14 1015
ans = 1×2
15 1240
ans = 1×2
16 1496
ans = 1×2
17 1785
ans = 1×2
18 2109
ans = 1×2
19 2470
ans = 1×2
20 2870
ans = 1×2
21 3311
ans = 1×2
22 3795
ans = 1×2
23 4324
ans = 1×2
24 4900
ans = 1×2
25 5525
ans = 1×2
26 6201
ans = 1×2
27 6930
ans = 1×2
28 7714
ans = 1×2
29 8555
ans = 1×2
30 9455
ans = 1×2
31 10416
ans = 1×2
32 11440
ans = 1×2
33 12529
ans = 1×2
34 13685
ans = 1×2
35 14910
ans = 1×2
36 16206
ans = 1×2
37 17575
ans = 1×2
38 19019
ans = 1×2
39 20540
ans = 1×2
40 22140
ans = 1×2
41 23821
ans = 1×2
42 25585
ans = 1×2
43 27434
ans = 1×2
44 29370
ans = 1×2
45 31395
ans = 1×2
46 33511
ans = 1×2
47 35720
ans = 1×2
48 38024
ans = 1×2
49 40425
ans = 1×2
50 42925

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!