Plot the partial sums of a series - Matlab.

843 Views Asked by At

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))
2

There are 2 best solutions below

0
On

Add the following loop:

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

And then

plot (1:10,SUM)

0
On

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

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