Trying to find best fit of the shape y = a/(x-b)+c. Getting warning of badly conditioned polynomial sometimes but not always.
1 view (last 30 days)
Arrays x and y fit the equation y = a/(x-b)+c almost perfectly, but this was the only technique I could come up with to incorporate the x-shift caused by the constant 'b'.
f = @(b)(polyfitNormR((x - b).^-1, y));
xAdjustment = fminsearch(f,0);
function [normr] = polyfitNormR(x,y)
[~,errorData] = polyfit(x,y,1);
normr = errorData.normr;
It works perfectly every time, but I have one particular instance where I get a bunch of warnings and it runs extremely slowly. The fits still look good but I would like to avoid a code that causes warnings in some circumstances. The reason it shows a warning is because fminsearch somehow reduces the array 'x' to essentially the same number on the order of 1E-4 or smaller (even though I am originally passing an array on the order of 1E-1).
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.
I am looking for either a different technique to find this best fit, or an explanation of how to amend my code to avoid this warning using the current technique.
Ajay Pattassery on 9 Aug 2019
Try scaling and shifting the input x such that the mean of x is zero and the standard deviation as one. This scaling and shifting improves the numerical properties of both the polynomial and the fitting algorithm.
See the doc polyfit for more details.
The input data (x) will be automatically centered and scaled if polyfit is used with three output values.
[p,S,mu] = polyfit(x,y,n);
mu(mu(1) is mean(x), and mu(2) is std(x)) can then be given as the fourth argument in polyval (which evaluates the polynomial p at each point in x) to convert back easily.
[y,delta] = polyval(p,x,S,mu);