MATLAB Answers

How do I remove the z variable in the polynomial equation?

58 views (last 30 days)
Wilvard Lachica
Wilvard Lachica on 30 Jul 2020 at 2:18
Commented: Wilvard Lachica on 3 Aug 2020 at 5:17
I am trying to apply a polynomial curve fit in my data using the curve fitting toolbox. However, the polynomial equation is not directly in terms of variable x. It is somehow in terms of z variable. The curve fit I apply is only for x and y variables. How can I remove the z variable? I want to get the function that is already in y in terms of x. Hoping for your help. Thank you.

Accepted Answer

John D'Errico
John D'Errico on 30 Jul 2020 at 16:04
Edited: John D'Errico on 30 Jul 2020 at 17:09
What you don't understand is that IF you did what you want, the polynomial will become diffcult to use and difficult to evaluate in double precision arithmetic.
So, CAN you do that? Well yes. But it might sometimes be a terribly poor choice, especially for higher order polynomials.
Now, I lack your data, only a picture is provided. But it is not too difficult to give an example of what happens and the problems that can arise. Your data is roughly scattered over the interval [150,1800] in x, and it is not too complicated a function in the way it looks.
x = linspace(150,1800,30);
y = sin((x - 1000)/400)/4 + .3 + randn(size(x))/15;
plot(x,y,'o')
That has roughly the same amount of shape to it as your data, over the same interval, with a similar amount of noise. Don't complain if I did not get it perfectly, because this is after all, just an example. Perfection is a waste of time here.
You used the basic fitting tools from a plot to create that polynomial. First, while this gives you a pretty picture, in order to use the polynomial, you do NOT want to use the coefficients it shows.
What happens if we try to use polyfit directy? Thus, with no
format short g
P0 = polyfit(x,y,6)
P0 =
9.0754e-19 -4.9938e-15 1.0911e-11 -1.2623e-08 8.671e-06 -0.0029785 0.40369
Surprisingly, polyfit did not complain, although it was probably quite close to having numerical problems in the solution, based on the coefficients, and what I know about regression modelling. Those really tiny coeffcients are a symptom of a problem, in that they are impossible to accurately estimate in double precision.
The problem is, you are raising numbers that are on the order of 1000 to posers as high as the 6th power. That forces you to now add and subtract numbers that vary by nearly 20 powers of 10. And THAT is a serious problem when you are working with double precision arithmetic.
This is why the fitting tool needed to do what is called standardizing your data.
[P1,S,MU] = polyfit(x,y,6);
P1
P1 =
0.014331 0.0099421 -0.030979 -0.090154 0.00016552 0.30904 0.28267
MU =
975
500.88
As you can see now, the coefficients are all now nice, reasonably sized numbers. The idea is to use the polynomial in the form
z = (x - MU(1))/MU(2);
For example, suppose you wanted to use your polynomial near the top end of that spectrum?
X = 1800;
X^6
ans =
3.4012e+19
(X - MU(1))/MU(2)
ans =
1.6471
((X - MU(1))/MU(2))^6
ans =
19.967
So once we standardize the problem, now the linear algebra becomes a far simpler thing. Now the polynomial itself becomes a far easier thing to work with. At most, now we are adding and subtracting numbers that are no larger than roughly 20.
Can you convert this back, removing the centering and scaling? Well, yes. It would be a numerically risky thing to do, for no real gain. And you would need to know what you were doing. But, since you would probably try to do it anyway, here is a little function to do the transformation. BE VERY CAREFUL THOUGH.
function Pnew = untransformpoly(P0,MU)
% untransform a polynomial created as a centered/scaled polynomial fit from polyfit.
N = length(P0);
Pnew = zeros(1,length(P0));
% constant term.
Pnew(end) = P0(end);
if N > 1
Xn = [zeros(1,N-1),1];
for n = 1:N-1
Xn = conv(Xn,[1, -MU(1)]/MU(2),'same');
Pnew = Pnew + P0(N-n)*Xn;
end
end
You ABSOLUTELY want to use the full precision of the coefficients AND MU. If you were to use only 4 digit approximations to those numbers, then you may get garbage.
[P1,S,MU] = polyfit(x,y,6);
Pnew = untransformpoly(P1,MU)
Pnew =
Columns 1 through 4
9.0754072677916e-19 -4.9937623358492e-15 1.09114553847358e-11 -1.2623399895332e-08
Columns 5 through 7
8.67104167860174e-06 -0.00297848387230903 0.403691304932131
Those are the coefficients of the polynomial, transformed back, so now in terms of x. See if it worked.
xi = 150:1800;
plot(x,y,'bo',xi,polyval(pnew,xi),'r-')
Again, if you are not careful, thus not using the full precision of the coefficients and the variable MU, then the polynomial could get pretty nasty looking. Don't believe what you see when MATLAB only displays only 4 digits, as that is only a 4 digit approximation to the number. As it is, this computation can become a bit nasty for high order polynomials.

  1 Comment

Wilvard Lachica
Wilvard Lachica on 3 Aug 2020 at 5:17
I think this is the correct answer because I try to center and scale the data. The z variable was already removed. But, like what you said, when I try to substitute some value of x, the value of y is not precise compared to the obtain data. Thank you very much for your explanation. It really help! 🙂

Sign in to comment.

More Answers (0)