MATLAB: Plotting partial sums of series

for loopplottingsummation

Hello! I am trying to calculate and plot on -pixpi for k=1:1:20. I want to plot each partial sum with a pause and retain the plots with my hold on function. So far I have the code written below. I have tried writing it as a function but I don't understand them. I have included some of my reasoning below. Thank you so much for any help you can provide.
x = -pi:0.1:pi ; S=x; %initializing x
for n=1:20 %n is loop index
x = sin(n*x)/n ;
S(n)= x ;%storing the partial sum
hold on %I'm trying to retain each plot but it plots them all on the same plot
n=length(S);
plot(n,S)%I have problems when trying to plot the product as if I
%use x = -pi:0.1:pi) the length isn't the same and it won't plot anything
pause(0.5)
end

Best Answer

Do you mean something like this?
x = -pi:0.1:pi ; %initializing x
S = zeros(20,numel(x));
figure
xlim([1 20])
grid
hold on
for n=1:20 %n is loop index
S(n) = sum(sin(n*x)/n) ; %storing the partial sum
plot(1:n,S(1:n),'k')
pause(0.5)
end