Plotting partial sums of series
5 views (last 30 days)
Show older comments
Hello! I am trying to calculate and plot
on -pi≤x≤pi 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
0 Comments
Accepted Answer
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
More Answers (0)
See Also
Categories
Find more on Graphics Performance 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!