Polynomial equation not giving expected result
Mostrar comentarios más antiguos
I have a set of X axis values that are known moisture content numbers. These values give me the Y axis values in a microcontroller as shown below.
MContent = [1.278096078 22.26039162 31.88752617 40.4470463 45.821662 51.19483315 54.14512732 58.15256688 62.08950419 65.25710918 67.5910596 69.40480782 71.08289411 73.09132161];
BitValue = [1020 960 862 657 440 380 341 193 193 186 179 112 106 85];
When plotting this I will get the following output.

I wanted to obtain a polynomial equation that will fit this line. Therefore I created a 5th order polynomial as shown below.
p5 = polyfit(MContent, BitValue, 5);
format long g
disp(p5)
pp5 = polyval(p5, MContent);
hold on
plot(MContent, pp5, '-p')
Which gives this output.

This line is a satisfactory fit and I intended to use the equation obtained from this to get bit values when I add the moisture content into the 'x' value. To get this polynomial equation I used the following command.
p5 = polyfit(MContent, BitValue, 5);
format long g
disp(p5)
Which gives me
Columns 1 through 4
-1.3759358768261e-05 0.00276912138617569 -0.193482771635317 5.27065703595948
Columns 5 through 6
-53.9134842525771 1080.5582927381
Ignoring the x^5 value, as it is too small, I will use the rest and get the following equation.
yAxis = (0.00276912138617569*(MContent.^4))-(0.193482771635317*(MContent.^3))+(5.27065703595948*(MContent.^2))-(53.9134842525771*(MContent.^1))+(1080.5582927381*(MContent.^0));
But when I try to plot
figure;plot(MContent, yAxis, '-x');
I get the folllowing plot

Which is no where near to the original MContent to BitValue plot. I would like to know why, and how I can get an equation that will fit my original 'MContent' to 'BitValue' plot.
The full code is shown below for reference.
close all
MContent = [1.278096078 22.26039162 31.88752617 40.4470463 45.821662 51.19483315 54.14512732 58.15256688 62.08950419 65.25710918 67.5910596 69.40480782 71.08289411 73.09132161];
BitValue = [1020 960 862 657 440 380 341 193 193 186 179 112 106 85];
plot(MContent, BitValue, ':s', 'MarkerSize', 6, 'MarkerFaceColor', 'b')
title('MContent to bit value from 12th March 2019')
xlabel('Moisture Content (%)')
ylabel('Bit value (bits)')
p5 = polyfit(MContent, BitValue, 5);
format long g
disp(p5)
pp5 = polyval(p5, MContent);
hold on
plot(MContent, pp5, '-p')
legend('M.C. (original)', '5th degree polynomial', 'Location', 'NorthEast')
hold off
yAxis = (0.00276912138617569*(MContent.^4))-(0.193482771635317*(MContent.^3))+(5.27065703595948*(MContent.^2))-(53.9134842525771*(MContent.^1))+(1080.5582927381*(MContent.^0));
figure;plot(MContent, yAxis, '-x');
2 comentarios
Pawel Tokarczuk
el 18 de Mzo. de 2019
What you're seeing is the result of numerical instability.
You should start by centring and scaling your x-variable:
mc = (MContent - 50.0)/50.0;
dpb
el 18 de Mzo. de 2019
While standardizing would be agoodthing™, numerical issues aren't the problem here; the problem is the OP just ignored one coefficient that once x gets sizable is by far the dominant factor.
The same effect would be true if the coefficients were computed for normalized/standardized variables.
Respuesta aceptada
Más respuestas (1)
Chamath Vithanawasam
el 18 de Mzo. de 2019
0 votos
Categorías
Más información sobre Polynomials en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

