简体   繁体   English

在MATLAB中以特殊方式拟合多项式数据

[英]Polynomial data fitting in a special way in MATLAB

I have some data let's say the following vector: 我有一些数据,可以说以下向量:

[1.2 2.13 3.45 4.59 4.79] [1.2 2.13 3.45 4.59 4.79]

And I want to get a polynomial function, say f to fit this data. 我想得到一个多项式函数,用f表示该数据。 Thus, I want to go with something like polyfit . 因此,我想使用诸如polyfit东西。 However, what polyfit does is minimizing the sum of least square errors. 但是, polyfit作用是最小化最小平方误差之和。 But, what I want is to have 但是,我想要的是

f(1)=1.2 f(2)=2.13 f(3)=3.45 f(4)=4.59 f(5)=4.79 f(1)= 1.2 f(2)= 2.13 f(3)= 3.45 f(4)= 4.59 f(5)= 4.79

That is to say, I want to manipulate the fitting algorithm so that it will give me the exact points that I already gave as well as some fitted values where exact values are not given. 就是说,我要操纵拟合算法,以便它将给我我已经给出的精确点以及一些未给出确切值的拟合值。 How can I do that? 我怎样才能做到这一点?

I think everyone is missing the point. 我认为每个人都没有抓住重点。 You said that "That is to say, I want to manipulate the fitting algorithm so that I will give me the exact points as well as some fitted values where exact fits are not present. How can I do that?" 您说:“就是说,我想操纵拟合算法,以便为我提供确切的点以及一些不存在确切拟合的拟合值。我该怎么做?”

To me, this means you wish an exact (interpolatory) fit for a listed set, and for some other points, you want to do a least squares fit. 对我来说,这意味着您希望为列出的集合精确(插值)拟合,而对于其他一些点,则希望进行最小二乘拟合。

You COULD do that using LSQLIN, by setting a set of equality constraints on the points to be fit exactly, and then allowing the rest of the points to be fit in a least squares sense. 您可以使用LSQLIN做到这一点,方法是在要精确拟合的点上设置一组相等约束,然后以最小二乘的方式允许其余点拟合。

The problem is, this will require a high order polynomial. 问题是,这将需要一个高阶多项式。 To be able to fit 5 points exactly, plus some others, the order of the polynomial will be quite a bit higher. 为了能够精确拟合5个点以及其他一些点,多项式的阶数会高很多。 And high order polynomials, especially those with constrained points, will do nasty things. 而且高阶多项式,尤其是那些具有约束点的多项式,会做讨厌的事情。 But feel free to do what you will, just as long as you also expect a poor result. 但是,只要您还期望获得不错的结果,就可以随心所欲地做。

Edit: I should add that a better choice is to use a least squares spline, which is something you CAN constrain to pass through a given set of points, while fitting other points in a least squares sense, and still not do something wild and crazy as a result. 编辑:我应该补充一点,更好的选择是使用最小二乘样条线,这是您可以约束的点,以通过给定的点集,同时以最小二乘方的意义拟合其他点,并且仍然不做任何疯狂的事情结果是。

Polyfit does what you want. Polyfit可满足您的需求。 An N-1 degree polynomial can fit N points exactly, thus, when it minimizes the sum of squared error, it gets 0 (which is what you want). 一个N-1度多项式可以精确地拟合N个点,因此,当它使平方误差的总和最小时,它将变为0(这是您想要的)。

y=[1.2 2.13 3.45 4.59 4.79];
x=[1:5];
coeffs = polyfit(x,y,4);

Will get you a polynomial that goes through all of your points. 将为您提供遍历您所有观点的多项式。

What you ask is known as Lagrange Interpolation . 您所要求的被称为拉格朗日插值 There is a MATLAB file exchange available. 有可用的MATLAB文件交换。 http://www.mathworks.com/matlabcentral/fileexchange/899-lagrange-polynomial-interpolation http://www.mathworks.com/matlabcentral/fileexchange/899-lagrange-polynomial-interpolation
However, you should note that least squares polynomial fitting is generally preferred to Lagrange Interpolation since the data you have in principle will have noise in it and Lagrange Interpolation will fit the noise as well as the data you have. 但是,您应注意,通常,最小二乘多项式拟合通常比拉格朗日插值法更可取,因为原则上您拥有的数据中会包含噪声,而拉格朗日插值法将与您所拥有的数据一样拟合噪声。 So if you know that your data actually represents M dimensional polynomial and have N data, where N>>M, then you will have a order N polynomial with Lagrange. 因此,如果您知道您的数据实际上代表M维多项式并且具有N个数据,其中N >> M,那么您将拥有一个带Lagrange的N阶多项式。

You have options. 您有选择。

  1. Use polyfit, just give it enough leeway to perform an exact fit. 使用polyfit,只要给它足够的空间来进行精确拟合即可。 That is: 那是:

     values = [1.2 2.13 3.45 4.59 4.79]; p = polyfit(1:length(values), values, length(values)-1); 

    Now 现在

     polyval(p,2) %returns 2.13 
  2. Use interpolation / extrapolation 使用插值/外推

     values = [1.2 2.13 3.45 4.59 4.79]; xInterp = 0:0.1:6; valueInterp = interp1(1:length(values), values, xInterp ,'linear','extrap'); 

    Interpolation provides a lot of options for smoothing, extrapolation etc. For example, try: 插值提供了许多平滑,外推等选项。例如,尝试:

     valueInterp = interp1(1:length(values), values, xInterp ,'spline','extrap'); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM