该代码产生了菲涅耳衍射的不精确图形

2024-04-24 21:37:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试绘制一维水平衍射图,并编写了以下代码:

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(与屏幕的距离)时,得到的图形到处都是。我看不出我的代码有什么问题

谢谢


Tags: inimportfornppltxpxminintegral