How to avoid this Warning: Polynomial is badly conditioned?Need Help
8 views (last 30 days)
Show older comments
Hey all, when I used this code below:
for i = 1:height(stationList)
% Locate the pairs or stations in Cmo
rowIdx1 = strcmp(stationNames, stationList.station_name{i}); % use find(rowIdx1) to see row number
rowIdx2 = strcmp(stationNames, stationList.closest_station{i}); % use find(rowIdx2) to see row number
% If station could not be located, skip this iteration and throw warning.
if ~any(rowIdx1)
warning('%s could not be located.',stationList.station_name{i})
continue
end
if ~any(rowIdx2)
warning('%s could not be located.',stationList.closest_station{i})
continue
end
% Loop through the selected months
for j = 1:numel(months)
% Extract tables for given station and month
T1 = Cmo{rowIdx1, months(j)};
T2 = Cmo{rowIdx2, months(j)};
% Remove missing values and compute linear reg coeffs.
nanIdx = isnan(T1.rrr24) | isnan(T2.rrr24);
T1FillIdx = isnan(T1.rrr24) & ~isnan(T2.rrr24);
coefs = polyfit(T1.rrr24(~nanIdx), T2.rrr24(~nanIdx), 1);
stationList.(monthNames{j})(i,:) = coefs;
% This simple approach assumes the rows of T1 are from the
% same dates as the rows of T2. Here we check that assumption.
% If this assumption-check ever fails, a more rigorous approach
% will be needed that matches the rows by date.
assert(isequal(T1.date,T2.date),'Assumption violation: dates do not match between tables.')
% Compute missing values (if the values in T2 are also missing, it will return NaN.
T1.rrr24(T1FillIdx) = coefs(1) * T2.rrr24(T1FillIdx) + coefs(2);
% ^^^^^^^^^^ ^^^^^^^^^ % Update the original Cmo array of nx12 tables.
Cmo{rowIdx1, months(j)} = T1;
end
end
I got this error:
In polyfit (line 79)
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.
![Capture.JPG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/268884/Capture.jpeg)
Do you know what should I do?
Thank you
0 Comments
Accepted Answer
Walter Roberson
on 29 Jan 2020
Some of your rrr24 values are the same or very close to each other.
Using the centering and scaling mechanism might help, but I suspect that you will still have the problem. You should investigate why some of your rrr24 values are nearly the same.
3 Comments
Walter Roberson
on 29 Jan 2020
Remember that your polyfit is asserting that the amount of rain at T1 is "causing" a response of an amount of rain at T2. So if you had 1 mm of rain in one period at T1 and (say) 3 mm for the period at T2 then you would be asserting that 1 in any other period "causes" 3 in that other period because you are proposing that the relationship is fixed and linear.
I would suggest to you that this model is not what you want.
I would suggest discretizing the precipitation values and finding the mean response for the same bin, and then looking at the response curve.
For example by 0.5,
binnumber = floor(T1.rrr24 * 1/0.5) + 1;
meanresponse = accumarray( binnumber, T2.rrr24, [], @mean)
More Answers (0)
See Also
Categories
Find more on Historical Contests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!