How to use the result of my matrix calculation to create 2D plot

조회 수: 1 (최근 30일)
Jun Xiao
Jun Xiao 2019년 10월 31일
댓글: Daniel M 2019년 11월 1일
I tried to plot my the result of my matrix calculation. However, my matlab said that my calculation result lambda1 or Z is not a matrix so I cannot plot without using the command plot3. But I have to plot the 2D figure with different colors represent different result of lambda1. And the commands like pcolor and color require my results to be matrix. What should I do?
n1 = 1.5;
n2 = 3.0;
b1 = 8*10.^(-18);
c1 = pi*10.^(-13);
k1 = (2*pi)*n1;
k2 = (2*pi)*n2;
d1 = [1 1; k1 -k1];
d2 = [1 1; k2 -k2];
t1 = inv(d1);
t2 = inv(d2);
for l1= 1:0.1:10
for l2= 1:0.1:10
p2= [exp(i*k2*l2) 0; 0 exp(-i*k2*l2)];
p1= [exp(i*k1*l1) 0; 0 exp(-i*k1*l1)];
M = t1*d2*p2*t2*d1*p1;
M1= real(M);
[X,Y]= eig(M1);
lambda1 = [0 1]*Y*[0;1];
lambda2 = real([1 0]*Y*[1;0]);
Z = lambda1;
hold on
end
end
pcolor(l1,l2,Z)
  댓글 수: 1
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 10월 31일
편집: KALYAN ACHARJYA 2019년 10월 31일
Formulate the question in an easy way so that it can be understood easily.

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

채택된 답변

Daniel M
Daniel M 2019년 10월 31일
l1,l2, and Z are all a single value. You need to understand how to store values in a for loop:
  댓글 수: 2
Jun Xiao
Jun Xiao 2019년 11월 1일
Thank you for the instruction. I know how to store value Z in a for loop now. But, I still don't know why my variants l1 and l2 cannot be stored as common for loop always did. Can you point it for me?
Daniel M
Daniel M 2019년 11월 1일
Those variables don't really exist outside of the for loop. They look like vectors, but they're not. Their value is only the value of that iteration, and therefore outside of the loop they only keep the value of the last iteration. Just recreate them after the loop.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by