%% clear format compact seed = 10; rng(seed); %% n = 10; sc = 0.1; t = linspace(0, 3, n)'; y = sin(t) + sc*randn(n,1); clf plot(t, y, 'o') hold on fplot(@sin, [0 3]) xlabel('t') ylabel('y') legend('noisy data', 'noiseless data', 'location', 'best') grid on hold off %% % A polynomial interpolant can be used to fit the data. Here we build one using % a Vandermonde matrix. V = t.^0; % vector of ones k = n - 1; %k = 3; for j = 1:k V(:,j+1) = t.*V(:,j); end c = V\y; p = @(x) polyval(flipud(c), x); plot(t, y, 'o') hold on fplot(p, [0 3]) fplot(@sin, [0 3]) grid on legend('noisy data', 'interpolation', 'noiseless data', 'location', 'best') hold off