在 matlab 中绘制球谐函数

Plotting spherical harmonics in matlab

我想在笛卡尔坐标系中绘制零阶球谐函数,但 Matlab 的输出不同,不是球体。

x11 = linspace(-1,1,100);
x22 = linspace(-1,1,100);
x33 = linspace(-1,1,100);

[x1 ,x2, x3]= meshgrid(x11,x22,x33);

G = (exp((-1)*(x1.^2 + x2.^2 + x3.^2)));
isosurface(G);

谁能告诉我哪里错了,如果可能的话,告诉我如何绘制高阶球谐函数。谢谢你。

axis equal 添加到您的代码底部,这将使您的坐标轴变得等长(以像素为单位)。现在你的表面看起来像一个球体!

首先,我不建议您从评估立方体内所有可能坐标的球谐函数开始工作,然后使用 isosurface 绘制事物(我什至认为您误解了 isosurface作为某个恒定半径的切削数据(绝对不是这种情况,请参阅 documentation)。

绘制球谐函数的最佳方法是在球坐标系中使用它们的公式(r, phi, theta)。您可以找到一些模式的公式 here。仅提供 angular 部分的公式,径向部分取决于您的域。

l=1 为例,m=-1 您可以像这样在(方位角、仰角)网格上生成此谐波:

azimuths = linspace(0, 360, 361) * pi / 180;
elevations = linspace(0, 180, 181) * pi / 180;

[A, E] = ndgrid(azimuths, elevations);
H = 0.25 * sqrt(15/(2*pi)) .* exp(-1j*A) .* sin(E) .* cos(E);

然后您可以像这样将网格转换回笛卡尔网格:

X = cos(A) .* sin(E);
Y = sin(A) .* sin(E);
Z = cos(E);

您还可以添加一些径向扭曲,让事情看起来更好:

Data = abs(imag(H));
minData = min(Data(:));
maxData = max(Data(:));
Distord = (Data - minData)/(maxData-minData);

X = Distord .* cos(A) .* sin(E);
Y = Distord .* sin(A) .* sin(E);
Z = Distord .* cos(E);

surf(X, Y, Z, Data);
shading flat;

给出:

使用MATLAB的球谐函数

degree=6;
order=0;
grid=40;
radius=5;

% Create the grid
delta = pi/grid;
theta = 0 : delta : pi; % altitude
phi = 0 : 2*delta : 2*pi; % azimuth
[phi,theta] = meshgrid(phi,theta)

% Calculate the harmonic
Ymn = legendre(degree,cos(theta(:,1)))
Ymn = Ymn(order+1,:)';
yy = Ymn;
for kk = 2: size(theta,1);
    yy = [yy Ymn]
end;
yy = yy.*cos(order*phi);

order = max(max(abs(yy)));
rho = radius + 2*yy/order;

% Apply spherical coordinate equations
r = rho.*sin(theta);
x = r.*cos(phi);  % spherical coordinate equations
y = r.*sin(phi);
z = rho.*cos(theta);

% Plot the surface
clf
surf(x,y,z)
light
lighting phong
axis tight equal off
view(40,30)
camzoom(1.5)

给出: