MATLAB: Plotting Fourier Series Coefficients

fourierplot

Hi, I am trying to find Fourier coefficients of two signals: x(t) = u(t) – u(t-1) [0<t<2] and y(t) = u(t) – u(t-0.5) [0<t<1]. I have found the Fourier series coefficients through this code:
syms t k L n
evalin(symengine,'assume(k,Type::Integer)');
a = @(f,t,k,L) int(f*cos(k*pi*t/L)/L,t,-L,L);
b = @(f,t,k,L) int(f*sin(k*pi*t/L)/L,t,-L,L);
fs = @(f,t,n,L) a(f,t,0,L)/2 + ...
symsum(a(f,t,k,L)*cos(k*pi*t/L) + b(f,t,k,L)*sin(k*pi*t/L),k,1,n);
f = heaviside(t) - heaviside(t-1);
f1 = heaviside(t) - heaviside(t-0.5);
>> [B,how]=simple(b(f,t,k,1)); B
B =
(2*sin((pi*k)/2)^2)/(pi*k)
>> [B,how]=simple(b(f1,t,k,1)); B
B =
(2*sin((pi*k)/4)^2)/(pi*k)
However, I am unsure how to plot the magnitude line spectra of these two in the same figure from 0 to 40*pi rad/sec. I have tried ez plot but keep getting errors. I am new to this and not sure what to do. Thanks!

Best Answer

bfun = matlabFunction(B, k);
kspan = linspace(0,40,100);
plot(kspan, bfun(kspan));
Related Question