%% MATLAB example for regression via least-squares %% Suppose given a 'complicated' function clear, clf f = @(x) 1+sin(10*x).*exp(x); fplot(f,[-1,1]),grid on, shg %% % We'd like to approximate with a polynomial, say degree 10. Let's try do % this via regression. We can do via interpolation: n = 20+1; % degree+1 Z = linspace(-1,1,n).'; % 11 sample points hold on plot(Z,f(Z),'k.','markersize',15),shg %% V = fliplr(vander(Z)); % The matrix of Z^i c = V\f(Z); % solve linear system to get polynomial p = @(x)c(1); for ii = 1:length(c)-1 p = @(x)p(x)+c(ii+1)*x.^ii; end fplot(p,[-1,1],'color','k'),grid on, shg %% % We see that the approximant (red) is looking mostly good, but not near % the endpoint $x=1$. We can improve the situation by sampling at more % points, then solving a least-squares problem. Least-squares makes the % situation much more robust! Z = linspace(-1,1,100).'; % 100 sample points hold on plot(Z,f(Z),'bo','markersize',5),shg V = fliplr(vander(Z)); % The matrix of Z^i % c = V(:,1:n)\f(Z); % solve linear system to get polynomial [Q,R] = qr(V(:,1:n),0); c = R\(Q'*f(Z)); p = @(x)c(1); for ii = 1:length(c)-1 p = @(x)p(x)+c(ii+1)*x.^ii; end fplot(p,[-1,1],'color','r'),grid on, shg