Diffraction grating lines in Python

Viewed 43

I am trying to reproduce a diffraction grating pattern (figure described below) using Python. However, I am facing issues with that. Here is a summary of the theory and the problem I am facing:

The intensity of the diffraction pattern on the screen, a distance $x$ from the central axis of the system is given by:

Formula for the intensity:

enter image description here

where u is the distance from the central axis (the dashed line in the figure below)

The schematic diagram in the problem:

enter image description here

I need to reproduce a figure that has a central bright fringe and alternating bright and dark fringes (with reducing intensities) to the left and right of the central fringe. So far, I have the following piece of code:

import numpy as np
from scipy.integrate import trapz

def Intensity_img(u,x):
    wavelength = 500*1e-3 # micro meters
    f = 1e+6 # micro meters
    alpha = np.pi/20
    w = 1000
    
    return np.cos(np.pi*u/w) * np.sin(alpha*u)*np.sin(2*np.pi*x*u/(wavelength*f))

x = np.arange(-50000,50100,100)
y = np.arange(-50000,50100,100)
# X, Y = np.meshgrid(x,y)
print(len(x))
a = -500
b = 500
N = 1000
u = np.arange(a,b+1,1)
print(len(u))
I_img = np.full((len(x),len(x)),0)
I_img = np.float128(I_img)

for i in range(len(x)-1):
    y_img = Intensity_img(u,x[i])
        
    I_img[i] = trapz(y_img,u)
    
I = I_img**2

The real part of the intensity function integrates to 0 (because the real part is an odd function over a symmetric interval). The imaginary part needs to be integrated and I have used the trapz method from the scipy library.

When I produce a density plot of this function, I get something very different:
Two, horizontal bright fringes and a central (horizontal) dark fringe

enter image description here

Any helpful tips or hints would be highly appreciated. I think that I am doing something wrong in the for-loop.

1 Answers

It looks like you have a mistake in you formula expression:

return np.cos(np.pi*u/w) * np.sin(alpha*u)*np.sin(2*np.pi*x*u/(wavelength*f))

Should be:

return np.cos(np.pi*u/w) * np.sin(alpha*u)*np.exp(2*np.pi*x*u/(wavelength*f))

Notice the np.exp instead of np.sin.
Now, if it was a copy mistake when you inserted your code here, please let me know.

Related