Getting incorrect plot – curve does not cross the x axis correctly
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Milan Hrstka
el 29 de Abr. de 2021
Respondida: Star Strider
el 29 de Abr. de 2021
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 comentarios
Respuesta aceptada
Star Strider
el 29 de Abr. de 2021
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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!