clear %% % We get a cardinal function if we use data that is one at a node and zero % at the others. N = 7; n = (N-1)/2; t = 2*(-n:n)'/N; y = zeros(N,1); y(n+1) = 1; plot(t, y, '.') hold on p = triginterp(t, y); figure(1) fplot(p, [-1 1]) xlabel('x') ylabel('p(x)') title('Trig cardinal function') grid on %% % Here is a 2-periodic function and one of its interpolants. figure(2) f = @(x) exp(sin(pi*x)-2*cos(pi*x)); fplot(f,[-1 1]) hold on y = f(t); plot(t, y, '.') fplot(triginterp(t,y),[-1 1]) xlabel('x') ylabel('p(x)') title('Trig interpolation') grid on %% % The convergence of the interpolant is exponential (spectral). We let $N$ % go needlessly large here in order to demonstrate that unlike polynomials, % trigonometric interpolation is stable on equally spaced nodes. N = (3:3:90)'; err = 0*N; x = linspace(-1,1,1601)'; % for measuring error for k = 1:length(N) n = (N(k)-1)/2; t = 2*(-n:n)'/N(k); p = triginterp(t,f(t)); err(k) = norm(f(x)-p(x),inf); end figure(3) semilogy(N, err, '.-') title('Error in trig interpolation') xlabel('N') ylabel('max error') grid on