g=@(x) 1.*(x>0)+(-1).*(x<0);
a_0=(1/pi)*quad(g,-pi,pi)
x = linspace(-pi,pi,200);
for k = 1:numel(x)
for n=1:10
a_n(n,k)=(1/pi)*(g(x(k))*cos(n*x(k)));
b_n(n,k)=(1/pi)*(g(x(k))*sin(n*x(k)));
end
A_n(k) = sum(a_n(:,k));
B_n(k) = sum(b_n(:,k));
f(k)=a_0+A_n(k).*cos(n.*x(k))+B_n(k).*sin(n.*x(k));
end
plot(x,f)