MATLAB Answers

Locks
0

error message using polyfit (nonlinear regression)

Asked by Locks
on 20 Apr 2013
Accepted Answer by bym

hi,

I get the following error meesage:

    Warning: Polynomial is not unique; degree >= number of data points. 
    > In polyfit at 71
      In deltaAdjustment at 27
      In run at 74 

The code I am using is the following:

for putCall =1:2
    dataPutCall = data16(data16(:,3)==putCall,:);
    dates=unique(dataPutCall(:,6));
    for i=1:length(dates)
        date=dates(i);
        dataDate = dataPutCall(dataPutCall(:,6) == date,:);
        Ts = unique(dataDate(:,5));
        for indexT = 1:length(Ts)
            T = Ts(indexT);
            dataT = dataDate(dataDate(:,5) == T,:);
                number=dataT(:,13);  
                x= dataT(:,2);
                y=dataT(:,10);
                p = polyfit(x,y,2);
                f = polyval(p,x);
                a=p(3);
                b=p(2);
                c=p(1);
                SlopeSkew(number)=b+2*c.*x;
                Slope=SlopeSkew';
            end
        end
    end

I have used this code for a smaller matrix and there is was worked perfectly but not it seems as something is not working correctly and I am unsure what excatly it is

  0 Comments

Products

No products are associated with this question.

1 Answer

Answer by bym
on 20 Apr 2013
 Accepted answer

I think it is obvious from the message; you don't have a enough data points to fit the polynomial uniquely. Like fitting a line through just one point, there are many solutions

x=[1,2];
y = [2,8];
polyfit(x,y,2)
Warning: Polynomial is not unique; degree >= number of data points. 
> In polyfit at 72

  3 Comments

Locks
on 21 Apr 2013

You're right, I didn't get it at first because it's working for a smaller dataset. I assume when using polyfit(x,y,2), there are at least three inputs needed to compute the curve up to power of 2, is that correct?

Yes, at least n+1 points for degree n.

Locks
on 21 Apr 2013

perfect, thanks


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!