我一直在尝试绘制一维水平衍射图案的图形,并编写了以下代码:
import math
import cmath
import numpy as np
import matplotlib.pyplot as plt
lamda=0.2
k=(2*math.pi)/lamda
z=0.005
def expfunc(x,xp):
return cmath.exp(1j*k*((x-xp)**2)/(2*z))
def X(xp1,xp2,x,xp,expfunc,N):
h=(xp2-xp1)/N
y=0.0
for i in np.arange(1, N/2 +1): #summing odd order y terms
y+=4*expfunc(x,xp)
xp+=2*h
xp=xp1+2*h
for i in np.arange(0, N/2): #summing even order y terms
y+=2*expfunc(x,xp)
xp+=2*h
integral= (h/3)*(y+expfunc(x, xp1)+expfunc(x, xp2))
integral= (integral.real)**2
return integral
NumPoints = 90000
xmin = 0
xmax =20
dx = (xmax - xmin) / (NumPoints - 1)
xvals = [0.0] * NumPoints
yvals = np.zeros(NumPoints)
for i in range(NumPoints):
xvals[i] = xmin + i * dx
yvals[i] = X(xmin,xmax,xvals[i],0.1,expfunc,200)
plt.plot(xvals,yvals)
plt.show()
该图旨在是一个 sinc 函数,但是当我改变参数 N、间隔数和 z(与屏幕的距离(时,我得到的图到处都是。我看不出我的代码出了什么问题
谢谢
频率似乎有问题。您是否可能在return cmath.exp(1j*k*...
expfunc()
缺少减号?