Vector addition of scattering amplitudes from diffraction gratings
Matlab code
% Movie of vector sum of grating scattering amplitudes
clear; close all;
vid = VideoWriter('gratingVector.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 60;
open(vid);
prompt = 'Number of homogeneously illuminated grating grooves [default = 10]: ';
ngrooves = input(prompt);
if isempty(ngrooves)
ngrooves = 10;
end
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);
set(gca,'linewidth',7);
hold off;
lp = [0.4 -0.4 0.7];
philblue = [0.32 0.35 0.7];
light('Position',lp,'Style','infinite');
IntPlotx = 0:pi/360:8*pi;
IntPloty = 0.0*IntPlotx;
for i = 0:pi/360:8*pi
subplot(1, 2, 1)
newplot
x = 0;
y = 0;
for j = 1:ngrooves
mArrow3([x y 0],[x+cos(j*i) y+sin(j*i) 0],'color',[1 0 0],'stemWidth',...
0.025*ngrooves/10,'tipWidth',0.07*ngrooves/16,'facealpha',0.8);
x = x+cos(j*i);
y = y+sin(j*i);
end
mArrow3([0 0 0],[x y 0],'color',philblue,'stemWidth',...
0.035*ngrooves/10,'tipWidth',0.08*ngrooves/10,'facealpha',0.8);
LL = ngrooves;
str1 = '\phi = ';
str2 = num2str(i/pi,'% 4.3f');
str3 = ' pi';
strTot = [str1,str2,str3];
hText = text(7*ngrooves/10, 8*ngrooves/10, strTot, 'FontSize',16);
axis square;
axis equal;
axis on;
set(gca,'fontsize',18,'linewidth',2);
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
ax.XTick = -round(LL/2):LL/10:LL;
ax.YTick = -LL:LL/10:LL;
light('Position',lp,'Style','infinite');
xlim([-LL/2 LL]);
ylim([-LL LL]);
zlim([-LL LL]);
view(0,90);
hold off;
subplot(1, 2, 2)
newplot
x = 0;
y = 0;
for j = 1:ngrooves
x = x+cos(j*i);
y = y+sin(j*i);
end
k = round((i*360/pi))+1;
IntPloty(k) = x^2+y^2;
plot(IntPlotx(1:k),IntPloty(1:k),'color',philblue, 'Linewidth', 2)
LL = ngrooves;
axis square;
%axis equal;
axis on;
ax = gca;
set(gca,'XTick',0:pi:8*pi)
set(gca,'XTickLabel',{'0','\pi','2\pi','3\pi','4\pi','5\pi','6\pi','7\pi','8\pi'})
set(gca,'fontsize',18,'linewidth',2);
light('Position',lp,'Style','infinite');
xlim([0 8*pi]);
ylim([0 (11/10)*LL^2]);
xlabel('Phase angle');
ylabel('Intensity');
view(0,90);
hold off;
% Store the frame
frame = getframe(gcf);
writeVideo(vid,frame);
hold off;
end
hold off;
% Output the movie as an mpg file
close(vid);