Plotting partial sums of series

41 views (last 30 days)
Ollie Lehki
Ollie Lehki on 22 Feb 2021
Commented: Ollie Lehki on 25 Feb 2021
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

Accepted Answer

Alan Stevens
Alan Stevens on 22 Feb 2021
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
  1 Comment
Ollie Lehki
Ollie Lehki on 25 Feb 2021
I ended up doing something similar! My professor doesn't allow us to use the sum command so I had to get a littel crafty. Posting this incase anyone else ever needs it.
x = -pi:0.1:pi ; S = 0; %initializing x
for n=1:20 %n is loop index
S = S + sin(n*x)/n ;
plot(x, S)
hold on %to put them all on the same plot
pause(1) %to put a pause of 1 sec while graphing
end

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!