Plotting two variables, getting graph and x intercept

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?

 채택된 답변

Star Strider
Star Strider 2020년 8월 27일
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.

댓글 수: 2

M K
M K 2020년 8월 27일
This worked beautifully! Thank you!
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Stevens
Alan Stevens 2020년 8월 27일
Here's an alternative
f = @(x,y) x.^3 + 6*x.^2 + 4*(1-y.^2);
coeff = @(y) [1 6 0 4*(1-y.^2)];
y = 0:4;
for i = 1:length(y)
R(:,i) = roots(coeff(y(i)));
end
ezplot(f)

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

질문:

M K
2020년 8월 27일

댓글:

2020년 8월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by