How to avoid this Warning: Polynomial is badly conditioned?Need Help

6 views (last 30 days)
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
Do you know what should I do?
Thank you

Accepted Answer

Walter Roberson
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
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)
BN
BN on 29 Jan 2020
Edited: BN on 29 Jan 2020
Thank You, I truly understand what you said. I read the floor and accumarray documentation. You are right. After thinking about what you mentioned, I use to change all zeros in precipitation data to near-zero value (0.001) then I run the code again, this time although warning still shows but all slope and y-intercepts have good values. After that, In the final results of Cmon I change every <=0.001 values to zero again. After that, I control and check my results randomly and I am sure that the results are reasonable, so I think I should don't mind the warning.
I just have an idea that maybe regress (multiple linear regression) could help me and make no warning, but as my data are okay now I think I don't consider it.
Your answer was really helpful for me so I really thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Historical Contests in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!