Getting incorrect plot – curve does not cross the x axis correctly

조회 수: 1 (최근 30일)
Milan Hrstka
Milan Hrstka 2021년 4월 29일
답변: Star Strider 2021년 4월 29일
Hello,
I'm trying to plot a quadratic equation with highlighted roots and I am facing a problem – my curve does not cross x axis correctly.
For example:
y = 2x^2 - 4x, => roots are x1 = 2, x2 = 0 and I get this graph:
So the blue curve should cross the red points but does not. I dont know why.
My code for plotting this is like this:
x = [min(x1, x2)-5:0.1:max(x1, x2)+5];
y = (koef_a*x).^2 + koef_b*x + koef_c;
plot(x, y, 'b'), grid on, xlabel('x'), ylabel('y');
ax = gca; % Get handles to axis.
ax.YAxisLocation = 'origin';
ax.XAxisLocation = 'origin';
hold on;
plot(x1, 0, 'o', 'MarkerSize', 5, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r');
plot(x2, 0, 'o', 'MarkerSize', 5, 'MarkerFaceColor', 'r', 'MarkerEdgeColor', 'r');
hold off;
I would be very grateful for any help. Thanks

채택된 답변

Star Strider
Star Strider 2021년 4월 29일
The roots are [0 2], so use those with the poly function:
rts = [0 2];
coefs = poly(rts)
coefs = 1×3
1 -2 0
figure
fplot(@(x) x.^2 - 2.*x, [-5 5])
hold on
scatter(rts, [0 0], 'r', 'filled')
hold off
grid
set(gca,'XTick',-5:5)

추가 답변 (0개)

카테고리

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