N = 5;
n = 2*N;
h = pi/N;

x = h*[0:n-1];        % grid for interpolating
xx = h/20 * [0:20*n]; % grid for plotting

function y=f(x)
  y = x .* (x .- 2*pi) .* exp(-x);
  % y = sin(6.*x);
end

function y = tpolyval(c,x)
  y = zeros(1,length(x));
  N = length(c)/2;
  for j=[N+2:2*N]
    y = y .+ c(j).*exp(-(length(c)-j+1).*i.*x);
  end
  for j=[1:N]
    y = y .+ c(j).*exp((j-1).*i.*x);
  end
  y = y .+ (c(N+1)/2) .* (exp(N.*i.*x)+exp(-N.*i.*x));
  y = y./length(c);
end

yy = f(xx);

yk = fft(f(x));
yyi = tpolyval(yk,xx);

plot(xx,yy,xx,yyi)
 