%% % We measure the convergence rate for piecewise linear interpolation of % exp(sin(7*x)). f = @(x) exp(sin(7*x)); x = linspace(0, 1, 10001)'; % sample the difference at many points n = 2.^(3:10)'; err = 0*n; for i = 1:length(n) nn = n(i); t = linspace(0, 1, nn+1)'; % interpolation nodes p = plinterp(t, f(t)); err(i) = max(abs(f(x) - p(x))); end %% % Since we expect convergence that is O(h^2)=O(1/n^2), we use a log-log % graph of error and expect a straight line of slope $-2$. loglog(n, err, '.-') hold on loglog(n, 0.1*(n/n(1)).^(-2), '--' ) xlabel('n') ylabel('||f-p||_\infty') title('Convergence of PL interpolation') legend('error', '2nd order') axis tight grid on