评估半球内点的函数并在 Python 中绘制幻灯片
我正在尝试评估一个函数,该函数取决于从球体中心到半球体内任意点的半径。
我首先定义三个数组,分别对应沿半径、仰角和方位角的点。在 for 循环中,我计算 x、y 和 z 坐标来评估函数。
我不确定我是否正确进行了映射。我需要将求值函数的值存储在与 x、y 和 z 坐标相对应的 3D 矩阵中,以便在后处理步骤中绘制切片,但我一直在确定如何定义函数矩阵的大小。
在笛卡尔坐标系中非常容易,因为可以将每个坐标与矩阵的维数联系起来。这就是为什么我需要一些关于如何滑动矩阵的指导,因为我没有带有笛卡尔坐标的 3D 矩阵。我如何从球坐标构建这个矩阵?
任何帮助将不胜感激!
这是我的(没有成果的)尝试:
import numpy as np
beta = 1
rho = np.linspace(0, 1, 20)
phi = np.linspace(0, 2*np.pi, 20)
theta = np.linspace(0, np.pi/2, 10)
f = np.empty([len(theta), len(theta), len(phi)], dtype=complex)
for i in range(len(rho)):
for j in range(len(phi)):
for k in range(len(theta)):
x = rho[i] * np.sin(theta[k]) * np.cos(phi[j])
y = rho[i] * np.sin(theta[k]) * np.sin(phi[j])
z = rho[i] * np.cos(theta[k])
R = np.sqrt(x**2 + y**2 + z**2)
f[k, i, j] = -1j*((z/R)/(z/R + beta)) * (np.exp(1j*k*R)/R)
I am trying to evaluate a function that depends on the radius from the center of a sphere to any point inside half a sphere.
I start by defining three arrays corresponding to the points along the radius, the elevation and azimuthal angles. In a for
loop I compute the x, y and z coordinates to evaluate the function.
I am not sure if I am doing the mapping properly. I need to store the values of the evaluated function in a 3D matrix corresponding to the x, y, and z coordinates to plot slices in a postprocessing step, but I am stuck identifying how I can define the size of my function matrix.
In cartesian coordinates is really easy since one can link every coordinate with the dimension of the matrix. That's why I need some guidance in how I can slide the matrix since I don't have a 3D matrix with the cartesian coordinates. How I can construct this matrix from the spherical coordintaes?
Any help will be more than appreciated!
Here is my (unfruitful) attempt:
import numpy as np
beta = 1
rho = np.linspace(0, 1, 20)
phi = np.linspace(0, 2*np.pi, 20)
theta = np.linspace(0, np.pi/2, 10)
f = np.empty([len(theta), len(theta), len(phi)], dtype=complex)
for i in range(len(rho)):
for j in range(len(phi)):
for k in range(len(theta)):
x = rho[i] * np.sin(theta[k]) * np.cos(phi[j])
y = rho[i] * np.sin(theta[k]) * np.sin(phi[j])
z = rho[i] * np.cos(theta[k])
R = np.sqrt(x**2 + y**2 + z**2)
f[k, i, j] = -1j*((z/R)/(z/R + beta)) * (np.exp(1j*k*R)/R)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你只是有一个错字,第二个维度又是 len(theta) 而不是 len(rho)。还应该
注意的是,如果我没记错的话,你根本不需要
R
,它只是rho[i]
。You just have a typo, the second dimension is again len(theta) isntead of len(rho). It should be
Note also that, if I am not mistaken, you don't need
R
at all, it's justrho[i]
.