Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot cubic roots in matlab

Tags:

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.

like image 956
user2175783 Avatar asked Mar 05 '23 18:03

user2175783


1 Answers

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');
like image 158
Phil Goddard Avatar answered Mar 12 '23 14:03

Phil Goddard