Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Octave / Matlab: How to plot the roots of a polynomial

Im trying to plot the roots of a polynomial, and i just cant get it.

First i create my polynomial

p5 = [1 0 0 0 0 -1] %x^5 - 1
r5 = roots(p5)
stem (p5)

Im using the stem function, but I would like to remove the stems, and just get the circle around the roots.

Is this possible, is stem the right command?

Thanks in advance,

PS: This is not homework, but very close, will tag it if requested.

like image 508
Tom Avatar asked Mar 14 '10 04:03

Tom


People also ask

How do you find the roots of a polynomial in octave?

Octave can find the roots of a given polynomial. This is done by computing the companion matrix of the polynomial (see the compan function for a definition), and then finding its eigenvalues. p(x) = x^2 - 5. Note that the true result is +/- sqrt(5) which is roughly +/- 2.2361.

How do you find the roots of a polynomial in Matlab?

r = roots( p ) returns the roots of the polynomial represented by p as a column vector. Input p is a vector containing n+1 polynomial coefficients, starting with the coefficient of xn. A coefficient of 0 indicates an intermediate power that is not present in the equation.

How do you write roots in Matlab?

Description. B = sqrt( X ) returns the square root of each element of the array X . For the elements of X that are negative or complex, sqrt(X) produces complex results.


1 Answers

If you have complex roots that you want to plot with the real part on the x-axis and the imaginary part on the y-axis, you can just use the PLOT function:

plot(r5,'o');

If you are wanting to plot the function and the roots together, you will have to ignore the complex roots (as yuk mentions in the comment below):

p5 = [1 0 0 0 0 -1];
r5 = roots(p5);
realRoots = r5(isreal(r5));  %# Gets just the real roots
x = -2:0.01:2;               %# x values for the plot
plot(x,polyval(p5,x));       %# Evaluate the polynomial and plot it
hold on;                     %# Add to the existing plot
plot(realRoots,zeros(size(realRoots)),'o');  %# Plot circles for the roots
like image 63
gnovice Avatar answered Sep 17 '22 21:09

gnovice