MATLAB curve fitting - least squares method - wrong “fit” using high degrees

喜你入骨 提交于 2019-12-01 10:27:38

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).

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';

by

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!