理论知识可以参考下面的这篇文章。
https://blog.csdn.net/Carifee/article/details/119831170?spm=1001.2014.3001.5501
方法1:
%%单缝衍射%%
clear
lambda=500e-9; %波长
a=1e-3; %缝宽
f=1; %透镜到像面的距离
ym=6*lambda*f/a; %屏幕上y的范围
n=51; %屏幕上的取样点数,相当于
ys=linspace(-ym,ym,n); %划定取样数组
yp=linspace(0,a,n); %将缝宽也划定数组
for i=1:n
sinphi=ys(i)/f;
u=pi*yp*sinphi/lambda;
sumcos=sum(cos(u));
sumsin=sum(sin(u));
B(i,:)=(sumcos^2+sumsin^2)/n^2;
end
N=255; %确定灰度的等级
Br=(B/max(B))*N;
subplot(1,2,1)
image(ym,ys,Br);
colormap(hot(N)); %色调处理
subplot(1,2,2)
plot(B,ys,'k');
clc
clear
lambda=632.8e-9; %波长
xmax=0.02; %x轴宽度
def=1e-4; %步长
x=-xmax:def:xmax;
y=-xmax:def:xmax;
lenm=length(x);
lenn=length(y);
for m=1:lenm
for n=1:lenn
alpha=pi*x(m)/(lambda);
beta=pi*y(n)/(lambda);
I(m,n)=((sin(beta))/(beta))^2;
end
end
I=I/(max(max(I)));
[X,Y]=meshgrid(x,y);
figure
imshow(255*I);
xlabel('x');
ylabel('y');
figure
mesh(x,y,I)
xlabel('x');
ylabel('y');
zlabel('光强');
%%光栅衍射%%
clear
lamda=500e-9; %波长
N=2; %缝数 ,可以随意更改变换
a=2e-4; %缝宽
f=5; %透镜到像面的距离
d=5*a; %光栅常数
ym=2*lamda*f/a; %屏幕上y的范围
xs=ym;
n=1001;
ys=linspace(-ym,ym,n);
for i=1:n
sinphi=ys(i)/f;
alpha=pi*a*sinphi/lamda;
beta=pi*d*sinphi/lamda;
B(i,:)=(sin(alpha)./alpha).^2.*(sin(N*beta)./sin(beta)).^2;
B1=B/max(B);
end
NC=255; %确定灰度的等级
Br=(B/max(B))*NC;
subplot(1,2,1)
image(xs,ys,Br);
colormap(hot(NC)); %色调处理
subplot(1,2,2)
plot(B1,ys,'k');
clc
clear
lambda=632.8e-9; %波长
r=0.0005; %圆孔半径
f=1; %焦距
def=1e-5;
xm=6000*lambda*f;
[x,y]=meshgrid(-xm:def:xm);
s=2*pi*r*sqrt(x.^2+y.^2)./(lambda*f);
I=4*(besselj(1,s)./(s+eps)).^2;
figure
plot(-s,I,s,I);
xlabel('s');
ylabel('光强');
figure
imshow(I*255) %二维图
xlabel('x');
ylabel('y');
figure
mesh(x,y,I) %三维图
xlabel('x');
ylabel('y');
zlabel('光强')
方法1:
clear
lamda=500e-9;
a=1e-3;
b=1e-3;
f=1;
m=500;
ym=8000*lamda*f;
ys=linspace(-ym,ym,m);
xs=ys;
n=255;
for i=1:m
sinth1=xs(i)/sqrt(xs(i)^2+f^2);
sinth2=ys./sqrt(ys.^2+f^2);
angleA=pi*a*sinth1/lamda;
angleB=pi*b*sinth2./lamda;
B(:,i)=(sin(angleA).^2.*sin(angleB).^2.*5000./(angleA.^2.*angleB.^2));
end
subplot(1,2,1)
image(xs,ys,B)
colormap(gray(n))
subplot(1,2,2)
plot(B(m/2,:),ys,'k')
clc
clear
a=0.0005; %长
b=0.0005; %宽
lambda=632.8e-9; %波长
f=1; %焦距
xm=6000*lambda*f;
def=1e-5;
I0=1;
[x,y]=meshgrid(-xm:def:xm);
alpha=(pi*a*x)/(f*lambda);
beta=(pi*b*y)/(f*lambda);
I=I0*(sin(alpha).^2.*sin(beta).^2./((alpha.^2+eps).*(beta.^2+eps)));
figure
imshow(I*255);
xlabel('x');
ylabel('x');
figure
mesh(x,y,I)
xlabel('x');
xlabel('x');
zlabel('光强');