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

1 Ansicht (letzte 30 Tage)
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 3 Dez. 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 am 3 Dez. 2022
n=20 and I would only want to get the function for the sum until n=20

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

VBBV
VBBV am 3 Dez. 2022
s2 = sin((2*m-1)*x)./(2*m-1);
Use element wise division
  7 Kommentare
VBBV
VBBV am 3 Dez. 2022
Yes. You get the summation of the expression for values upto 20.
Jordi van Selm
Jordi van Selm am 3 Dez. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by