% script: fouriertest.m
% description: perform some approximation experiments with 
% Fourier polynomials on the function myfcn defined externally.

% programmer: t. shores
% last rev.: 2/8/16

n = 2 % highest frequency in Fourier approximation
N = 4 % sampling number on [0,2*pi]
a = zeros(1,n+1); 
b = zeros(1,n);
% construct the coefficients for myfcn
xnodes = linspace(0,2*pi,N+1);
xnodes = xnodes(1:N); % trim off 2*pi
xnodes = xnodes - pi; % let's work on [-pi,pi]
f = myfcn(xnodes);
% dispose of 0th coefficients
a(n+1) = 2*sum(f)/N;
% now the rest
for j = 1:n
  a(j) = 2*f*cos(j*xnodes)'/N;
  b(j) = 2*f*sin(j*xnodes)'/N;
end
% display norm of node error
x = -pi:.01:pi;
disp('|| myfcn(x) - trigeval(a,b,x) ||:') ,disp(norm(myfcn(x) - trigeval(a,b,x),inf))
% now try plotting
clf
plot(x,myfcn(x),x,trigeval(a,b,x));
