Completed Exercise 2

This commit is contained in:
Ceres 2025-12-02 18:42:02 +00:00
parent 02e81cf7bb
commit fdacfc4b09
Signed by: ceres-sees-all
GPG key ID: 9814758436430045
17 changed files with 381 additions and 10 deletions

View file

@ -37,8 +37,9 @@ def plot1D(aperture, z, k, screen_range, resolution): #Function for part 1
ax = plt.axes()
xs, intensities = genData(aperture, z, k, screen_range, resolution) #Plot intensity against distance
ax.plot(xs, intensities)
# xs, intensities = genData(2e-5, 0.05, 8.377e6, 0.015)
# ax.plot(xs, intensities)
plt.xlabel("Position (m)")
plt.ylabel("Relative Intensity")
plt.title("1D diffraction")
plt.show()
def plot2Drectangular(aperture, z, k, screen_range, resolution): #Function for part 2
@ -57,8 +58,8 @@ def plot2Drectangular(aperture, z, k, screen_range, resolution): #Function for p
for y in tqdm(ys):
xIntensities = []
for x in xs:
realpart, realerror = integrate.dblquad(Fresnel2dreal, xp1, xp2, yp1, yp2, args=(y, x, k, z), epsabs=1e-10, epsrel=1e-10)#Calculate both parts
imagpart, imagerror = integrate.dblquad(Fresnel2dimag, xp1, xp2, yp1, yp2, args=(y, x, k, z), epsabs=1e-10, epsrel=1e-10)
realpart, realerror = integrate.dblquad(Fresnel2dreal, xp1, xp2, yp1, yp2, args=(y, x, k, z))#Calculate both parts
imagpart, imagerror = integrate.dblquad(Fresnel2dimag, xp1, xp2, yp1, yp2, args=(y, x, k, z))
I = c*e0*((realpart*constant)**2+(imagpart*constant)**2)#Combine both parts and constants
xIntensities.append(I)
@ -71,6 +72,9 @@ def plot2Drectangular(aperture, z, k, screen_range, resolution): #Function for p
plt.imshow(intensity,vmin=0.0,vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r") #Plot the graphs
plt.colorbar()
plt.xlabel("X Position (m)")
plt.ylabel("Y Position (m)")
plt.title("2D diffraction with a rectangular aperture")
plt.show()
def plot2Dcircular(aperture, z, k, screen_range, resolution): #Function for part 3
@ -108,6 +112,9 @@ def plot2Dcircular(aperture, z, k, screen_range, resolution): #Function for part
plt.imshow(intensity,vmin=0.0,vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r")
plt.colorbar()
plt.xlabel("X Position (m)")
plt.ylabel("Y Position (m)")
plt.title("2D diffraction with a circular aperture")
plt.show()
def monte(aperture, z, k, screen_range, resolution, samples): #Function for part 4
@ -133,14 +140,13 @@ def monte(aperture, z, k, screen_range, resolution, samples): #Function for part
error = aperture*np.sqrt((meansq-mean*mean)/N)
return integral, error
def genData(aperture, z, k, resolution, screen_range): ~Function to generate the data
def genData(aperture, z, k, resolution, screen_range): #Function to generate the data
xs = np.linspace(-screen_range/2, screen_range/2, num=resolution) #Generate values to integrate for
ys = np.linspace(-screen_range/2, screen_range/2, num=resolution)
intensities = []
completion = 0
constant = k/(2*np.pi*z)
@ -157,8 +163,16 @@ def monte(aperture, z, k, screen_range, resolution, samples): #Function for part
intensity = genData(aperture, z, k, resolution, screen_range) #Generate data
extents = (-screen_range/2,screen_range/2,-screen_range/2,screen_range/2)
plt.imshow(intensity,vmin=1.0*intensity.min(),vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r")
for y in range(len(intensity)):
for x in range(len(intensity[y])):
if intensity[y][x] < 0.05*intensity.max():
intensity[y][x] = 0
plt.imshow(intensity,vmin=0,vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r")
plt.colorbar()
plt.xlabel("X Position (m)")
plt.ylabel("Y Position (m)")
plt.title("2D diffraction through Monte Carlo")
plt.show()
MyInput = '0' #Selection menu
@ -193,14 +207,14 @@ while MyInput != 'q':
resolution = input("Please enter the resolution of the plot (pixels): ")
plot2Dcircular(float(aperture), float(z), float(k), float(screen_range), int(resolution))
elif MyInput == '4':
print('You have chosen part (3): 2D circular diffraction usinf Monte Carlo')
print('You have chosen part (3): 2D circular diffraction using Monte Carlo')
aperture = input("Please input the desired aperture (m): ")
z = input("Please enter the desired distance from the screen (m): ")
wl = input("Please enter the desired wavelength of light (m): ")
k = (2*np.pi)/float(wl)
screen_range = input("Please enter the diameter of the screen (m): ")
resolution = input("Please enter the resolution of the plot (pixels): ")
samples = input("Please enter the desired nu,ber of samples for the calculation: ")
samples = input("Please enter the desired number of samples for the calculation: ")
monte(float(aperture), float(z), float(k), float(screen_range), int(resolution), int(samples))
elif MyInput != 'q':
print('This is not a valid choice')