线性最小二乘法拟合.doc
《线性最小二乘法拟合.doc》由会员分享,可在线阅读,更多相关《线性最小二乘法拟合.doc(19页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、MATLAB程序设计实践课程考核一 实现以下科学计算算法,并举一例应用之。(参考书籍精通MATLAB科学计算,王正林等著,电子工业出版社,2009年)“线性最小二乘法拟合”解答:一, 最小二乘法拟合;方法介绍在科学实验的统计方法研究中,往往要从一组实验数据(xi,yi)中寻找出自变量x和因变量y之间的关系yf(x)。由于观测数据往往不准确,因此并不要求yf(x)经过所有的点(xi,yi),而只要求在给定的点xi上误差f(xi)yi按照某种标准达到最小,通常采用欧氏范数2作为误差量度的标准。这就是所谓的最小二乘法。MATLAB实现在MATLAB中实现最小二乘法拟合通常可以采用如下途径:利用pol
2、yfit函数进行多项式拟合。polyfit函数源程序:function p,S,mu = polyfit(x,y,n)%POLYFIT Fit polynomial to data.% P = 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 is a% row vector of length N+1 containing the polynomial coefficients in% des
3、cending powers, P(1)*XN + P(2)*X(N-1) +.+ P(N)*X + P(N+1).% 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 for the triangular factor (R) from a QR% decomposition of the Vandermonde matri
4、x of X, the degrees of freedom% (df), and the norm of the residuals (normr). If the data Y are random,% an estimate of the covariance matrix of P is (Rinv*Rinv)*normr2/df,% where Rinv is the inverse of R.% P,S,MU = POLYFIT(X,Y,N) finds the coefficients of a polynomial in% XHAT = (X-MU(1)/MU(2) where
5、 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.% Warning messages result if N is = length(X), if X has repeated, or% nearly repeated, points, or if X might need centering and scaling.
6、% Class support for inputs X,Y:% float: double, single% See also POLY, POLYVAL, ROOTS.% Copyright 1984-2004 The MathWorks, Inc.% $Revision: 5.17.4.5 $ $Date: 2004/07/05 17:01:37 $% The regression problem is formulated in matrix format as:% y = V*p or% 3 2% y = x x x 1 p3% p2% p1% p0% where the vecto
7、r p contains the coefficients to be found. For a% 7th order polynomial, matrix V would be:% V = x.7 x.6 x.5 x.4 x.3 x.2 x ones(size(x);if isequal(size(x),size(y) error(MATLAB:polyfit:XYSizeMismatch,. X and Y vectors must be the same size.)endx = x(:);y = y(:);if nargout 2 mu = mean(x); std(x); x = (
8、x - mu(1)/mu(2);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% Solve least squares problem.Q,R = qr(V,0);ws = warning(off,all); p = R(Q*y); % Same as p = Vy;warning(ws);if size(R,2) size(R,1) warning(MATLAB:polyfit:PolyNotUnique, . Pol
9、ynomial is not unique; degree = number of data points.)elseif condest(R) 1.0e10 if nargout 2 warning(MATLAB:polyfit:RepeatedPoints, . Polynomial is badly conditioned. Remove repeated data points.) else warning(MATLAB:polyfit:RepeatedPointsOrRescale, . Polynomial is badly conditioned. Remove repeated
10、 data pointsn . or try centering and scaling as described in HELP POLYFIT.) endendr = y - V*p;p = p.; % Polynomial coefficients are row vectors by convention.% S is a structure containing three elements: the triangular factor from a% QR decomposition of the Vandermonde matrix, the degrees of freedom
11、 and% the norm of the residuals.S.R = R;S.df = length(y) - (n+1);S.normr = norm(r);流程图:例题设yspan1,x,x2,用最小二乘法拟合如表所示数据。X0.51.01.52.02.53.0y1.752.453.814.808.008.60开始用polyfit功能函数进行拟合。流程图输入数据调用polyfit函数拟合终止建立m文件源代码:function task1()x=0.5:0.5:3.0;y=1.75 2.45 3.81 4.80 8.00 8.60;a=polyfit(x,y,2)x1=0.5:0.05
12、:3.0y1=a(3)+a(2)*x1+a(1)*x1.2plot(x,y,*)hold onplot(x1,y1,-r)legend(实验值,拟合值)end在matlab命令窗口中输入:task1()运行结果:task1()a = 0.4900 1.2501 0.8560x1 = Columns 1 through 9 0.5000 0.5500 0.6000 0.6500 0.7000 0.7500 0.8000 0.8500 0.9000 Columns 10 through 18 0.9500 1.0000 1.0500 1.1000 1.1500 1.2000 1.2500 1.30
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 线性 最小二乘法 拟合
限制150内