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.
MATLAB: How to remove the z variable in the polynomial equation
Curve Fitting Toolboxpolynomial curve fittingz variable
Related Solutions
Ok, now that I know where you are coming from, I'll pose this as an answer. It looks correct to me.
The call to polyfit will create a polynomial model that tries to best approximate your data, thus it is an approximation as you desire, with some caveats, so let me mention them.
The errors people make when using polynomial fits almost always seem to cluster in two basic categories. They have too little data to support the degree polynomial they want to fit, or they have poorly scaled data. In fact, the latter almost can be put in the first category too.
So what does it mean to have insufficient data? For example, 2 points exactly determine a straight line. 3 points determine a quadratic polynomial. 6 points will exactly determine a 5th degree polynomial, etc. But really, you want more than an exact fit, because those exact fits will be interpolating polynomials. They will not be approximants, but interpolants. An interpolant passes exactly through the original data points. An approximant allows for error in the data, and tries to smooth through the noise.
More data is better than less. And if you have fewer than N+1 data points to fit an Nth degree polynomial, then polyfit will fail to work properly. It will also give you a nasty message telling you it is upset.
Note that i said N+1 data points, and that more is better. But replicate data points don't count. (They do help in the fit, but the fit will still fail unless you have N+1 points or more with DISTINCT x locations.) Again though, MORE is better, and significantly more is better yet.
So insufficient data is the most common failure mode for those using a polynomial fit. The second most common failure mode comes from poorly scaled data. This causes numerical problems in the linear algebra used for the fit, even when in theory, you have a technically sufficient amount of data. For example...
x = 1:6;y = rand(1,6);P = polyfit(x,y,5)P = 0.070949 -1.2754 8.548 -26.224 35.858 -16.163
So polyfit produces an interpolating polynomial there. It is total garbage, because I used random data, but who cares? It is an interpolant.
pred = polyval(P,x)pred = 0.81472 0.90579 0.12699 0.91338 0.63236 0.09754yy = 0.81472 0.90579 0.12699 0.91338 0.63236 0.09754
If you want an approximant, then use a lower order polynomial. Here, a quadratic polynomial.
P = polyfit(x,y,2)P = -0.020326 0.038862 0.75407 pred = polyval(P,x)pred = 0.7726 0.75048 0.68771 0.58429 0.44021 0.25548
As you can see, it misses the garbage data I generated.
plot(x,y,'bo',x,pred,'-Sr')
The red curve is an approximation to that random data. I guess you can't expect too much from random data. ;-)
But now let me change things in a way that should be trivial. I'll add 10000 to each x.
x2 = x + 10000;P2 = polyfit(x2,y,5)Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling asdescribed in HELP POLYFIT. > In polyfit (line 79) P2 = -1.4651e-05 0.72213 -14234 1.4026e+08 -6.9088e+11 1.3609e+15
Now polyfit throws a hissy fit, even though theoretically a polynomial should be able to interpolate those 6 points as easily as the first set.
Here, I needed to use the centering/scaling capability of polyfit.
[P2,~,mu] = polyfit(x2,y,5)P2 = 1.626 -0.41381 -4.0344 0.73114 1.7511 0.47007mu = 10004 1.8708
As you should see, polyfit is now happy. The only difference is you need to pass the vector mu into polyval when you evaluate the function.
pred = polyval(P2,x2,[],mu)pred = 0.81472 0.90579 0.12699 0.91338 0.63236 0.09754yy = 0.81472 0.90579 0.12699 0.91338 0.63236 0.09754
As you can see, it now predicts exactly, an interpolant again.
There are lots of other subtly different errors you can make with a polynomial fit, but those are the main issues you will see.
See the third item in the Description section of the polyfit documentation page. That third output tells MATLAB to center and scale your data. That centered and scaled data is used in place of the "raw" x data to create the polynomial fit.
As stated in the description of the mu output argument on that documentation page, if you ask for the third output from polyfit you'll need to specify that output as an input when or if you call polyval. This will tell polyval how to transform your data before evaluating the fit.
Looking at the data from the "Use Centering and Scaling to Improve Numerical Properties" on that documentation page, would you rather work with data on the order of 3.2e16 to fit a fifth degree polynomial or data on the order of 8?
>> x = 1750:25:2000;>> max(x.^5)ans = 3.2000e+16 >> xnorm = normalize(x, 'zscore');>> max(xnorm.^5)ans = 7.7870
Best Answer