Matlab Instruction:
p = polyfit(x,y,n) finds the coefficients
of a polynomial p(x) of degree n that fits the
data, p(x(i)) to y(i), in a least squares sense.
The result p is a row vector of length n+1
containing the polynomial coefficients in descending powers:
简而言之,polyfit使用最小二乘法原理,获得两组数据的曲线拟合.
polyval类似于polyfit,它用来得到已知的多项式函数的函数值. 当定义n =
1时,我们得到两组数据之间的线性回归模型.关于线性回归,值得提到的是matlab的R2013b中已经有regression和plotregression
function可以直接使用.
例如:
x = rand(20,1); y = rand(20,1) % define random datasets
p = polyfit(x,y,1); % linear regression model
f = polyval(p,x) % estimated polynomial output of x
figure('Color',[1 1 1]);
polyfit(x,y,'o',x,f,'-');
我们得到的离散点和拟合出的线性关系如下:
当然,关于这两个函数还有很多可以讨论的地方, 这里只列出最基本的应用.