设为首页收藏本站

EPS数据狗论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1332|回复: 0

多项式插值或拟合

[复制链接]

8

主题

80

金钱

115

积分

入门用户

发表于 2018-9-13 15:30:20 | 显示全部楼层 |阅读模式
  1. function [p,S] = polyfit(x,y,n)
  2. %p=polyfit(x,y,k)用k次多项式拟合向量数据(x,y)
  3. %p返回多项式的降幂系数.当k>=n-1时,polyfit实现多项式插值.  
  4. %例如 用二次多项式拟合数据
  5. %   x | 0.1  0.2  0.15 0.0  -0.2 0.3
  6. %   --|------------------------------
  7. %   y | 0.95 0.84 0.86 1.06 1.50 0.72
  8. % 求解
  9. %    x=[0.1,0.2,0.15,0,-0.2,0.3];
  10. %    y=[0.95,0.84,0.86,1.06,1.50,0.72];
  11. %    p=polyfit(x,y,2)
  12. %    xi=-0.2:0.01:0.3;
  13. %    yi=polyval(p,xi);
  14. %    plot(x,y,'o',xi,yi,'k');
  15. %    title('polyfit');

  16. %POLYFIT Fit polynomial to data.
  17. %   POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of
  18. %   degree N that fits the data, P(X(I))~=Y(I), in a least-squares sense.
  19. %
  20. %   [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a
  21. %   structure S for use with POLYVAL to obtain error estimates on
  22. %   predictions.  If the errors in the data, Y, are independent normal
  23. %   with constant variance, POLYVAL will produce error bounds which
  24. %   contain at least 50% of the predictions.
  25. %
  26. %   The structure S contains the Cholesky factor of the Vandermonde
  27. %   matrix (R), the degrees of freedom (df), and the norm of the
  28. %   residuals (normr) as fields.   
  29. %
  30. %   See also POLY, POLYVAL, ROOTS.

  31. %   J.N. Little 4-21-85, 8-23-86; CBM, 12-27-91 BAJ, 5-7-93.
  32. %   Copyright (c) 1984-98 by The MathWorks, Inc.
  33. %   $Revision: 5.9 $  $Date: 1997/11/21 23:40:57 $

  34. % The regression problem is formulated in matrix format as:
  35. %
  36. %    y = V*p    or
  37. %
  38. %          3  2
  39. %    y = [x  x  x  1] [p3
  40. %                      p2
  41. %                      p1
  42. %                      p0]
  43. %
  44. % where the vector p contains the coefficients to be found.  For a
  45. % 7th order polynomial, matrix V would be:
  46. %
  47. % V = [x.^7 x.^6 x.^5 x.^4 x.^3 x.^2 x ones(size(x))];

  48. if ~isequal(size(x),size(y))
  49.     error('X and Y vectors must be the same size.')
  50. end

  51. x = x(:);
  52. y = y(:);

  53. % Construct Vandermonde matrix.
  54. V(:,n+1) = ones(length(x),1);
  55. for j = n:-1:1
  56.     V(:,j) = x.*V(:,j+1);
  57. end

  58. % Solve least squares problem, and save the Cholesky factor.
  59. [Q,R] = qr(V,0);
  60. p = R\(Q'*y);    % Same as p = V\y;
  61. r = y - V*p;
  62. p = p.';          % Polynomial coefficients are row vectors by convention.

  63. % S is a structure containing three elements: the Cholesky factor of the
  64. % Vandermonde matrix, the degrees of freedom and the norm of the residuals.

  65. S.R = R;
  66. S.df = length(y) - (n+1);
  67. S.normr = norm(r);
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

客服中心
关闭
在线时间:
周一~周五
8:30-17:30
QQ群:
653541906
联系电话:
010-85786021-8017
在线咨询
客服中心

意见反馈|网站地图|手机版|小黑屋|EPS数据狗论坛 ( 京ICP备09019565号-3 )   

Powered by BFIT! X3.4

© 2008-2028 BFIT Inc.

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