clear L=1; lam=500*1e-9; %a=1e-2; a=input('slit size a = '); thdiff = asin(lam/a); thimage = atan(a/L); thmax=max(thdiff,thimage); thmax=min(2.5*thmax,pi/4); fprintf('lam=%g m a=%g m L=%gm\n',lam,a,L); fprintf('a^2/(L*lam)=%g\n',a^2/(L*lam)); fprintf('First diff. minimum = %g m\n',L*tan(thdiff)); nth=max(100,20*round(thmax/thdiff)); nu=max(round(100*sqrt(2*pi*a^2/(L*lam))),100); th=linspace(-thmax,thmax,nth); u=linspace(-0.5,0.5,nu); fdonelast=0; for ith=1:nth d=sqrt(L^2+(L*tan(th(ith))-a*u).^2); ph=exp(2*pi*i/lam*d); A(ith)=trapz(u,ph./d,2); I=abs(A.^2); if(rem(ith,nth/10)==0) fprintf('%d%% ',round(100*ith/nth)); end end fprintf('\n'); clf fs=20; hold on plot(L*tan(th),I,'b-') set(gca,'fontsize',fs,'fontname','Arial') xlabel('y along screen (m)'); ylabel('Intensity (Arb.)'); aexp = floor(log10(a)); title(sprintf('Single-slit diffraction: L=%g m, $\\lambda$=%3d nm, a=%g x 10$^{%d}$ m',... L,round(lam/1e-9),a/10^aexp,aexp),'interpreter','latex'); maxx=max(L*tan(th)); maxI=max(I); axis([-maxx*1.1 maxx*1.1 0 maxI*1.2]) grid on line([-a/2 -a/2],[0 maxI*1.2],'color','r') line([a/2 a/2],[0 maxI*1.2],'color','r') fname = sprintf('single_slit_a=%ge%d.jpg',a/10^aexp,aexp); fprintf('Writing to file %s\n\n',fname); eval(['print -djpeg -r300 ' fname]);