Getting incorrect plot – curve does not cross the x axis correctly
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Star Strider
2021년 4월 29일
rts = [0 2];
coefs = poly(rts)
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!