clear all
close all
syms n aSym(n) bSym(n) x(t) t A(t)
To=4;
f=1/To;
wo=2*pi*f;
A(t)=1;
x(t)=A(t)*t*heaviside(t)-(t*heaviside(t-1));

a0=(int(x(t),0,1)/To)
aSym(n)=simplify(int(x(t)*cos(n.*wo.*t),t,0,1)/(To/2))
bSym(n)=simplify(int(x(t)*sin(n.*wo.*t),t,0,1)/(To/2))

nMax = 20;
n = 1:nMax;
a = eval(aSym(n));
b=  eval(bSym(n));

subplot(411)
stem(a0)
ylabel('a0')

subplot(412)
stem(a)
ylabel('an')

subplot(413)
stem(b)
ylabel('bn')

xApprox = a0;
for m = 1:nMax
    xApprox = xApprox + a(m)*cos(m*wo*t) + b(m)*sin(m*wo*t);
end
t = -20*pi:0.1:20*pi;

xCalc = eval(xApprox);
subplot(4,1,4);
plot(t,xCalc)
grid on
ylabel('x(t)')
