MATLAB: Plotting two variables, getting graph and x intercept

plot

I need to plot the equation :
0 = x^3 + 6x^2 + 4(1 – y^2)
The variable y needs to take values 0, 1, 2, 3, 4. I'm wondering if there is a loop I can do for this.
I would also like to get the x-intercepts as an output.
Is there a way to plot them on single graphs and all on the same graph?

Best Answer

Try this:
xv = linspace(-6, 4, 25);
yv = [1 2 3 4];
for k = 1:numel(yv)
ply = [1 6 0 4*(1-yv(k).^2)];
rts(:,k) = roots(ply);
pv(k,:) = polyval(ply, xv);
end
figure
plot(xv, pv)
grid
The x-intercepts will be the roots, returned column-wise in the ‘rts’ matrix.