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

25 次查看(过去 30 天)
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

回答(3 个)

Mischa Kim
Mischa Kim 2014-4-8
编辑:Mischa Kim 2014-4-8
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 2016-10-18
how to form a loop of average of square of 1 upto 512

Diyana  Abdulahad
Output the sum of the squares of the integers from 1 to 10.
  3 个评论
Walter Roberson
Walter Roberson 2022-3-28
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by