function [alpha,a,b] = fseries2(t,f)
% Fourier series for t sine and cosine terms of a 
% function f with domain [-pi,pi].

alpha=integral(f,-pi,pi)/pi;
k=1;
while k<t+1
a(k)=integral(@(x) cos(k.*x).*f(x),-pi,pi)/pi;
b(k)=integral(@(x) sin(k.*x).*f(x),-pi,pi)/pi;
k=k+1;
end


x=linspace(-pi,pi,1000);


k=1;
while k<length(x)+1
yf(k)=alpha/2;
j=1;
while j<length(a)+1
yf(k)=yf(k)+a(j)*cos(j*x(k))+b(j)*sin(j*x(k));
j=j+1;
end
k=k+1;
end

for s=1:length(x)
yd(s)=f(x(s));
end

w=1;
while w<length(a)
freq(w)=w/(2*pi);
w=w+1;
end
freq(w)=w/(2*pi);


figure(739)
stem(freq,a);
hold on
stem(0,alpha);
hold off
xlabel('frequency')
ylabel('cosine coefficients')
title('cosine coefficient versus frequency','FontSize',20,'FontWeight','bold')


figure(740)
stem(freq,b);
xlabel('frequency')
ylabel('sine coefficient')
title('sine coefficient versus frequency','FontSize',20,'FontWeight','bold')

figure(741)
stem(freq,sqrt(a.^2+b.^2));
xlabel('frequency')
ylabel('signal amplitude')
title('amplitude versus frequency','FontSize',20,'FontWeight','bold')


figure(t)
plot(x,yd,x,yf);



