Reputation: 15
a=[0,pi/2,pi/2,pi];
b=[0,-1,0,0];
plot(a,b)
hold on
n=50;
ao=-pi/4;
T=[0,pi,0,1];
for i=1:length(T)
t=linspace(a(i),b(i));
end
suma=0;
for i=1:n
bn=cos(pi*i)/2/i;
an=(1-cos(pi*i))/4/(i)^2;
suma=suma+(bn.*sin(2.*i.*t))+(an.*cos(2.*i.*t));
end
series=ao/2+suma;
plot(t,series)
I need to plot two functions: determined function and its Fourier series, but I have some problem in plotting Fourier, it's not combined with the first one. I can guess that the problem in t
or in T
, but have no idea how to fix it.
Upvotes: 1
Views: 288
Reputation: 60
First, your coefficients are incorrect. You should get:
a0 = -.5;
an = -(1/pi)*sin(pi*i)/i + (2/(pi^2*i^2))*sin((pi*i)/2)^2;
bn = -(1/(pi^2*i^2))*sin(pi*i) + 1/(pi*i)*cos(pi*i);
Then, as others pointed out, you want to use a single, long time vector:
t = linspace(a(1),a(end),1e3);
Using this code produces the graph you desire
a = [0,pi/2,pi/2,pi];
b = [0,-1,0,0];
plot(a,b)
hold on
n = 50;
T = [0,pi,0,1];
a0 = -.5;
t = linspace(a(1),a(end),1e3);
suma=0;
for i=1:n
bn = -(1/(pi^2*i^2))*sin(pi*i) + 1/(pi*i)*cos(pi*i);
an = -(1/pi)*sin(pi*i)/i + (2/(pi^2*i^2))*sin((pi*i)/2)^2;
suma = suma+(bn.*sin(2.*i.*t))+(an.*cos(2.*i.*t));
end
series = a0/2 + suma;
plot(t,series)
It should be noted that since this function is not smooth (there is a jump discontinuity) you will experience the Gibbs phenomenon and will see spikes near the discontinuity.
Upvotes: 2