Plotting partial sums of series

39 vues (au cours des 30 derniers jours)
Ollie Lehki
Ollie Lehki le 22 Fév 2021
Commenté : Ollie Lehki le 25 Fév 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

Réponse acceptée

Alan Stevens
Alan Stevens le 22 Fév 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 commentaire
Ollie Lehki
Ollie Lehki le 25 Fév 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by