MATLAB曲线拟合-最小二乘法-错误的“拟合"用度高 [英] MATLAB curve fitting - least squares method - wrong "fit" using high degrees

查看:237
本文介绍了MATLAB曲线拟合-最小二乘法-错误的“拟合"用度高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有人可以帮助我解决以下问题吗? 以下代码计算与给定数据集的最佳多项式拟合,即:指定次数的多项式.

Anyone here that could help me with the following problem? The following code calculates the best polynomial fit to a given data-set, that is; a polynomial of a specified degree.

不幸的是,无论数据集可能是多少(通常在6级或更高),MATLAB的拟合度都是完全错误的.通常,拟合曲线以某种指数式的方式向下完全偏离数据. (请参见示例:度= 8).

Unfortunately, whatever the data-set may be, usually at degree 6 or higher, MATLAB gets a totally wrong fit. Usually the fit curves totally away from the data in a sort of exponantial-looking-manner downwards. (see the example: degree = 8).

x=[1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5] % experimental x-values
y=[4.3 6.2 10.1 13.5 19.8 22.6 24.7 29.2] % experimental y-values

degree=8; % specify the degree
A = zeros(length(x),degree);
for exponent=0:degree;
for data=1:length(x);
   A(data,exponent+1)=x(data).^exponent; % create matrix A
end;
end;

a=inv((transpose(A)*A))*transpose(A)*y'; % a are the coëfficients of the polynom
a=flipud(a);
fitpolynoom=polyval(a,x);
error=sum((y-fitpolynoom).^2); % calculates the fit-error using the least-squares method
fitpolynoom=polyval(a,x);

figure;
plot(x,y,'black*',x,fitpolynoom,'g-');

error % displays the value of the fit-error in the Matlab command window

谢谢.

推荐答案

首先,请注意以下几点:对于Matlab中的最小二乘拟合多项式,可以改用现有的polyfit函数.此外(可能取决于您的应用程序),您可能不应该拟合$ 8 $次多项式,尤其是当您拥有$ 8 $数据点时.在这个答案中,我假设您有充分的理由将多项式拟合到您的数据中(例如,仅出于自学目的).

First, some remarks: for least-squares fitting polynomials in Matlab, you could use the existingpolyfit function instead. Furthermore (this may depend on your application) you probably should not be fitting $8$th degree polynomials, especially when you have $8$ data points. In this answer, I assume you have good reasons to fit polynomials to your data (e.g., just for self-study purposes).

问题是由矩阵求逆引起的数字问题.对于求解类型为$ Ax = b $的方程式,其中$ A $是平方矩阵,实际上不建议对$ A $求逆(请参见).在最小二乘情况下, \ begin {equation} a =(A ^ \ mathrm {T} A)^ {-1} A ^ \ mathrm {T} y ^ \ mathrm {T} \ end {equation} 最好解决 \ begin {equation} (A ^ \ mathrm {T} A)a = A ^ \ mathrm {T} y ^ \ mathrm {T} \ end {equation} 通过其他方式.在您的MATLAB代码中,您可以替换

The issue is a numeric problem arising from matrix inversion. For solving equations of type $Ax=b$ where $A$ is a square matrix, actually inverting $A$ is not recommended (See Blogpost 'Don't invert that matrix' by John D. Cook). In the least-squares case, instead of \begin{equation} a = (A^\mathrm{T} A)^{-1} A^\mathrm{T} y^\mathrm{T} \end{equation} it is better to solve \begin{equation} (A^\mathrm{T} A)a = A^\mathrm{T} y^\mathrm{T} \end{equation} by other means. In your MATLAB code, you may replace

a=inv((transpose(A)*A))*transpose(A)*y';

作者

a = (transpose(A) * A) \ (transpose(A) * y');

通过对您的代码进行此修改,我得到了适合数据点的信息.

By this modification to your code, I obtained a fit going through the data points.

这篇关于MATLAB曲线拟合-最小二乘法-错误的“拟合"用度高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆