Matlab函数拟合相关(polyfit\lsqcurvefit\curving fitting tool(cftool))

1.polyfit函数(只限多项式拟合)

语法:

            a = polyfit ( x, y, n)

参数n为x的最高阶,返回值a是n阶函数的系数,a是一个长度为n+1的行向量。

2.lsqcurvefit函数

应用场合:

            可以自编公式进行拟合,比polyfit函数灵活,可以得出拟合优度。

语法:

           [s,resnorm]=lsqcurvefit(f,a,x,y)
     f:符号函数句柄。
     a:系数预估的值(预拟合的未知参数的估计值)。
     x:我们已经获知的x的值。
     y:我们已经获知的x对应的y的值。
     s:求解的系数a的值
     resnorm:拟合优度

举例

function[s,resnorm]=formular1(x,y)

a=[0 0 0 0]

f=@(a,x)a(1)+a(2)*(x).^0.5+a(3)*x+a(4)*(x).^2

[s,resnorm]=lsqcurvefit(f,a,x,y)

3.curving fitting tool(cftool)

通过下拉菜单“Type of fit”选择拟合曲线的类型,工具箱提供的拟合类型有:
Custom Equations:用户自定义的函数类型
Exponential:指数逼近,有 2 种类型, aexp(bx) 、 aexp(bx) + cexp(dx)
Fourier:傅立叶逼近,有 7 种类型,基础型是 a0 + a1cos(xw) + b1sin(xw)
Gaussian:高斯逼近,有 8 种类型,基础型是 a1exp(-((x-b1)/c1)^2)
Interpolant:插值逼近,有 4 种类型,linear、nearest neighbor、cubic spline、shape-preserving
Polynomial:多项式i逼近,有 9 种类型,linear polynomial、quadratic polynomial、cubic polynomial 以及 4-9th degree polynomial
Power:幂逼近,有 2 种类型。
Rational:有理数逼近,分子(Numerator)、分母(Denominator)共有的类型是 linear rational、quadratic rational、cubic rational、4-5th degree rational;此外,分子(Numerator)还包括 constant 型
Smoothing Spline:平滑样条逼近
Sum of Sin Functions:正弦曲线逼近,有 8 种类型,基础型是a1sin(b1x + c1)

  • 注:center and scale--------将xdata原始数据进行中心化与比例化处理,即将x变量替换为(x-mean(x))/std(x),同样替换y变量。类似于线性插值技术。这是在计算任何多项式项之前完成的。这种变换在理论上不会改变拟合,但在有限精度的计算机上可以使结果更好。matlab在曲线拟合工具箱中加入这个选项是为了当xdata与ydata拟合不好时,选择这个选项有利于得到更好的拟合效果。

在这里插入图片描述

  • SSE(和方差、误差平方和):The sum of squares due to error
    接近0,表示与数据拟合的好,但是要小心过拟合
  • MSE(均方差、方差):Mean squared error
  • RMSE(均方根、标准差):Root mean squared error
  • R-square(确定系数):Coefficient of determination
    测数据与推理数据之间的相关系数平方值,趋近于1较好
  • Adjusted R-square:Degree-of-freedom adjusted coefficient of determination
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
polyfit函数MATLAB程序代码如下: ```matlab function [p, S, mu] = polyfit(x, y, n) %POLYFIT Fit polynomial to data. % POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) of % degree N that fits the data Y best in a least-squares sense. P(X) % is a row vector of length N+1 containing the polynomial coefficients % in descending powers. % % [P,S] = POLYFIT(X,Y,N) returns the polynomial coefficients P and a % structure S for use with POLYVAL to obtain error estimates for % predictions. S contains fields: % S.normr -- 2-norm of the residual: norm(Y - PVAL)./sqrt(length(Y)) % S.resid -- vector of residuals: Y - PVAL % S.df -- degrees of freedom % S.normr^2 -- S.normr squared % S.R -- the triangular factor from a QR decomposition of the % Vandermonde matrix of X % S.coeff -- vector of coefficients in a model that is orthogonal % to the constant term: X - repmat(mean(X),size(X,1),1) % = Q*R, Y - repmat(mean(Y),size(Y,1),1) = Q*[Rc;rest] % where Rc is a vector of regression coefficients for % the orthogonal polynomial terms, and rest is a vector % of coefficients for the null space of X - ones(n,1)*[1 % mean(X)]. % % [P,S,MU] = POLYFIT(X,Y,N) finds the coefficients of a polynomial in % XHAT = (X-MU(1))/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). % This centering and scaling transformation improves the numerical % properties of both the polynomial and the fitting algorithm. % % Class support for inputs X,Y: % float: double, single % % See also POLY, POLYVAL, ROOTS, LSCOV, VANDER. % Copyright 1984-2004 The MathWorks, Inc. % $Revision: 5.26.4.8 $ $Date: 2004/12/06 16:38:03 $ if ~isequal(size(x),size(y)) error('X and Y vectors must be the same size.') end x = x(:); y = y(:); if nargin < 3, n = 1; end if n > length(x)-1 error('N must be less than the length of X.') end if length(x) ~= length(y) error('X and Y vectors must be the same length.') end % Construct Vandermonde matrix. V(:,n+1) = ones(length(x),1,class(x)); for j = n:-1:1 V(:,j) = x.*V(:,j+1); end % Compute coefficients. [Q,R] = qr(V,0); ws = warning('off','MATLAB:rankDeficientMatrix'); p = R\(Q'*y); % Same as p = V\y; warning(ws); % Remove trailing zeros. p = p.'; % Polynomial coefficients are row vectors by convention. if length(p) > n p = p(1:n+1); end % Compute error structure vector. if nargout > 1 r = y - V*p.'; normr = norm(r); df = length(y) - (n+1); S.normr = normr; S.resid = r; S.df = df; S.normr2 = normr^2; S.R = R; S.coeff = p; end % Undo scaling and centering. if nargout > 2 mu = [mean(x); std(x)]; else mu = []; end if length(p) > 1 p = polyfix(p,mu); end % ------------------------------------------------------------------ function c = polyfix(c,mu) %POLYFIX Fix polynomial coefficients for new data. % C = POLYFIX(C,MU) fixes the polynomial coefficients C for a % change of variable XHAT = (X-MU(1))/MU(2). MU(1) is the mean of % the original data and MU(2) is the standard deviation. C is a % row vector of polynomial coefficients in descending powers. if isempty(c), return, end m = length(c) - 1; if m == 0, return, end c = c(:); % Scale coefficients. scal = cumprod([1 repmat(mu(2),1,m)]./[1:m+1]); c = c.*scal; % Shift coefficients. c(1:m) = c(1:m) - c(m+1:-1:2).'*mu(1)./mu(2).^(1:m); % Remove scale factors. c = c./mu(2).^max(0,(0:m)); c = c.'; % Polynomial coefficients are row vectors by convention.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值