Cumulative sum with a for loop

I need to create a program for 1^2 +2^2 +...+1000^2
Both vectorised and with for loops.
I have managed the vector one I think:
x=1:1000
xsums=cumsum(x.^2)
y=xsums(1000)
however for the the for loop version of the program I can't seem to get it, what I have made is :
x=1:1000
for n = 1:length(x)
y=sum(n.^2)
end
I'm also not even sure if that is the right idea.
any help would be great thanks

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 25 de Oct. de 2017
"I need to create a program for 1^2 +2^2 +...+1000^2"
sum((1:1000).^2)
or
s = 0;
for ii = 1:1000
s = s + ii^2;
end

5 comentarios

KL
KL el 25 de Oct. de 2017
@Phil Whitfield: The difference between using cumsum (you don't even need it!) and Andrei's solution is worth noting.
>> s = sum((1:1000).^2);
>> x=1:1000;
>> xsums=cumsum(x.^2);
>> whos s xsums
Name Size Bytes Class Attributes
s 1x1 8 double
xsums 1x1000 8000 double
the size difference is 1000 times! Imagine you're doing it for a much more bigger number.
Jan
Jan el 25 de Oct. de 2017
Editada: Jan el 25 de Oct. de 2017
This is a homework question. Phil has shown his own effort and posted almost running code. Therefore I consider it as okay and useful to provide working solutions. +1
The loop has the advantage here, that it does not use a lot of temporary memory. It would run with n=1e12 also on a 8GB machine, in opposite to the vectorized version.
how would I do the same thing for 1- (1/2) + (1/3) -(1/4)... +(1/999)?
I have managed this so far:
i=0
z=0
b=0
x=-1;
for a=1:999;
if mod(a,2)==0
i=x./a
elseif mod(a,2)~=0
z=1./a
end
b= b+(i+z)
end
disp(b)
and
sum((1./(1:2:1000))-(1./(2:2:1000)))
I'm pretty sure the second one is right but I cant get the first to give the same answer.
I really appreciate it, thanks
Stephen23
Stephen23 el 26 de Oct. de 2017
Editada: Stephen23 el 26 de Oct. de 2017
Going up to 1000 gives the wrong answer. Try something like these:
>> 1+sum((1./(3:2:999))-(1./(2:2:999)))
ans = 0.693647430559821
>> sum(1./(1:2:999))-sum(1./(2:2:999))
ans = 0.693647430559813
loop, gives same output:
>> b = 0;
>> for k=2:2:999, b=b-1/k; end
>> for k=1:2:999, b=b+1/k; end
>> b
b = 0.693647430559823
Note that these only differ at the 14th significant figure.
Jan
Jan el 27 de Oct. de 2017
@Phil:
S = 0;
for a = 1:999
S = S + (-1)^(a-1) / a;
end
Or without the expensive power operation:
S = 0;
m = 1;
for a = 1:999
S = S + m / a;
m = -m;
end

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 25 de Oct. de 2017
Editada: Jan el 25 de Oct. de 2017
Further solutions:
  • DOT product:
v = 1:n;
s = v * v.';
This uses one temporary vector only, while sum(v .^ 2) needs to create two of them: v and v.^2 .
  • Avoid the squaring: The elements of 1^2, 2^2, 3^2, ... are:
1, 4, 9, 16, 25
The difference is
3, 5, 7, 9
with an obvious pattern. Then:
s = sum(cumsum(1:2:2*n))
This is cheaper as squaring the elements. As loop:
s = 0;
c = 1;
d = 1;
for ii = 1:n
s = s + d;
c = c + 2;
d = d + c;
end
Only additions, but s = s + ii * ii is nicer and slightly faster.
  • Finally remember C.F. Gauss, who provided some methods to process the results of sums efficiently:
s = n * (n+1) * (2*n+1) / 6
Nice!

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 25 de Oct. de 2017

Comentada:

Jan
el 27 de Oct. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by