当每个变量都有自己的参数时,如何编写描述所有组合的多变量循环?
How to write a multivariable loop that describes all combinations when each variable has its own parameters?
我正在尝试编写一个函数来执行一维菲涅尔积分的辛普森规则积分。并且对编码来说是新的。我正在努力编写一个代码来求解积分并使用数组为所有可能的值绘制它。更准确地说,我试图弄清楚如何描述 y,其中 x 是参数,x' 在 x' 极限的循环中,这是积分的极限,如图所示。对我的代码的任何进一步建议的更改都会非常有帮助。
The format of the question that was given
这是我目前的努力。包含和缩进的块我无法摆脱。
import math
import cmath
import numpy as np
import matplotlib.pyplot as plt
NumPoints = 100 #Number of areas to be calculated for integration approximation
λ = 1*10**-6 #Wavelength
z = 0.02 #Screen distance
k = (2*math.pi)/λ #wave number
j = j=cmath.sqrt(-1)
xmin = -0.005 #x = screen coordinate
xmax = +0.005
xvals = np.linspace(xmin,xmax,NumPoints) # x intervals between limits
xdmin = 0.0 #xd = aperture coordinate
xdmax = 2*10**-5
dxd = xdmax/NumPoints
xdvals = np.linspace(xdmin,xdmax,Numpoints) # xd intervals between limits
#All figures in SI
def simpson (x, xd ):
for i in range(Numpoints):
while (xdmin<xd<xdmax):
yvals[i] = (k/2(math.pi)z)math.exp((1jk/2z)(xvals[i] - xd)**2)
integral = np.sum(yvals[i])*dxd
return integral
print("The integral =", simpson, "for the given constants." )
plt.plot(xvals,yvals)
谢谢!
如果自己写Simpon's Rule不重要,那么可以使用导入from scipy.integrate import simps
。以下代码尝试解决您的问题:
import math
import cmath
from scipy.integrate import simps
import numpy as np
# Variables
wavelength = 1*10**(-6) # Wavelength
z = 0.02 # Screen distance
k = (2*math.pi) / wavelength # Wave number
n = 100
aperture_width = 2*10**(-5)
# Define the integrand
def f(a, b):
integrand = cmath.exp(1j*k/(2*z)*(a - b)**2)
return integrand
# Paramters defining the limits of integration and x'
x = np.linspace(-0.005, 0.005, n)
xd = np.linspace(0.0, aperture_width, n)
# Create a list of y values from the integrand function above
y = [0.0]*n
for i in range(1, n):
y[i] = f(x[i], xd[i])
# Using Simpon's Rule, integrate y dx
print(k/(2*math.pi*z)*simps(y, x)) # Output is complex
运行 此代码生成输出 (-26942.956248350412-72020.42454065284j)
。如果这个答案太难看,你可以用科学记数法简化答案如下:
# Using Simpon's Rule, integrate y dx
answer = k/(2*math.pi*z)*simps(y, x)
simplified_answer = '{:.2e}'.format(answer) # Round and put in scientific notation
print(simplified_answer) # Output is complex
这样会更漂亮 -2.69e+04-7.20e+04j
。
我正在尝试编写一个函数来执行一维菲涅尔积分的辛普森规则积分。并且对编码来说是新的。我正在努力编写一个代码来求解积分并使用数组为所有可能的值绘制它。更准确地说,我试图弄清楚如何描述 y,其中 x 是参数,x' 在 x' 极限的循环中,这是积分的极限,如图所示。对我的代码的任何进一步建议的更改都会非常有帮助。 The format of the question that was given
这是我目前的努力。包含和缩进的块我无法摆脱。
import math
import cmath
import numpy as np
import matplotlib.pyplot as plt
NumPoints = 100 #Number of areas to be calculated for integration approximation
λ = 1*10**-6 #Wavelength
z = 0.02 #Screen distance
k = (2*math.pi)/λ #wave number
j = j=cmath.sqrt(-1)
xmin = -0.005 #x = screen coordinate
xmax = +0.005
xvals = np.linspace(xmin,xmax,NumPoints) # x intervals between limits
xdmin = 0.0 #xd = aperture coordinate
xdmax = 2*10**-5
dxd = xdmax/NumPoints
xdvals = np.linspace(xdmin,xdmax,Numpoints) # xd intervals between limits
#All figures in SI
def simpson (x, xd ):
for i in range(Numpoints):
while (xdmin<xd<xdmax):
yvals[i] = (k/2(math.pi)z)math.exp((1jk/2z)(xvals[i] - xd)**2)
integral = np.sum(yvals[i])*dxd
return integral
print("The integral =", simpson, "for the given constants." )
plt.plot(xvals,yvals)
谢谢!
如果自己写Simpon's Rule不重要,那么可以使用导入from scipy.integrate import simps
。以下代码尝试解决您的问题:
import math
import cmath
from scipy.integrate import simps
import numpy as np
# Variables
wavelength = 1*10**(-6) # Wavelength
z = 0.02 # Screen distance
k = (2*math.pi) / wavelength # Wave number
n = 100
aperture_width = 2*10**(-5)
# Define the integrand
def f(a, b):
integrand = cmath.exp(1j*k/(2*z)*(a - b)**2)
return integrand
# Paramters defining the limits of integration and x'
x = np.linspace(-0.005, 0.005, n)
xd = np.linspace(0.0, aperture_width, n)
# Create a list of y values from the integrand function above
y = [0.0]*n
for i in range(1, n):
y[i] = f(x[i], xd[i])
# Using Simpon's Rule, integrate y dx
print(k/(2*math.pi*z)*simps(y, x)) # Output is complex
运行 此代码生成输出 (-26942.956248350412-72020.42454065284j)
。如果这个答案太难看,你可以用科学记数法简化答案如下:
# Using Simpon's Rule, integrate y dx
answer = k/(2*math.pi*z)*simps(y, x)
simplified_answer = '{:.2e}'.format(answer) # Round and put in scientific notation
print(simplified_answer) # Output is complex
这样会更漂亮 -2.69e+04-7.20e+04j
。