Plotting complex roots of polynomial

Hi I need to plot of the roots of a polynomial in the complex plane. This is the code I have so far but it doesn't seem to be working.
z = -1:0.01:1;
p = z.^9 + z.^8 - z.^7 + z.^6 - z.^5 + z + 1;
plot(z,p)
hold on
r = roots(p);
scatter(real(r),imag(r))
hold off

답변 (1개)

David Goodmanson
David Goodmanson 2020년 5월 19일

0 개 추천

Hi Thomas,
what the roots function is looking for is the coefficients of the powers of z in decreasing order. In this case that would be
r = roots([1 1 -1 1 -1 0 0 0 1 1])
This shows one real root at approximately -1.9, and eight complex roots. If you use
z = -2:0.01:1;
you can see the curve go through the real root.

댓글 수: 4

Thomas Sun
Thomas Sun 2020년 5월 19일
Hi David
Thanks for the response
I tried this but it didn't seem to work - the complex roots don't seem to come up
here is the code I used
z = -2:0.01:1;
p = z.^9 + z.^8 - z.^7 + z.^6 - z.^5 + z + 1;
r = roots([1 1 -1 1 -1 0 0 0 1 1])
plot(z,p);
hold on
plot(r,'o')
% scatter(real(r),imag(r)) % this one works too
hold off
Thomas Sun
Thomas Sun 2020년 5월 19일
Hi Dave
I tried your code but with it a graph for p doesnt even show up
David Goodmanson
David Goodmanson 2020년 5월 19일
what do you get for the sizes of z and p?
maybe if you tried 'close all' and put 'figure(1)' just before the plot command ....?

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

카테고리

도움말 센터File Exchange에서 Polynomials에 대해 자세히 알아보기

태그

질문:

2020년 5월 19일

댓글:

2020년 5월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by