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 gdisp(p5)pp5 = polyval(p5, MContent);hold onplot(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 gdisp(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 allMContent = [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 gdisp(p5)pp5 = polyval(p5, MContent);hold onplot(MContent, pp5, '-p')legend('M.C. (original)', '5th degree polynomial', 'Location', 'NorthEast')hold offyAxis = (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');
Best Answer