
%clear all;
format long;

f=inline('sin(3*x)');
n=5;

number_test_pts=500;
method_parm=1;

h=(2*pi)/(2*n+1);
t=(0:2*n)'*h;
ft=feval(f,t);

l=-n:n;

lunghezza_l=2*n+1;

c=[];

if method_parm == 0
    
    for index=1:lunghezza_l
        l_loc=l(index);
        exp_loc=exp(i*l_loc*t);
        c_loc=ft'*exp_loc/lunghezza_l;
        c=[c; c_loc];
    end
    
else
    
    c_fft=fft(ft,length(ft))*h/(2*pi);
    c=[c_fft(n+2:2*n+1,:); c_fft(1:n+1,:)];
    
end


% TESTING INTERPOLATION.

pts_intp=t;
f_pts_intp=ft;

%\sum_(k=-n)^n c_k exp(ikt_j)=f(t_j), j=0:2n

k=(-n:n)';
for j=1:length(pts_intp)
    t_loc=t(j);
    exp_loc=exp(i*k*t_loc);
    f_approx_pts_intp(j,1)=c'*exp_loc;
end

abserr_intp_nodes=norm(f_approx_pts_intp-f_pts_intp,inf);
fprintf('\n \t [MAX ABS.ERROR AT INTP. NODES]: %2.2e',abserr_intp_nodes);

h_test=2*pi/number_test_pts;
pts_test=(0:h_test:2*pi)';
f_pts_test=feval(f,pts_test);

k=(-n:n)';
for j=1:length(pts_test)
    t_loc=pts_test(j);
    exp_loc=exp(i*k*t_loc);
    f_approx_pts_test(j,1)=c'*exp_loc;
end

abserr_intp_test_pts=norm(f_approx_pts_test-f_pts_test,inf);
fprintf('\n \t [MAX ABS.ERROR AT TEST PTS.]: %2.2e',abserr_intp_test_pts);
% [f_pts_test f_approx_pts_test]