Polynomial fit with centering & scaling
40 views (last 30 days)
Show older comments
Hello,
When I plotted the following data and applied polyfit with second order, it showed the warning like: 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. This fitting can also be done using [p, S, mu] = polyfit(x,y,2). However, the fitting coefficients from [p, S, mu] = polyfit(x, y, 2) are totally different from p=polyfit(x,y,2). How can I use the [p, S, mu] = polyfit(x,y,2) and obtain coefficients that correspond to my unscaled system p=polyfit(x,y,2)?
x y
xy = [
1508.936 19.2189
1509.159 54.27226
1509.472 102.8022
1509.852 154.8707
1510.224 199.6335
1510.809333 258.6648
1511.443429 316.4253
1512.112 370.7319
1512.4112 391.8055
1513.118545 441.4118
1514.002667 497.8672
1514.777778 547.8429
1515.6144 598.6195
1516.467 650.4304
1517.3032 701.3778 ]
0 Comments
Answers (3)
Torsten
on 17 Mar 2022
Edited: Torsten
on 17 Mar 2022
Here is an example that shows how to proceed:
x = [1,2,3];
y = [4,9,25];
order = 1;
p = polyfit(x,y,order);
[p1,S,mu] = polyfit(x,y,order);
% Transform coefficients to those of the polynomial p centered at 0
p1 = flip(p1); % Flip order to [p0, ..., pn]
p2 = zeros(1,order+1);
for i = 0:order
for k = i:order
p2(i+1) = p2(i+1) + nchoosek(k, k-i) * p1(k+1)/mu(2)^k * (-mu(1))^(k-i);
end
end
p2 = flip(p2); % Back to original order [pn, ..., p0]
% Compare
p
p2
and here is the link:
0 Comments
Walter Roberson
on 17 Mar 2022
Edited: Walter Roberson
on 17 Mar 2022
xy = [
1508.936 19.2189
1509.159 54.27226
1509.472 102.8022
1509.852 154.8707
1510.224 199.6335
1510.809333 258.6648
1511.443429 316.4253
1512.112 370.7319
1512.4112 391.8055
1513.118545 441.4118
1514.002667 497.8672
1514.777778 547.8429
1515.6144 598.6195
1516.467 650.4304
1517.3032 701.3778 ];
x = xy(:,1); y = xy(:,2);
format long g
[p, S, mu] = polyfit(x,y,2)
xscaled = (x-mu(1))/mu(2);
pscaled = polyfit(xscaled, y, 2)
syms X t
Xs = (X-mu(1))/mu(2)
punscaled = collect(subs(poly2sym(p, t), t, Xs), X)
vpa(punscaled, 16)
polyfit(x, y, 2)
0 Comments
Torsten
on 17 Mar 2022
Edited: Torsten
on 17 Mar 2022
Another option:
xy = [
1508.936 19.2189
1509.159 54.27226
1509.472 102.8022
1509.852 154.8707
1510.224 199.6335
1510.809333 258.6648
1511.443429 316.4253
1512.112 370.7319
1512.4112 391.8055
1513.118545 441.4118
1514.002667 497.8672
1514.777778 547.8429
1515.6144 598.6195
1516.467 650.4304
1517.3032 701.3778 ];
x = xy(:,1); y = xy(:,2);
order = 2;
p = polyfit(x,y,order);
[p1,S,mu] = polyfit(x,y,order);
syms t
p1s = flip(p1)*((t-mu(1))/mu(2)).^(0:order).'; % create polynomial p1
cp1s = coeffs(taylor(p1s,t,0)); % compute Taylor coefficients in t=0
cp1 = double(cp1s); % convert to double
pp = flip(cp1); % flip to original order
p
pp
0 Comments
See Also
Categories
Find more on Polynomials 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!