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

1 view (last 30 days)
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 Comments
Dyuman Joshi
Dyuman Joshi on 3 Dec 2022
What exactly are you trying to plot?
Is n just equal to 20 or is it equal to 20 * (sum of the series) ?

Sign in to comment.

Accepted Answer

VBBV
VBBV on 3 Dec 2022
s2 = sin((2*m-1)*x)./(2*m-1);
Use element wise division
  7 Comments
Jordi van Selm
Jordi van Selm on 3 Dec 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

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!