Why is Matlab plotting my function wrong?

I'm trying to plot this function in MatLab Appdesigner on a UI Axes figure.
Y = (6E-15)*(X.^4) - (1E-10)*(X.^3) + (6E-7)*(X.^2) - (0.0012*X) + (0.7465)
Due to the specificity of my data, I have to manually set the X and Y limits and the tick labels of the graph. When plotting, I should get a curve that looks like this one:
But every time I plot it in the app, I get:
For reference, here's my full plotting/graph setup code.
function calculateButtonPushed(app, event) % callback to button pushed
app.UIAxes.XLim = [0. 7000];
app.UIAxes.YLim = [0, 1];
app.UIAxes.XTickLabel = [0 1000 2000 3000 4000 5000 6000 7000];
app.UIAxes.YTickLabel = (0:0.2:1.0);
X = [1622:1.0:6500]; % set X-values
Y = (6E-15)*(X.^4) - (1E-10)*(X.^3) + (6E-7)*(X.^2) - (0.0012*X) + (0.7465);
plot (app.UIAxes, X, Y);
I've tried separating out the scientific notation values into the full decimals and added/removed sets of parentheses, in the hopes it was just some error with how MatLab was reading the function, but nothing seems to work. Does anyone know what's wrong with the code/how to fix this? Thanks!

4 Comments

Dennis
Dennis on 12 Jul 2018
Edited: Dennis on 12 Jul 2018
If i copy paste your formula for x=6000 i get Y=1.3225. In your first plot the corresponding y value is around 0.9. What did you do to get the values of your first plot, because the second one seems to be the correct one.
The values are from test data, I just put them in Excel, plotted them, and took the line of best fit.
I would suggest you plot the fit in excel. I suspect that you'll find that it's not a good fit and only gets close to your curve between 2500 and 4000.
By the way, a more readable way to achieve your plot:
X = 1622:6500; %set x-values. Using [] only slows the code
coeffs = [6e-15, -1e-10, 6e-7, -0.0012, 0.7465]; %correlation coefficients
Y = polyval(coeffs, X);
plot(app.UIAxes, X, Y);
edit: Forgot to say. There is nothing wrong with your code and matlab plots the function you're giving it correctly
Okay, I'll try that. Thank you for the help!

Sign in to comment.

Answers (1)

The coefficient for the x^4 term is extremely small. When x is 6000, you're multiplying 6e-15 by about 1.3e15.
I recommend using polyfit with three output arguments to fit a polynomial to a centered and scaled version of your X data. This way instead of working with 6000^4 you're going to be working with numbers closer to 2^4. Then evaluate that fitted polynomial using polyval (making sure to pass that third output from polyfit into polyval) and plot that fitted data.

Categories

Products

Release

R2017b

Asked:

on 12 Jul 2018

Commented:

on 13 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!