0
votes

I have some data let's say the following vector:

[1.2 2.13 3.45 4.59 4.79]

And I want to get a polynomial function, say f to fit this data. Thus, I want to go with something like polyfit. However, what polyfit does is minimizing the sum of least square errors. 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

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?

4

4 Answers

1
votes

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.

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

0
votes

Polyfit does what you want. 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).

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.

0
votes

What you ask is known as Lagrange Interpolation . There is a MATLAB file exchange available. 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.

0
votes

You have options.

  1. Use polyfit, just give it enough leeway to perform an exact fit. 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');