% 绘制球谐函数图像
% degree, Ynm的n, 阶
% order, Ynm的m, 级
% method, 绘图的R
%% 示例 - 绘制球谐函数表
maxD = 3; % 最高阶数
for n = 0:maxD
for m = 0:n
subplot(maxD+1,maxD+1,n*(maxD+1)+m+1)
plot_sph_harm(n,m,2);
end
end
function plot_sph_harm(degree,order,method)
%% 坐标
theta = 0:pi/30:pi; % polar angle
phi = 0:pi/30:2*pi; % azimuth angle
[phi,theta] = meshgrid(phi,theta); % define the grid
%% 计算球谐函数
Ln = legendre(degree,cos(theta(:,1)),'norm'); % 一行对应一个order
yy = repmat(Ln(order+1,:)',[1,size(theta,2)]); % Ln(i+1,:)对应order=i
Ynm = yy.*cos(order*phi); % 球谐函数, 相当于绘图的color
%% R的选择
if nargin
switch method
case 1
R = ones(