matlab polyfit源码,MATLAB的拟合函数polyfit 的程序代码是什么啊
时间: 2023-10-12 20:05:10 浏览: 126
matlab 函数逼近与拟合源程序代码.zip_lsqlin_nlinfit函数_傅里叶拟合_多项式拟合_有理多项式
5星 · 资源好评率100%
polyfit函数的MATLAB程序代码如下:
```matlab
function [p, S, mu] = polyfit(x, y, n)
%POLYFIT Fit polynomial to data.
% POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
% degree N that fits the data Y best in a least-squares sense. P(X)
% is a row vector of length N+1 containing the polynomial coefficients
% in descending powers.
%
% [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
% structure S for use with POLYVAL to obtain error estimates for
% predictions. S contains fields:
% S.normr -- 2-norm of the residual: norm(Y - PVAL)./sqrt(length(Y))
% S.resid -- vector of residuals: Y - PVAL
% S.df -- degrees of freedom
% S.normr^2 -- S.normr squared
% S.R -- the triangular factor from a QR decomposition of the
% Vandermonde matrix of X
% S.coeff -- vector of coefficients in a model that is orthogonal
% to the constant term: X - repmat(mean(X),size(X,1),1)
% = Q*R, Y - repmat(mean(Y),size(Y,1),1) = Q*[Rc;rest]
% where Rc is a vector of regression coefficients for
% the orthogonal polynomial terms, and rest is a vector
% of coefficients for the null space of X - ones(n,1)*[1
% mean(X)].
%
% [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in
% XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X).
% This centering and scaling transformation improves the numerical
% properties of both the polynomial and the fitting algorithm.
%
% Class support for inputs X,Y:
% float: double, single
%
% See also POLY, POLYVAL, ROOTS, LSCOV, VANDER.
% Copyright 1984-2004 The MathWorks, Inc.
% $Revision: 5.26.4.8 $ $Date: 2004/12/06 16:38:03 $
if ~isequal(size(x),size(y))
error('X and Y vectors must be the same size.')
end
x = x(:);
y = y(:);
if nargin < 3, n = 1; end
if n > length(x)-1
error('N must be less than the length of X.')
end
if length(x) ~= length(y)
error('X and Y vectors must be the same length.')
end
% Construct Vandermonde matrix.
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end
% Compute coefficients.
[Q,R] = qr(V,0);
ws = warning('off','MATLAB:rankDeficientMatrix');
p = R\(Q'*y); % Same as p = V\y;
warning(ws);
% Remove trailing zeros.
p = p.'; % Polynomial coefficients are row vectors by convention.
if length(p) > n
p = p(1:n+1);
end
% Compute error structure vector.
if nargout > 1
r = y - V*p.';
normr = norm(r);
df = length(y) - (n+1);
S.normr = normr;
S.resid = r;
S.df = df;
S.normr2 = normr^2;
S.R = R;
S.coeff = p;
end
% Undo scaling and centering.
if nargout > 2
mu = [mean(x); std(x)];
else
mu = [];
end
if length(p) > 1
p = polyfix(p,mu);
end
% ------------------------------------------------------------------
function c = polyfix(c,mu)
%POLYFIX Fix polynomial coefficients for new data.
% C = POLYFIX(C,MU) fixes the polynomial coefficients C for a
% change of variable XHAT = (X-MU(1))/MU(2). MU(1) is the mean of
% the original data and MU(2) is the standard deviation. C is a
% row vector of polynomial coefficients in descending powers.
if isempty(c), return, end
m = length(c) - 1;
if m == 0, return, end
c = c(:);
% Scale coefficients.
scal = cumprod([1 repmat(mu(2),1,m)]./[1:m+1]);
c = c.*scal;
% Shift coefficients.
c(1:m) = c(1:m) - c(m+1:-1:2).'*mu(1)./mu(2).^(1:m);
% Remove scale factors.
c = c./mu(2).^max(0,(0:m));
c = c.'; % Polynomial coefficients are row vectors by convention.
阅读全文