How to plot shaded rectangles in between band gaps?
조회 수: 9 (최근 30일)
이전 댓글 표시
How to add the rectangle shaded boxes in between the band gaps. Ref image attached in end of the page.
clc
close all
c = 3e8;
d1 = 0.4;d2 = 0.6;d = d1 + d2;
n1 = sqrt(12);n2 = 1;
lambda = linspace(400e-3,800e-3, 10000000);
D1 = (2*pi*n1*d1)./lambda;D2 = (2*pi*n2*d2)./lambda;
RHS = cos(D1).*cos(D2) - 0.5*(n1^2+n2^2)/(n1*n2) * sin(D1) .*sin(D2);
kz1 =acos(RHS)/d;
IDX1 = -1<RHS<1;
kz1(IDX1)=nan;
kz=real(kz1);
kz(kz==0)=nan;
figure(1)
plot(kz,lambda,'r')
xlabel('wave vector')
ylabel('frequency')
댓글 수: 3
Star Strider
2023년 8월 15일
The obvious solution is the patch function. I tried that, however I could not figure out how your code works.
채택된 답변
Rik
2023년 8월 15일
When you want to do something, try to split it into parts you can solve. In this case you want to draw rectangles. You can do so with the patch function. Now you have two problems.
The first problem is that you don't know how to use that function. Luckily for you, Matlab has excellent documentation, which includes examples. Reading the documentation and trying it should therefore solve the problem.
The second problem is that you need the coordinates of the corners. The x-coordinates are easy enough: 0 to pi. How can you imagine finding out what the y-coordinates are? Since those are the gaps between the lines, that should be where there is a large gap in the consecutive y-values when you sort them. To sort values you can use the aptly named sort function, while the diff function is the easiest way to determine the step size. Now you need to decide what steps are the normal steps between y-values, and what steps are the gaps between the lines.
As you can see, thinking back from your problem towards smaller problems you know how to solve helps in getting to a solution. That is the strategy I always use. Once I get stuck, I try to write up what input I have and what output I need, along with what I tried (and the larger context of my problem). That way people can either focus on the problem at hand, or suggest a workaround that avoids the problem I was stuck on.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!