代码为菲涅耳衍射生成不准确的图形

时间:2018-01-28 17:00:59

标签: python graph integration

我一直试图绘制1d水平衍射图的图形,并编写了以下代码:

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,距离屏幕的距离时,我得到的图表就到处都是。我没有看到我的代码有什么问题

由于

1 个答案:

答案 0 :(得分:0)

频率似乎有问题。您是否可能在expfunc()return cmath.exp(1j*k*...错过减号?