How to plot polynomial function correctly?

조회 수: 99 (최근 30일)
Adrian Brand
Adrian Brand 2020년 3월 24일
댓글: María Guadalupe Gama Cruz 2021년 2월 24일
Hello, I'm trying to plot polynomial function but output is always bad. Can you help me find, what I'm missing? Thank you.
This is my code:
d = @(g)1-5*g-2.5*g.^2+6*g.^3+5*g.^4;
e = -2:1/100:2;
plot(e,d(e));
This is what I'm getting:
And this is what I want:

채택된 답변

Alex Mcaulley
Alex Mcaulley 2020년 3월 24일
Your code is working fine, the difference is just the axis limits. Try this:
d = @(g)1-5*g-2.5*g.^2+6*g.^3+5*g.^4;
e = -2:1/100:2;
plot(e,d(e));
ylim([-2,5])
xlim([-3,6])

추가 답변 (1개)

John D'Errico
John D'Errico 2020년 3월 24일
편집: John D'Errico 2020년 3월 24일
Hmm. Why does this feel like deja vu? (Answer: I just answered almost the same question for someone else, not 5 minutes ago.)
d = @(g)1-5*g-2.5*g.^2+6*g.^3+5*g.^4;
e = -2:1/100:2;
plot(e,d(e));
grid on
It looks fine to me. Whats the problem? That two programs arbitrarily chose a different set of axes to plot the same curve?
axis([-3,4.5,-1.5,4.5])
Same curve. Same plot. Very different conclusions, just by changing the axis scaling.
  댓글 수: 2
Adrian Brand
Adrian Brand 2020년 3월 24일
thank you very much too.
María Guadalupe Gama Cruz
María Guadalupe Gama Cruz 2021년 2월 24일
how do you plot the x axis?

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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by