Plotting partial sums of series
39 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ollie Lehki
le 22 Fév 2021
Commenté : Ollie Lehki
le 25 Fév 2021
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 commentaires
Réponse acceptée
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
Plus de réponses (0)
Voir également
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!