简体   繁体   中英

Writing code to integrate 1D Fresnel Diffraction in Python

my task is to integrate the following equation for 1d Fresnel diffraction, in red in:

式

The point is that you are fourier transforming the aperture to a pattern on the screen, but for now just focussing on integrating the horizontal strip in 1d (ignoring the height for now). yprime is thus ignored. You also have fixed z and k, and j is an imaginary number. I have written the following code for it:

import math 
import numpy as np
import cmath 

k=5
z=5
x=0
j=cmath.sqrt(-1)
func=math.exp((j*k/2*z)(x-xp)*(x-xp))
def X(xp1,xp2,function,N):

    h=(xp2-xp1)/N
    y=0.0
    xp=xp1

    for x in np.arange(1, N/2 +1): #summing odd order y terms

        y+=4*f(xp)
        xp+=2*h

    xp=xp1+2*h
    for x in np.arange(0, N/2): #summing even order y terms

        y+=2*f(x)
        xp+=2*h

    integral= (h/3)*(y+f(xp1)+f(xp2))    

    return integral

print(simpson(0,5,func,10))

however, it is saying that xp is not defined. but I clearly have defined xp in the function.

Does anyone have an idea what could be wrong?

Thanks

EDIT: here is a neater version of my code. But it's still asking me to define xp..

import math
import cmath

lamda=0.2
k=(2*math.pi)/lamda
z=0.1

def expfunc(x, xp):

    func = math.exp(((1j)*k/2*z)(x-(xp))*(x-(xp)))

    return(func)


def X(xp1,xp2,x,f,N):

    h=(xp2-xp1)/N
    y=0.0
    xp=xp1

    for i in np.arange(1, N/2 +1): #summing odd order y terms

        y+=4*f(xp)
        xp+=2*h

    xp=xp1+2*h
    for i in np.arange(0, N/2): #summing even order y terms

        y+=2*f(xp)
        xp+=2*h

    integral= (h/3)*(y+f(xp1)+f(xp2))    

    return integral

print(X(0,1,x,expfunc,10))

You try to use the variable xp before you have defined it.

import math 
import numpy as np
import cmath 

k=5
z=5
x=0
j=cmath.sqrt(-1)
func=math.exp((j*k/2*z)(x-xp)*(x-xp)) #xp is not defined yet

You gave initial values for everything else except xp .

when you define func as you did

func=math.exp((j*k/2*z)(x-xp)*(x-xp))

you define a single value called func . What you probably want is something like that:

func = lambda x,xp : math.exp((j*k/2*z)(x-xp)*(x-xp))

and then change call of func to

   y+=4*f(x, xp)

and

    y+=2*f(x, xp)

I believe the issue is y+=4*f(xp) inside the first for loop of the function X .

At the very end you have print(X(0,1,x,expfunc,10)) where expfunc is acting as f in the bit of code y+=4*f(xp) . The function expfunc takes two arguments, one of them defined as xp . Although the variable you pass in f is defined with the name xp , the function only sees that you have passed in the first argument x and not the argument xp .

Further, I do not see the variable x in print(X(0,1,x,expfunc,10)) defined anywhere.

Also, the second snipit of code is much different than the first. If the same questions apply then you should remove the first snipit altogether and/or rephrase your questions because from what I see in the second chuck the error you claim to be getting should not be raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM