How to plot shaded rectangles in between band gaps?

8 views (last 30 days)
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 Comments
GULZAR
GULZAR on 15 Aug 2023
I didn't try, i have no idea about how to start. If you know please help me...Thank you.
Star Strider
Star Strider on 15 Aug 2023
The obvious solution is the patch function. I tried that, however I could not figure out how your code works.

Sign in to comment.

Accepted Answer

Rik
Rik on 15 Aug 2023
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.
  1 Comment
GULZAR
GULZAR on 17 Aug 2023
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;
% lambda(IDX1)=nan;
kz=real(kz1);
kz(kz==0)=nan;
y = [pi pi pi; -pi -pi -pi; -pi -pi -pi; pi pi pi];
x = [0.426 0.56 0.7402; 0.426 0.56 0.7402; 0.453 0.58 0.75; 0.453 0.58 0.75];
figure(1)
plot(lambda,kz,'r')
hold on
plot(lambda,-kz,'r')
hold off
hold on
patch(x,y,'blue')
hold off
ylabel('wave vector')
xlabel('frequency
I tried the patch function. But i find the x values manually. How to find the x values by code. Can you know, let me know. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!