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

Dennis
Dennis 2018년 7월 12일
편집: Dennis 2018년 7월 12일
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.
Hanna Al-Kowsi
Hanna Al-Kowsi 2018년 7월 12일
The values are from test data, I just put them in Excel, plotted them, and took the line of best fit.
Guillaume
Guillaume 2018년 7월 12일
편집: Guillaume 2018년 7월 12일
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
Hanna Al-Kowsi
Hanna Al-Kowsi 2018년 7월 13일
Okay, I'll try that. Thank you for the help!

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2018년 7월 12일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2018년 7월 12일

댓글:

2018년 7월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by