% Test cosine series
%
% Samuli Siltanen Nov 2014

% Choose level of trigonometric approximation
N = 20;

% Construct points for numerical integration
M = 1024;
xvec = ([0:(M-1)]/M)*2*pi;
Dx   = xvec(2)-xvec(1);

% Compute the cosine series coefficients
coefs_c = zeros(N+1,1);
for nnn = 0:N;
    coefs_c(nnn+1) = 1/(2*pi)*Dx*sum(MyFun1(xvec).*cos(nnn*xvec));
end

% Reconstruct the function from coefficients
Ny   = 1024;
y    = linspace(0,2*pi,Ny);
funN = zeros(size(y));
for nnn = 0:N;
    if nnn==0
        funN = funN + coefs_c(nnn+1)*cos(nnn*y);
    else
        funN = funN + 2*coefs_c(nnn+1)*cos(nnn*y);
    end
end

% Take a look at the result
figure(1)
clf
plot(y,MyFun1(y),'k')
hold on
plot(y,funN,'r')
axis([0 2*pi -1.2 1.2])