Update Exercise 2

This commit is contained in:
Ceres 2025-12-01 18:01:35 +00:00
parent d28aae39d9
commit ed96bc6c0d
Signed by: ceres-sees-all
GPG key ID: 9814758436430045
6 changed files with 200 additions and 43 deletions

Binary file not shown.

View file

@ -11,6 +11,7 @@
\usepackage[superscript]{cite}
\usepackage{tabularx}
\usepackage{float}
\usepackage[mocha]{catppuccinpalette}
\usepackage[group-separator={,}]{siunitx}
\usepackage{geometry}
\geometry{

4
Exercise 2/.chktexrc Normal file
View file

@ -0,0 +1,4 @@
CmdLine
{
--nowarn 3 --nowarn 36
}

View file

@ -1,6 +1,7 @@
import matplotlib.pyplot as plt
import numpy as np
from scipy import integrate
from tqdm import tqdm
def Fresnel2dreal(yp, xp, y, x, k, z):
kernel = np.cos((k/(2*z))*((x-xp)**2+(y-yp)**2))
@ -13,63 +14,72 @@ def Fresnel2dimag(yp, xp, y, x, k, z):
c = 3e8
e0 = 8.85e-12
def plot1D():
def genData(aperture, z, k, screen_range):
def plot1D(aperture, z, k, screen_range, resolution):
def genData(aperture, z, k, screen_range, resolution):
y = 0
xp1=yp1=-aperture/2
xp2=yp2=aperture/2
xs = np.linspace(-screen_range/2, screen_range/2, num=1000)
xs = np.linspace(-screen_range/2, screen_range/2, num=resolution)
intensities = []
for x in xs:
realpart, realerror = integrate.dblquad(Fresnel2dreal, xp1, xp2, yp1, yp2, args=(y, x, k, z))
imagpart, imagerror = integrate.dblquad(Fresnel2dimag, xp1, xp2, yp1, yp2, args=(y, x, k, z))
constant = k/2*np.pi*z
completion = 0
for x in tqdm(xs):
realpart, realerror = integrate.dblquad(Fresnel2dreal, xp1, xp2, yp1, yp2, args=(y, x, k, z), epsabs=1e-10, epsrel=1e-10)
imagpart, imagerror = integrate.dblquad(Fresnel2dimag, xp1, xp2, yp1, yp2, args=(y, x, k, z), epsabs=1e-10, epsrel=1e-10)
I = c*e0*(realpart**2+imagpart**2)
I = c*e0*((realpart*constant)**2+(imagpart*constant)**2)
intensities.append(I)
completion = completion + 100/resolution
print(completion)
return xs, intensities
ax = plt.axes()
xs, intensities = genData(2e-4, 0.005, 8.377e6, 0.002)
xs, intensities = genData(aperture, z, k, screen_range, resolution)
ax.plot(xs, intensities)
# xs, intensities = genData(2e-5, 0.05, 8.377e6, 0.015)
# ax.plot(xs, intensities)
plt.show()
def plot2Drectangular():
def genData(aperture, z, k, screen_range):
def plot2Drectangular(aperture, z, k, screen_range, resolution):
def genData(aperture, z, k, screen_range, resolution):
xp1=yp1=-aperture/2
xp2=yp2=aperture/2
xs = np.linspace(-screen_range/2, screen_range/2, num=40)
ys = np.linspace(-screen_range/2, screen_range/2, num=40)
xs = np.linspace(-screen_range/2, screen_range/2, num=resolution)
ys = np.linspace(-screen_range/2, screen_range/2, num=resolution)
intensities = []
for y in ys:
constant = k/2*np.pi*z
completion = 0
for y in tqdm(ys):
xIntensities = []
for x in xs:
realpart, realerror = integrate.dblquad(Fresnel2dreal, xp1, xp2, yp1, yp2, args=(y, x, k, z))
imagpart, imagerror = integrate.dblquad(Fresnel2dimag, xp1, xp2, yp1, yp2, args=(y, x, k, z))
realpart, realerror = integrate.dblquad(Fresnel2dreal, xp1, xp2, yp1, yp2, args=(y, x, k, z), epsabs=1e-10, epsrel=1e-10)
imagpart, imagerror = integrate.dblquad(Fresnel2dimag, xp1, xp2, yp1, yp2, args=(y, x, k, z), epsabs=1e-10, epsrel=1e-10)
I = c*e0*(realpart**2+imagpart**2)
I = c*e0*((realpart*constant)**2+(imagpart*constant)**2)
xIntensities.append(I)
completion = completion + 100/resolution**2
print(completion)
intensities.append(xIntensities)
intensities = np.array(intensities)
return intensities
intensity = genData(2e-4, 0.005, 8.377e6, 0.002)
extents = (-0.01,0.01,-0.01,0.01)
intensity = genData(aperture, z, k, screen_range, resolution)
extents = (-screen_range/2,screen_range/2,-screen_range/2,screen_range/2)
plt.imshow(intensity,vmin=0.0,vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r")
plt.colorbar()
plt.show()
def plot2Dcircular():
def genData(aperture, z, k, screen_range):
def plot2Dcircular(aperture, z, k, screen_range, resolution):
def genData(aperture, z, k, screen_range, resolution):
xp1=-aperture/2
xp2=aperture/2
@ -79,41 +89,43 @@ def plot2Dcircular():
def yp2func(xp):
return np.sqrt((aperture/2)**2-(xp**2))
xs = np.linspace(-screen_range/2, screen_range/2, num=50)
ys = np.linspace(-screen_range/2, screen_range/2, num=50)
xs = np.linspace(-screen_range/2, screen_range/2, num=resolution)
ys = np.linspace(-screen_range/2, screen_range/2, num=resolution)
intensities = []
for y in ys:
constant = k/2*np.pi*z
for y in tqdm(ys):
xIntensities = []
for x in xs:
realpart, realerror = integrate.dblquad(Fresnel2dreal, xp1, xp2, yp1func, yp2func, args=(y, x, k, z))
imagpart, imagerror = integrate.dblquad(Fresnel2dimag, xp1, xp2, yp1func, yp2func, args=(y, x, k, z))
I = c*e0*(realpart**2+imagpart**2)
I = c*e0*((realpart*constant)**2+(imagpart*constant)**2)
xIntensities.append(I)
intensities.append(xIntensities)
intensities = np.array(intensities)
return intensities
intensity = genData(2e-5, 0.05, 8.377e6, 0.015)
extents = (-0.01,0.01,-0.01,0.01)
intensity = genData(aperture, z, k, screen_range, resolution)
extents = (-screen_range/2,screen_range/2,-screen_range/2,screen_range/2)
plt.imshow(intensity,vmin=0.0,vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r")
plt.colorbar()
plt.show()
def monte():
N = 1000
def monte(aperture, z, k, screen_range, resolution, samples):
N = samples
def doubleInteg(x, y, xp, yp, z, k, aperture):
values = []
for i in range(len(xp)):
if (xp[i]**2+yp[i]**2) > aperture:
if (xp[i]**2+yp[i]**2) > (aperture/2)**2:
values.append(0)
else:
value = np.exp(((1j*k)/(2*z))*((x-xp)**2+(y-yp)**2))
values.append(value.real)
value = np.exp(((1j*k)/(2*z))*((x-xp[i])**2+(y-yp[i])**2))
values.append(value.imag)
return np.array(values)
def monteCarlo(x, y, z, k, aperture):
@ -126,30 +138,79 @@ def monte():
error = aperture*np.sqrt((meansq-mean*mean)/N)
return integral, error
def genData(aperture, z, k, screen_range):
def genData(aperture, z, k, resolution, screen_range):
xs = np.linspace(-screen_range/2, screen_range/2, num=50)
ys = np.linspace(-screen_range/2, screen_range/2, num=50)
xs = np.linspace(-screen_range/2, screen_range/2, num=resolution)
ys = np.linspace(-screen_range/2, screen_range/2, num=resolution)
constant = k/2*np.pi*z
intensities = []
completion = 0
for y in ys:
constant = k/(2*np.pi*z)
for y in tqdm(ys):
xIntensities = []
for x in xs:
for x in tqdm(xs):
integral, error = monteCarlo(x, y, z, k, aperture)
I = c*e0*constant*integral
if I < 0.005:
I = 0
# completion = completion + 100/resolution**2
# print(completion)
xIntensities.append(I)
intensities.append(xIntensities)
intensities = np.array(intensities)
return intensities
intensity = genData(2e-4, 0.005, 8.377e6, 0.002)
extents = (-0.001,0.001,-0.001,0.001)
intensity = genData(aperture, z, k, resolution, screen_range)
extents = (-screen_range/2,screen_range/2,-screen_range/2,screen_range/2)
plt.imshow(intensity,vmin=0.0,vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r")
plt.imshow(intensity,vmin=1.0*intensity.min(),vmax=1.0*intensity.max(),extent=extents,origin="lower",cmap="nipy_spectral_r")
plt.colorbar()
plt.show()
monte()
MyInput = '0'
while MyInput != 'q':
MyInput = input('Enter a choice, "1", "2", "3", "4" or "q" to quit: ')
print('You entered the choice: ',MyInput)
if MyInput == '1':
print('You have chosen part (1): 1D rectangular diffraction')
aperture = input("Please input the desired aperture (m): ")
z = input("Please enter the desired distnce 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): ")
plot1D(float(aperture), float(z), float(k), float(screen_range), int(resolution))
elif MyInput == '2':
print('You have chosen part (2): 2D rectangular diffraction')
aperture = input("Please input the desired aperture (m): ")
z = input("Please enter the desired distnce 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): ")
plot2Drectangular(float(aperture), float(z), float(k), float(screen_range), int(resolution))
elif MyInput == '3':
print('You have chosen part (3): 2D circular diffraction')
aperture = input("Please input the desired aperture (m): ")
z = input("Please enter the desired distnce 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): ")
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')
aperture = input("Please input the desired aperture (m): ")
z = input("Please enter the desired distnce 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: ")
monte(float(aperture), float(z), float(k), float(screen_range), int(resolution), int(samples))
elif MyInput != 'q':
print('This is not a valid choice')
print('You have chosen to finish - goodbye.')

BIN
Exercise 2/main.pdf Normal file

Binary file not shown.

91
Exercise 2/main.tex Normal file
View file

@ -0,0 +1,91 @@
\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{autobreak}
\usepackage{lineno,hyperref}
\usepackage[table,x11names,dvipsnames,table]{xcolor}
\usepackage{authblk}
\usepackage{subcaption,booktabs}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage[nolist,nohyperlinks]{acronym}
\usepackage[superscript]{cite}
\usepackage{tabularx}
\usepackage{float}
\usepackage[group-separator={,}]{siunitx}
\usepackage{geometry}
\geometry{
a4paper,
papersize={210mm,279mm},
left=12.73mm,
top=20.3mm,
marginpar=3.53mm,
textheight=238.4mm,
right=12.73mm,
}
\setlength{\columnsep}{6.54mm}
%\linenumbers %%% Turn on line numbers here
\renewcommand{\familydefault}{\sfdefault}
\captionsetup[figure]{labelfont=bf,textfont=normalfont}
%\captionsetup[subfigure]{labelfont=bf,textfont=normalfont}
%%%% comment out the below for the other title option
\makeatletter
\def\@maketitle{
\raggedright\newpage
\noindent
\vspace{0cm}
\let\footnote\thanks{\hskip -0.4em \huge \textbf{{\@title}} \par}
\vskip 1.5em
{\large
\lineskip.5em
\begin{tabular}[t]{l}
\raggedright\@author\end{tabular}\par}
\vskip 1em
\@date\par
\vskip 1.5em
}
\makeatother
\begin{document}
\title{Exercise 2 Report}
\author[1]{Ceres Milner}
\affil[1]{Department of Physics, University of Bristol}
\renewcommand\Affilfont{\itshape\small}% chktex 6
\date{01.12.2025}
\maketitle
\begin{abstract}
This exercise aimed to use numerical integration methods to investigate diffraction of light through an aperture onto a screen. Two calculation methods were used, a numerical calculation and Monte Carlo integration. The numerical method gave cleaner, less `noisy' data, but the Monte Carlo method was considerably quicker.
\end{abstract}
\section{Introduction}
In this exersise we will use numerical methods to investigate the diffraction of light when passed through an aperture of different shapes. We will use two methods, first pure numerical integration methods, and then the Monte Carlo method, and compare the accuraccy and speed of these two methods. We will first create a 1 dimensional plot of the diffraction to ensure the results are as expected, and then 2 dimensional plots, using both square and curcular apertures.
\section{Theory and Methods}
To calculate the diffraction pattern of light passing through a single aperture, we will use the Fresnel diffraction integral, given by:
\begin{equation}
E(x, y, z)=\frac{i^{ikz}}{i\lambda z}\int_{-\infty}^\infty\int_{-\infty}^\infty E(x^\prime,y^\prime)\text{exp}\bigg\{\frac{ik}{2z}\big[(x-x^\prime)^2+(y-y^\prime)\big]\bigg\}\mathrm{d}x^\prime\mathrm{d}y^\prime
\end{equation}
To allow for this to be calculated computtionally, we will simplify it, integrating over the area of the aperture, and seperating out the real and imaginary parts of the equation:
\begin{align}
E(x,y,z)=\frac{kE_0}{2\pi z}\int_{x_1^\prime}^{x_2^\prime}\int_{y_1^\prime(x^\prime)}^{y_2^\prime(x^\prime)}\cos\bigg\{\frac{k}{2z}\Big[(x-x^\prime)^2+(y-y^\prime)^2\Big]\bigg\}\mathrm{d}x^\prime\mathrm{d}y^\prime \nonumber \\
+i\frac{kE_0}{2\pi z}\int_{x_1^\prime}^{x_2^\prime}\int_{y_1^\prime(x^\prime)}^{y_2^\prime(x^\prime)}\sin\bigg\{\frac{k}{2z}\Big[(x-x^\prime)^2+(y-y^\prime)^2\Big]\bigg\}\mathrm{d}x^\prime\mathrm{d}y^\prime
\end{align}
\section{Explanation of Code}
\section{Results and Discussion}
\section{Conclusion}
\end{document}