suffer 发表于 2006-11-14 18:17

基于矩阵QR分解的最小二乘拟合MATLAB源程序

function = fitqr(x,y,basefun)
% fitqrLeast-squares fit via solution of overdetermined system with QR
%      Given ordered pairs of data, (x_i,y_i), i=1,...,m, fitqr
%      returns the vector of coefficients, c_1,...,c_n, such that
%            F(x) = c_1*f_1(x) + c_2*f_2(x) + ... + c_n*f_n(x)
%      minimizes the L2 norm of y_i - F(x_i).
%
% Synopsis:c       = fitqr(x,y,basefun)
%            = fitqr(x,y,basefun)
%          = fitqr(x,y,basefun)
%
% Input:   x,y   = vectors of data to be fit
%          basefun = (string) name of user-supplied m-file that computes
%                  matrix A.The columns of A are the values of the
%                  basis functions evaluated at the x data points.
%
% Output:c = vector of coefficients obtained from the fit
%          R2 = (optional) adjusted coefficient of determination; 0 <= R2 <= 1
%               R2 close to 1 indicates a strong relationship between y and x
%          r= (optional) residuals of the fit

if length(y)~= length(x);error('x and y are not compatible');end

A = feval(basefun,x(:)); %Coefficient matrix of overdetermined system
c = A\y(:);            %Solve overdetermined system with QR factorization
if nargout>1
r = y - A*c;         %Residuals at data points used to obtain the fit
= size(A);
R2 = 1 - (m-1)/(m-n-1)*(norm(r)/norm(y-mean(y)))^2;
if nargout>2,rout = r;end
end

xtw1128 发表于 2006-12-19 16:27

zhaoym_0413 发表于 2007-1-18 17:26

不错的程序,不知楼主有没做过时间序列的非线性最小二乘拟合

happy 发表于 2007-1-21 15:09

原帖由 zhaoym_0413 于 2007-1-18 17:26 发表
不错的程序,不知楼主有没做过时间序列的非线性最小二乘拟合

matlab中有现成的函数

lym 发表于 2012-3-19 15:29

看得懂一点点
不知道楼主是否有LQG控制振动的程序啊
页: [1]
查看完整版本: 基于矩阵QR分解的最小二乘拟合MATLAB源程序