I get the following error meesage using the polyfit function:
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as described in HELP POLYFIT.
Has anybody see that before and has an idea what I need to do? I tried it with the help function, but I didn't understand what excatly could be false
the code I am using if the following, in case it helps:
if length(dataT(:,1))==1 SlopeSkew(number)=0; elseif length(dataT(:,1))==2 SlopeSkew(number)=0; else
% x is the Strike x= dataT(:,2); %is the implied volatility y=dataT(:,10); p = polyfit(x,y,2); f = polyval(p,x); thanks!
a=p(3); b=p(2); c=p(1); SlopeSkew(number)=b+2*c.*x; Slope=SlopeSkew';
No products are associated with this question.
So what is the length of x and y, and do you have any repeated x values?
There was a time when this function issued an error asking you not to have repeated X values. But the new error message is more accurate. You don't need unique X values. It's just that repeated X values won't allow you to estimate higher-order polynomials. So for instance:
x = [1;2;3;3;4]; y = (1:5)'; polyfit(x,y,2) polyfit(x,y,4)
The first call to polyfit works. The second would work if we had 5 points with distinct X values, but it doesn't work here because the 4 distinct X values allow polynomials only up to an exponent of 3.
In your example of fitting up to power 2, it seems like you either don't have 3 distinct points, or you have very ill-conditioned data.
Instead of repeated values, did you test the condition of the problem already? The docs suggest to use
[p, S, mu] = polyfit(x,y,n)
for a proper scaling. The matrix for the least-squares fit is ill-conditioned, when the values of x have a wide range and are far away from zero. Therefore the scaling does:
xx = (x - mean(x)) / std(x)
to get all data near to zero. The conversion back to the original values in POLIVAL is trivial.