声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 4453|回复: 4

[共享资源] 基于矩阵QR分解的最小二乘拟合MATLAB源程序

[复制链接]
发表于 2006-11-14 18:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
  1. function [c,R2,rout] = fitqr(x,y,basefun)
  2. % fitqr  Least-squares fit via solution of overdetermined system with QR
  3. %        Given ordered pairs of data, (x_i,y_i), i=1,...,m, fitqr
  4. %        returns the vector of coefficients, c_1,...,c_n, such that
  5. %            F(x) = c_1*f_1(x) + c_2*f_2(x) + ... + c_n*f_n(x)
  6. %        minimizes the L2 norm of y_i - F(x_i).
  7. %
  8. % Synopsis:  c       = fitqr(x,y,basefun)
  9. %           [c,R2]   = fitqr(x,y,basefun)
  10. %           [c,R2,r] = fitqr(x,y,basefun)
  11. %
  12. % Input:   x,y     = vectors of data to be fit
  13. %          basefun = (string) name of user-supplied m-file that computes
  14. %                    matrix A.  The columns of A are the values of the
  15. %                    basis functions evaluated at the x data points.
  16. %
  17. % Output:  c = vector of coefficients obtained from the fit
  18. %          R2 = (optional) adjusted coefficient of determination; 0 <= R2 <= 1
  19. %               R2 close to 1 indicates a strong relationship between y and x
  20. %          r  = (optional) residuals of the fit

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

  22. A = feval(basefun,x(:)); %  Coefficient matrix of overdetermined system
  23. c = A\y(:);              %  Solve overdetermined system with QR factorization
  24. if nargout>1
  25.   r = y - A*c;           %  Residuals at data points used to obtain the fit
  26.   [m,n] = size(A);
  27.   R2 = 1 - (m-1)/(m-n-1)*(norm(r)/norm(y-mean(y)))^2;
  28.   if nargout>2,  rout = r;  end
  29. end
复制代码

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2006-12-19 16:27 | 显示全部楼层
发表于 2007-1-18 17:26 | 显示全部楼层
不错的程序,不知楼主有没做过时间序列的非线性最小二乘拟合
发表于 2007-1-21 15:09 | 显示全部楼层
原帖由 zhaoym_0413 于 2007-1-18 17:26 发表
不错的程序,不知楼主有没做过时间序列的非线性最小二乘拟合


matlab中有现成的函数
发表于 2012-3-19 15:29 | 显示全部楼层
看得懂一点点
不知道楼主是否有LQG控制振动的程序啊
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-18 15:49 , Processed in 0.064628 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表