最小二乘法拟合多项式

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Curve Fitting with MATLAB

Curve fitting is a useful tool for representing a data set in a linear or quadratic fashion. MATLAB has two functions, polyfit and polyval, which can quickly and easily fit a polynimial to a set of data points. A first order polynomial is the linear equation that best fits the data. A polynomial can also be used to fit the data in a quadratic sense. As a reminder, the general formula for a polynomial is:

The degree of a polynomial is equal to the largest value of the exponents of the independent variable, x.

For example:

Polyfit and Polyval

"Polyfit" is a MATLAB function that computes a least squares polynomial for a given set of data. Polyfit actually generates the coefficients of the polynomial (which can be used to simulate a curve to fit the data) according to the degree specified.

"Polyval" evaluates a polynomial for a given set of x values. So, polyval actually generates a curve to fit the data based on the coefficients found using polyfit. MATLAB's explanation of polyfit and polyval are:

POLYFIT- Polynomial curve fitting

POLYFIT(x,y,n) finds the coefficients of a polynomial p(x) of degree n that fits

the data, p(x(i)) is approximately equal to y(i), in a least-squares sense. POLYVAL - Polynomial evaluation

If V is a vector whose elements are the coefficients of a polynomial, then

POLYVAL(V,s) is the value of the polynomial evaluated at s. If s is a matrix or

vector, the polynomial is evaluated at all points in s.

The following is an example MATLAB curve fit sequence using polyfit and polyval.

Here is the plot reflecting our first try at fitting the data with a first degree polynomial (or a straight line).

The coefficients given by MATLAB yield the following equation for the line:

Trying a higher polynomial:

Here is the plot of the curve generated from the 5th order polynomial. This curve fits the data perfectly.

The coefficients given by MATLAB for the curve yield the following equation:

相关文档
最新文档