"Matrix dimensions must agree" when trying to plot the sum of a sin function

1 vue (au cours des 30 derniers jours)
x=[-pi:pi/100:2*pi]
x = 1×301
-3.1416 -3.1102 -3.0788 -3.0473 -3.0159 -2.9845 -2.9531 -2.9217 -2.8903 -2.8588 -2.8274 -2.7960 -2.7646 -2.7332 -2.7018 -2.6704 -2.6389 -2.6075 -2.5761 -2.5447 -2.5133 -2.4819 -2.4504 -2.4190 -2.3876 -2.3562 -2.3248 -2.2934 -2.2619 -2.2305
figure
for n=20
m = (1:n).';
s2 = sin((2*m-1)*x)/(2*m-1);
plot(x,s2, 'displayname', "\alpha = " + n);
hold on
end
Error using /
Matrix dimensions must agree.
legend show
I get this error when I'm trying to plot for n = 20
What did I do wrong?
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 3 Déc 2022
What exactly are you trying to plot?
Is n just equal to 20 or is it equal to 20 * (sum of the series) ?
Jordi van Selm
Jordi van Selm le 3 Déc 2022
n=20 and I would only want to get the function for the sum until n=20

Connectez-vous pour commenter.

Réponse acceptée

VBBV
VBBV le 3 Déc 2022
s2 = sin((2*m-1)*x)./(2*m-1);
Use element wise division
  7 commentaires
VBBV
VBBV le 3 Déc 2022
Yes. You get the summation of the expression for values upto 20.
Jordi van Selm
Jordi van Selm le 3 Déc 2022
Thanks again. My last job was to merge two function to get:
x=[-pi:pi/100:2*pi]
x = 1×301
-3.1416 -3.1102 -3.0788 -3.0473 -3.0159 -2.9845 -2.9531 -2.9217 -2.8903 -2.8588 -2.8274 -2.7960 -2.7646 -2.7332 -2.7018 -2.6704 -2.6389 -2.6075 -2.5761 -2.5447 -2.5133 -2.4819 -2.4504 -2.4190 -2.3876 -2.3562 -2.3248 -2.2934 -2.2619 -2.2305
figure
n=20
n = 20
m = (1:n).';
s1 = (1/m*sin(m*x));
s2 = sin((2*m-1)*x)./(2*m-1);
plot(x,sum(s1,1),x,sum(s2,1), 'displayname', "\alpha = " + n);
legend show

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by