function p = pfit( x, y, poly_order ) % PFIT Polynomial fitter % Generates coefficients for polynomial fitting of a dataset, % using a polynomial of order poly_order. % J. Watlington, 11/18/95 [m,num_samples] = size( x ); poly_order = poly_order + 1; % num terms is poly_order + 1 % Define the vandermonde matrix for poly_order terms V = ones( [ num_samples, poly_order ] ); % preallocate for loc = 1:num_samples prod = 1; % first term is already set to one for power = 2:poly_order prod = prod * x( loc ); V( loc, power ) = prod; end end % Solve for the pseudo-inverse, and multiply it times the input % to obtain the optimal solution. p = (pinv( V ) * y')';