Graphing partial sums of a series
71 views (last 30 days)
Show older comments
Im trying to graph the following series (see picture 1). in matlab but i cant figure out what im doing wrong.
for the first two I have this code:
x = -5*pi : 0.01 : 5*pi;
S1 = 0;
for n = 1 : 100
S1 = S1 + (2*((1/2*n-1)*(sin((2*n-1)*(x))))-2*((1/2*n)*(sin((2*n)*(x)))));
end
figure(1);
p1 = plot(x,S1);
p1(1).Color = [0.6,0.3,0.3];
p1(1).LineWidth = 1.5;
title 'Series 1'
xlabel 'Time'
ylabel 'Series'
grid on
S2 = 1/2;
for n = 1:100
S2 = S2 + (4/pi^2)*(1/(2*n-1)^2)*(cos((2*n-1)*(x)));
end
figure(2);
p2 = plot(x,S2);
p2(1).Color = [0.2,0.1,0.9];
p2(1).LineWidth = 1.5;
title 'Series 2'
xlabel 'Time'
ylabel 'Signal'
grid on
for S2 I am getting the same graph but for S1 I am not.
Also how can I increase the size of the y-axis on both graphs?
Thanks, any help is appreciated.
0 Comments
Accepted Answer
Voss
on 2 Apr 2024 at 18:05
In your expression for S1:
S1 = S1 + (2*((1/2*n-1)*(sin((2*n-1)*(x))))-2*((1/2*n)*(sin((2*n)*(x)))));
You need parentheses around the denominators 2*n-1 and 2*n:
S1 = S1 + (2*((1/(2*n-1))*(sin((2*n-1)*(x))))-2*((1/(2*n))*(sin((2*n)*(x)))));
Of course, many of those other parentheses are unnecessary (no doubt inspired by all the unnecessary parentheses in the question statement itself); you could rewrite it as:
S1 = S1 + 2/(2*n-1)*sin((2*n-1)*x)-1/n*sin(2*n*x);
Now the plot seems to be correct:
x = -5*pi : 0.01 : 5*pi;
S1 = 0;
for n = 1 : 100
S1 = S1 + 2/(2*n-1)*sin((2*n-1)*x)-1/n*sin(2*n*x);
end
figure(1);
p1 = plot(x,S1);
p1(1).Color = [0.6,0.3,0.3];
p1(1).LineWidth = 1.5;
title 'Series 1'
xlabel 'Time'
ylabel 'Series'
grid on
Similarly, removing unnecessary parentheses in the expression for S2:
S2 = 1/2;
for n = 1:100
S2 = S2 + 4/pi^2/(2*n-1)^2*cos((2*n-1)*x);
end
figure(2);
p2 = plot(x,S2);
p2(1).Color = [0.2,0.1,0.9];
p2(1).LineWidth = 1.5;
title 'Series 2'
xlabel 'Time'
ylabel 'Signal'
grid on
Additionally, each of those calculations can easily be vectorized (that is, done using element-wise operations like .*, ./, and .^ and without for loops, using the sum function to perform the summation):
x = -5*pi : 0.01 : 5*pi;
n = ( 1 : 100 ) .'; % <- transpose
S1 = sum( 2./(2*n-1).*sin((2*n-1).*x)-1./n.*sin(2*n.*x) , 1 ); % <- sum over 1st dimension, i.e., n
figure
plot(x,S1,Color=[0.6,0.3,0.3],LineWidth=1.5)
title('Series 1'); xlabel('Time'); ylabel('Series'); grid on
S2 = 1/2 + sum( 4/pi^2./(2*n-1).^2.*cos((2*n-1).*x) , 1);
figure
plot(x,S2,Color=[0.2,0.1,0.9],LineWidth=1.5)
title('Series 2'); xlabel('Time'); ylabel('Signal'); grid on
0 Comments
More Answers (1)
Torsten
on 2 Apr 2024 at 18:03
x = -5*pi : 0.01 : 5*pi;
S1 = zeros(size(x));
for n = 1 : 100
S1 = S1 + 2/(2*n-1)*sin((2*n-1)*x)-2/(2*n)*sin(2*n*x);
end
figure(1);
p1 = plot(x,S1);
p1(1).Color = [0.6,0.3,0.3];
p1(1).LineWidth = 1.5;
title 'Series 1'
xlabel 'Time'
ylabel 'Series'
grid on
S2 = 1/2*ones(size(x));
for n = 1:100
S2 = S2 + 4/pi^2*1/(2*n-1)^2*cos((2*n-1)*x);
end
figure(2);
p2 = plot(x,S2);
p2(1).Color = [0.2,0.1,0.9];
p2(1).LineWidth = 1.5;
title 'Series 2'
xlabel 'Time'
ylabel 'Signal'
grid on
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!