资源描述:
《线性最小二乘法拟合》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、《MATLAB程序设计实践》课程考核一.实现以下科学计算算法,并举一例应用之。(参考书籍《精通MATLAB科学计算》,王正林等著,电子工业出版社,2009年)“线性最小二乘法拟合”解答:一,最小二乘法拟合;方法介绍在科学实验的统计方法研究中,往往要从一组实验数据(xi,yi)中寻找出自变量x和因变量y之间的关系y=f(x)。由于观测数据往往不准确,因此并不要求y=f(x)经过所有的点(xi,yi),而只要求在给定的点xi上误差ð=f(xi)-yi按照某种标准达到最小,通常采用欧氏范数ð^2作为误差量度的标准。这就是所谓的最小二乘法。MAT
2、LAB实现在MATLAB中实现最小二乘法拟合通常可以采用如下途径:利用polyfit函数进行多项式拟合。polyfit函数源程序:function[p,S,mu]=polyfit(x,y,n)%POLYFITFitpolynomialtodata.%P=POLYFIT(X,Y,N)findsthecoefficientsofapolynomialP(X)of%degreeNthatfitsthedataYbestinaleast-squaressense.Pisa%rowvectoroflengthN+1containingthepoly
3、nomialcoefficientsin%descendingpowers,P(1)*X^N+P(2)*X^(N-1)+...+P(N)*X+P(N+1).%%[P,S]=POLYFIT(X,Y,N)returnsthepolynomialcoefficientsPanda%structureSforusewithPOLYVALtoobtainerrorestimatesfor%predictions.Scontainsfieldsforthetriangularfactor(R)fromaQR%decompositionoftheVan
4、dermondematrixofX,thedegreesoffreedom%(df),andthenormoftheresiduals(normr).IfthedataYarerandom,%anestimateofthecovariancematrixofPis(Rinv*Rinv')*normr^2/df,%whereRinvistheinverseofR.%%[P,S,MU]=POLYFIT(X,Y,N)findsthecoefficientsofapolynomialin%XHAT=(X-MU(1))/MU(2)whereMU(1
5、)=MEAN(X)andMU(2)=STD(X).This%centeringandscalingtransformationimprovesthenumericalproperties%ofboththepolynomialandthefittingalgorithm.%%WarningmessagesresultifNis>=length(X),ifXhasrepeated,or%nearlyrepeated,points,orifXmightneedcenteringandscaling.%%Classsupportforinput
6、sX,Y:%float:double,single%%SeealsoPOLY,POLYVAL,ROOTS.%Copyright1984-2004TheMathWorks,Inc.%$Revision:5.17.4.5$$Date:2004/07/0517:01:37$%Theregressionproblemisformulatedinmatrixformatas:%%y=V*por%%32%y=[xxx1][p3%p2%p1%p0]%%wherethevectorpcontainsthecoefficientstobefound.For
7、a%7thorderpolynomial,matrixVwouldbe:%%V=[x.^7x.^6x.^5x.^4x.^3x.^2xones(size(x))];if~isequal(size(x),size(y))error('MATLAB:polyfit:XYSizeMismatch',...'XandYvectorsmustbethesamesize.')endx=x(:);y=y(:);ifnargout>2mu=[mean(x);std(x)];x=(x-mu(1))/mu(2);end%ConstructVandermonde
8、matrix.V(:,n+1)=ones(length(x),1,class(x));forj=n:-1:1V(:,j)=x.*V(:,j+1);end%Solveleastsquarespr