简体   繁体   中英

Plot cubic roots in matlab

I would like to plot the roots of the cubic equation x^{3}+Ax^{2}+1=0 in matlab. I know that there are 3 real roots for A<-1.88 and 1 if A>-1.88. I would like to plot the 3 real roots as a function of A and when it switches to 1 real root and 2 complex to plot the real root and the real part of the complex conjugate solutions all in the same plot (perhaps as 2-3 graphs).

I am a matlab beginner though. I tried

syms x A
r = solve(x^3 + A*x^2+1 == 0, x);
ezplot(vpa(r(1)),[-10,10])
ezplot(vpa(r(2)),[-10,10])
ezplot(vpa(r(3)),[-10,10])

but vpa doesnt know how to numerically evaluate r.

There's no need to do symbolic math for this,

A = (-3:0.01:0)'; % create a vector of values for A
r = arrayfun(@(A)real(roots([1 A 0 1])),A,'uni',false);  % calculate the polynomial roots for all values of A
r = [r{:}]; % convert result to a numeric array
plot(A,r');  % plot the result
grid on;
title('Real parts of Polynomial');
xlabel('A');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM