' dif3.bas - jackord@kw.igs.net - 16 Oct 06 - Liberty Basic v4.02 ' The diffraction grating: 1, 3, or 5 slits of width w=2 microns ' with a separation between slits of w, 2w, or 3w (at a separation of w ' the pattern is that of a single slit of width n*w) ' The intensity pattern is that viewed on a screen 1 m from the grating ' with one pixel representing 1 mm in the x direction ' The wavelengths are red C, yellow D, green E, and blue F ' A line plot (in magenta) of the analytic intensity expression for red light ' is superimposed for the case of 3 slits spaced 2w apart ' Initialize Window nomainwin button#1, "Plot", [q1], UL, 126, 2, 40, 20 WindowWidth=490 ' Pixel Scale 0-480 WindowHeight=269 ' Pixel Scale 0-240 UpperLeftX=10: UpperLeftY=10 dim a$(3): a$(0)="d=w": a$(1)="d=2w": a$(2)="d=3w" dim b$(3): b$(0)="n=1": b$(1)="n=3": b$(2)="n=5" combobox#1.chd, a$(, [waitHere], 2, 2, 60, 60 combobox#1.chn, b$(, [waitHere], 64, 2, 60, 60 open "Diffraction Grating" for graphics_nsb as #1 #1 "trapclose [quit]" dim la(4) la(0)=.6563/1000: la(1)=.589/1000 ' Red C, Yellow D la(2)=.5269/1000: la(3)=.4861/1000 ' Green E, Blue F dim c$(4) c$(0)="red": c$(1)="yellow": c$(2)="green": c$(3)="blue" #1.chd "selectindex 2" #1.chn "selectindex 2" #1 "cls ; down ; color black ; backcolor white" [waitHere] wait [q1] goto [plot] [plot] #1.chd "selectionindex?": input#1.chd, nd ' Slit spacing #1.chn "selectionindex?": input#1.chn, n: n=2*n-1 ' Slit number #1 "cls ; down ; color black ; backcolor white" pi=4*atn(1): w=.002: x0=240: z=1000: zz=z*z nw=5 for j=0-int(n/2) to int(n/2) ' Show slit pattern for i=0-int(nw/2) to int(nw/2) x1=x0+j*nd*nw+i #1 "line "; x1; " 10 "; x1; " 30" next i next j nw=141 ' Use 141 point model sy=200/(n*n*nw*nw) for nl=0 to 3 ' Wavelength loop #1 "color "; c$(nl) for nx=0 to 240 ' Screen loop r0=sqr(zz+nx*nx): sr=0: si=0 ' Algorithm C for j=0-int(n/2) to int(n/2) ' Slit number loop for i=0-int(nw/2) to int(nw/2) ' Slit loop xg=(j*nd*nw+i)*w/nw t=2*pi*nx*xg/r0/la(nl): a=.5*(1+z/r0)*sqr(z/r0) sr=sr+a*cos(t): si=si+a*sin(t) next i next j x2=240-int(sy*(sr*sr+si*si)) if x2<240 then ' Plot Intensity #1 "line "; x0+nx; " "; x2; " "; x0+nx; " 240" #1 "line "; x0-nx; " "; x2; " "; x0-nx; " 240" end if next nx next nl if n=3 and nd=2 then ' If d=2w and 3 slits #1 "color darkred" ' Dark red curve x1=40 for nx=1 to 240 t=atn(nx/z) ' Analytic calculation p0=2*pi*w*sin(t)/la(0) p0=sin(p0/2)/(p0/2) p1=2*pi*nd*w*sin(t)/la(0) p1=sin(n*p1/2)/sin(p1/2) x2=240-int(200*p0*p0*p1*p1/n/n) if x1<240 or x2<240 then #1 "line "; x0+nx-1; " "; x1; " "; x0+nx; " "; x2 ' Analytic curve #1 "line "; x0-nx+1; " "; x1; " "; x0-nx; " "; x2 end if x1=x2 next nx end if goto [waitHere] [quit] close #1 end