1

I want to plot the partial sums of the reciprocals of the squares between 1 and 10.

So far, I've got the following code. It's something along these lines, but I can't quite manage it. I need to sum the terms so far for each point between 1 and 10, but all I know how to do is sum all the terms.

for k = 1:10
   S(k) = 1/k^2; 
end
plot(1:10, sum(S(1:10))
horchler
  • 3,203
M4thGy
  • 11

2 Answers2

2

You are probably looking for "cumsum", cumulative sum. Loops are slow in Matlab.

plot(1:10,cumsum(1./[1:10].^2));

mathreadler
  • 25,824
0

Add the following loop:

for k=1:10 SUM (k) = sum ( S (1:k) ); end

And then

plot (1:10,SUM)

Eric S.
  • 2,800