How do I plot complex numbers or moduli?

조회 수: 8 (최근 30일)
Sean
Sean 2017년 6월 25일
댓글: KSSV 2017년 6월 25일
Dear Readers,
I have this code. However, there might be a mixture of imaginary and real eigenvalues. I want to plot everything or, if alle eigenvalues are complex, only the moduli. How do I go about this?
eigVals = zeros(2,numel(p));
figure
for k = 1:numel(p)
A = J(c(k), p(k), R(k), c2(k), p2(k));
eigVals(:,k) = eig(A);
hold on;
plot(p(k), eigVals(:,k)', '*');
end

답변 (1개)

KSSV
KSSV 2017년 6월 25일
You can check whether a value is real or complex using isreal. To get moduli or magnitude of complex number you can use abs. Read about isreal, abs. https://in.mathworks.com/help/matlab/complex-numbers.html
  댓글 수: 2
Sean
Sean 2017년 6월 25일
Great. I adapted the code, however plotting gives the error 'Data must be a single matrix Y or a list of pairs X,Y.'
eigVals = zeros(2,numel(p));
real_ev = [ ];
imag_ev = [ ];
figure
for k = 1:numel(p)
A = J(c(k), p(k), R(k), c2(k), p2(k));
eigVals(:,k) = eig(A);
real_ev(:,k) = real(eigVals(:,k));
imag_ev(:,k) = imag(eigVals(:,k));
hold on;
plot(p(k), real_ev(:,k)', 'g*', imag_ev(:,k)', 'b*');
end
Thanks in advance
KSSV
KSSV 2017년 6월 25일
Size of p(k) and real_ev(:,k) should be same to plot. Also the plot line should be plot(p(k), real_ev(:,k)', 'g*', p(k),imag_ev(:,k)', 'b*');

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

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by