5

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.

asked Mar 14, 2010 at 4:42
0

1 Answer 1

7

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
answered Mar 14, 2010 at 5:03
Sign up to request clarification or add additional context in comments.

7 Comments

Notice that r5 contains complex numbers and plot will ignore imaginary part. To plot real root only you can do: plot(r5(imag(r5)==0),zeros(sum(imag(r5)==0)),'o');
@yuk: Good catch. I updated the answer using the ISREAL function. ;)
@yuk, gnovice: Thank you both for your help, but how would i plot all roots (both img and real ) ?
@Tom: The first part of my new answer shows how to plot both the real and imaginary parts of all the roots together.
@gnovice, thanks, ended up using plot (real(r5),imag(r5),'o')
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.