Reputation: 403
I lost two days of trying to do this but with no result. How can I plot the quadratic equation's parabola and roots. Something like this. I just need to be able to see the parabola and that it crosses the abscissa at the write coordinates.
Here is what I have:
x = linspace(-50,50);
y = 1.*x.*x - 8.*x + 15;
plot(x,y)
hold on;
grid()
rts = roots([1,-8,15]);
plot(rts, zeros(size(rts)), 'o', "color", "r")
And the result is:
As you can see, the top of the parabola at 0 ordinate, instead of under it. I will appreciate your help!
Upvotes: 2
Views: 5511
Reputation: 22449
Christoph is right the plot could be misleading because a great linspace can flatten the part of the curve below the abscissa, and if you don't think and you make some error calculating the vertex, as I've done you are fried! This is another solution, I hope this one is right!
ezplot(@(x,y) x.^2 -x.*8 -y.+ 15)
hold on
grid on
rts = roots([1,-8,15]);
plot(rts,zeros(size(rts)),'o',"color","r");
line(xlim,[0 0], 'linestyle', '--')
Upvotes: 0
Reputation: 48390
Using a smaller linspace
range works fine for me:
x = linspace(1,6);
y = 1.*x.*x - 8.*x + 15;
plot(x,y)
hold on;
grid()
rts = roots([1,-8,15]);
plot(rts, zeros(size(rts)), 'o', "color", "r")
Upvotes: 2