x0为初始解向量;xdata,ydata为满足关系ydata=F(x, xdata)的数据; lb、ub为解向量的下界和上界 ,若没有指定界,则lb=[ ],ub=[ ]; options为指定的优化参数;
fun为拟合函数,其定义方式为:x = lsqcurvefit(@myfun,x0,xdata,ydata), 其中myfun已定义为 function F = myfun(x,xdata)
F = … % 计算x处拟合函数值fun的用法与前面相同;
resnorm=sum ((fun(x,xdata)-ydata).^2),即在x处残差的平方和; residual=fun(x,xdata)-ydata,即在x处的残差; exitflag为终止迭代的条件; output为输出的优化信息;
lambda为解x处的Lagrange乘子;
jacobian为解x处拟合函数fun的jacobian矩阵。
例:lsqcurvefit()优化程序 Data = ...
[0.0000 5.8955 0.1000 3.5639 0.2000 2.5173 0.3000 1.9790 0.4000 1.8990 0.5000 1.3938 0.6000 1.1359 0.7000 1.0096 0.8000 1.0343 0.9000 0.8435 1.0000 0.6856 1.1000 0.6100 1.2000 0.5392 1.3000 0.3946 1.4000 0.3903 1.5000 0.5474 1.6000 0.3459 1.7000 0.1370 1.8000 0.2211 1.9000 0.1704 2.0000 0.2636]; t = Data(:,1); y = Data(:,2);
% axis([0 2 -0.5 6]) plot(t,y,'ro')
title('Data points')
%We would like to fit the function y = c(1)*exp(-lam(1)*t) + c(2)*exp(-lam(2)*t) to the data
%The lsqcurvefit function solves this type of problem easily.
%To begin, define the parameters in terms of one variable x: %x(1) = c(1) %x(2) = lam(1) %x(3) = c(2) %x(4) = lam(2)
%Then define the curve as a function of the parameters x and the data t:
F = @(x,xdata)x(1)*exp(-x(2)*xdata) + x(3)*exp(-x(4)*xdata); x0 = [1 1 1 0];
[x,resnorm,~,exitflag,output] = lsqcurvefit(F,x0,t,y)
hold on plot(t,F(x,t)) hold off
Fsumsquares = @(x)sum((F(x,t) - y).^2); opts = optimset('LargeScale','off');
[xunc,ressquared,eflag,outputu] = ...
fminunc(Fsumsquares,x0,opts)
fprintf(['There were %d iterations using fminunc,' ... ' and %d using lsqcurvefit.\\n'], ... outputu.iterations,output.iterations)
fprintf(['There were %d function evaluations using fminunc,' ... ' and %d using lsqcurvefit.'], ...
outputu.funcCount,output.funcCount) type fitvector x02 = [1 0];
F2 = @(x,t) fitvector(x,t,y);
[x2,resnorm2,~,exitflag2,output2] = lsqcurvefit(F2,x02,t,y) fprintf(['There were %d function evaluations using the 2-d ' ... 'formulation, and %d using the 4-d formulation.'], ... output2.funcCount,output.funcCount) x0bad = [5 1 1 0];
[xbad,resnormbad,~,exitflagbad,outputbad] = ... lsqcurvefit(F,x0bad,t,y)
hold on
plot(t,F(xbad,t),'g')
legend('Data','Global fit','Bad local fit','Location','NE') hold off
fprintf(['The residual norm at the good ending point is %f,' ... ' and the residual norm at the bad ending point is %f.'], ... resnorm,resnormbad)
displayEndOfDemoMessage(mfilename)
拟合效果如下:
Data points6DataGlobal fitBad local fit 543210 00.20.40.60.811.21.41.61.82
直线的最小二乘拟合:
y=a+bx
式中有两个待定参数,a代表截距,b代表斜率。对于等精度测量所得到的N组数据(xi,yi),i=1,2……,N,xi值被认为是准确的,所有的误差只联系着yi。下面利用最小二乘法把观测数据拟合为直线。
用最小二乘法估计参数时,要求观测值yi的偏差的加权平方和为最小。对于等精度观测值的直线拟合来说,可使下式的值最小:
上式分别对a、b求偏导得:
整理后得到方程组:
解上述方程组便可求得直线参数a和b的最佳估计值。
1、可看成是一阶多项式拟合,跟前面曲线拟合方法一样。 2、利用linefit()函数进行最小二乘的直线拟合 使用: clear
x=[0.5 1 1.5 2 2.5 3];
y=[ 1.75 2.45 3.81 4.8 8 8.6];
[k,b]=linefit(x,y) %得到斜率k和常数b y1=polyval([k,b],x); plot(x,y1,’k-’,x,y,’k*’)
MATLAB一元到多元线性回归方程的计算和检验_百度文库
http://wenku.http://35331.cn//link?url=_71b8kDsSUfnYSXWKsvL_X2DAWqojz3kRUW9UiX2LPEZYJM2790sHg2d52R4VYoywJWBi1S44Wy0-mMlymAKiLNQY6z2hpHCm-fgyv0PWwK
(研究生 数理统计)多元线性回归及显著性检验Matlab程序(完美版)_百度文库 http://wenku.http://35331.cn//view/fece30795acfa1c7aa00cc82.html?from=search