clear all;
close all;

T = 0.0005; 
m = 20000*T;
n = 2*m;
t = linspace(0,T,n+1);
p = 2*(0.5*T - t)/T; 
p(1) = p(1)/2; p(n+1) = p(n+1)/2; 
% the corrections for p are due to the fact that p(t) is not periodic at the ends of the interval

% least square trigonometric approximation
for j = 0 : m
    a(j+1) = 2*p*cos(2*pi*j*t'/T)/n; 
    b(j+1) = 2*p*sin(2*pi*j*t'/T)/n;
end
nInt = 100;
tInt = linspace(0,T,nInt); 
tInt = tInt(1:nInt);
q = 0.5*a(1)*ones(1,nInt); 
for j = 1 : (m-1)
	q = q + a(1+j)*cos(2*pi*j*tInt/T) + b(1+j)*sin(2*pi*j*tInt/T);
end
q = q + a(m+1)*cos(2*pi*m*tInt/T);
pEx = 2*(0.5*T - tInt)/T;
plot(tInt,pEx,'r',tInt,q,'b');  

