However, this approach can be generalized for higher order fittings using the binomial theorem. (https://en.wikipedia.org/wiki/Binomial_theorem ). Consider the closed form equations above, but in matrix form, with A, b, and C being the sums of the columns. \n\nM = [nchoosek(2,0) nchoosek(2,1)*(-mu1) nchoosek(2,2)*(-mu1)^2;
0, nchoosek(1,0), nchoosek(1,1)*(-mu1);
0, 0, choosek(0,0)];
T = ( p.' ./ [mu2^2 mu2 1].' ) .* M
This should hopefully make it a bit clearer that the rows of the matrix M are the coefficients of the expansion of:
The general form of which is given by the Binomial Theorem.
Lastly, a couple of caveats; This is not a recommended workflow to manually convert from scaled to non-scaled coefficients. Manually transforming the data is largely is redundant as you can produce the un-scaled coefficients by simply calling POLYFIT with the 2 output syntax in addition to the 3 output syntax. However, please note that these results can differ slightly from the manually transformed coefficients. See the well-conditioned example from the documentation below using the generalized matrix form of the transformation to demonstrate.
>> load census
>> p = polyfit(cdate,pop,2);
>> [phat,S,mu] = polyfit(cdate,pop,2);
>> T = ( phat.' ./ [mu(2)^2 mu(2) 1].' ) .* [nchoosek(2,0) nchoosek(2,1)*(-mu(1)) nchoosek(2,2)*(-mu(1))^2; 0 nchoosek(1,0) nchoosek(1,1)*(-mu(1)); 0 0 nchoosek(0,0)]
T =
1.0e+04 *
0.0000 -0.0025 2.3366
0 0.0001 -0.2298
0 0 0.0062
>> pT = sum(T)
pT =
1.0e+04 *
0.0000 -0.0024 2.1130
>> p
p =
1.0e+04 *
0.0000 -0.0024 2.1130
>> p - pT
ans =
1.0e-09 *
-0.0000 0.0008 -0.7785
Note that even in this relatively nice example there are some numerical differences since the coefficients have been calculated differently. Now let's turn our attention back to the original example
>> x = -100000:10000:200000;
f = @(x) 1000*x.^2+ 3*x + 0.023;
y = f(x);
>> p = polyfit(x,y,2);
[phat,S,mu] = polyfit(x,y,2);
T = ( phat.' ./ [mu(2)^2 mu(2) 1].' ) .* [nchoosek(2,0) nchoosek(2,1)*(-mu(1)) nchoosek(2,2)*(-mu(1))^2; 0 nchoosek(1,0) nchoosek(1,1)*(-mu(1)); 0 0 nchoosek(0,0)]
T =
1.0e+12 *
0.0000 -0.0001 2.5000
0 0.0001 -5.0000
0 0 2.5000
>> pT = sum(T)
pT =
1.0e+03 *
1.0000 0.0030 0.0000
>> p
p =
1.0e+03 *
1.0000 0.0030 0.0000
>> p - pT
ans =
0.0000 -0.0000 0.0025
In this case you will see that the numerical difference has increased by nearly 10^7. For similar situations, it is likely that the newly calculated non-scaled coefficients will not be an accurate fit for your data, and all you have done is essentially circumvented the warnings for an ill-conditioned problem.