function P = demo1(Phi, n,m)
% 正规化勒让德系数向前列递推算法
phi=deg2rad(Phi);
P = zeros(n+1, n+1); % 初始化P矩阵
P(1,1) = 1;
P(2,1) = sqrt(3)*cos(phi);
P(2,2) = sqrt(3)*sin(phi);
for i = 3:n+1
l=i-1;
P(i,i) = sqrt((2*l+1)/(2*l))*sin(phi)*P(i-1,i-1);
end
for i = 3:n+1
for j = 1:i-1
l=i-1;
q=j-1;
P(i,j)= sqrt((2*l-1)*(2*l+1)/(l+q)/(l-q))*cos(phi)*P(i-1,j)-sqrt((2*l+1)*(l+q-1)*(l-q-1)/(2*l-3)/(l-q)/(l+q))*P(i-2,j);
end
end
clear;
%绘制带谐函数
lmax=input('请输入l的阶数:');
t=input('请输入t的阶数(t<l):');
% 导入或加载缔合勒让德函数的系数矩阵 (coeff_matrix)
% 创建一个球面上的网格
theta = linspace(0, pi, lmax+1);
phi = linspace(0, 2*pi, lmax+1);
[theta, phi] = meshgrid(theta, phi);
% 计算缔合勒让德函数的值
L_max = lmax; % 最大阶数
m_max = lmax; % 最大角动量量子数
% 预分配内存
Y_lm = zeros(size(theta));
for L = 0:L_max
for m = -L+t:L-t
% 计算该阶和角动量量子数对应的缔合勒让德函数值
P_lm = legendre(L, cos(theta)); % 缔合勒让德多项式
P_lm = squeeze(P_lm(abs(m) + 1, :, :)); % 去除多余的维度
Y_lm_temp = sqrt((2*L + 1) / (4 * pi) * factorial(L - abs(m)) / factorial(L + abs(m))) * P_lm .* exp(1i * m * phi);
% 使用 element-wise 乘法,确保维度相符
Y_lm = Y_lm_temp;
end
end
% 绘制图像
figure;
surf(sin(theta) .* cos(phi), sin(theta) .* sin(phi), cos(theta), real(Y_lm), 'EdgeColor', 'none');
title('Re(Y_{LM}(\theta, \phi))'); % 根据需要更改标题
xlabel('x');
ylabel('y');
zlabel('z');
colorbar;
% 显示图像
axis equal;
grid on;
view([45, 30]);
![](https://img-blog.csdnimg.cn/3570a497796e41f38b18a9a9615b973b.jpeg)
![](https://img-blog.csdnimg.cn/955f3774e8aa4121b99bce3a673ff7e6.jpeg)
%绘制缔合勒让德函数曲线
% 接收用户输入的l和m值
l = input('请输入l的值:');
m = input('请输入m的值:');
% 设置x的范围
x = linspace(-1, 1, 1000); % 根据需要设置x的范围和分辨率
% 计算缔合勒让德多项式的值
P_lm = legendre(l, x);
% 获取特定项 P_{l}^{m}(x)
P_lm_m = P_lm(m + 1, :);
% 标准化 P_lm_m
P_lm_m_normalized = normalize(P_lm_m, 'range');
% 绘制标准化后的图像
plot(x, P_lm_m_normalized);
% 生成标题字符串,使用用户输入的l和m的值
title(['P_{', num2str(l), '}^{', num2str(m), '}(x)']);
xlabel('x');
ylabel(['P_{', num2str(l), '}^{', num2str(m), '}(x)']);
grid on;
![](https://img-blog.csdnimg.cn/99358a51b0a244b9a0fc46ed5df162d0.jpeg)
![](https://img-blog.csdnimg.cn/e53159abd5634788bfd50298658dc446.jpeg)
![](https://img-blog.csdnimg.cn/4ffd66a2a8eb458faad8965022067a17.jpeg)