MATLAB: In the matlab page of Associated Legendre Polynomials, is the example for the spherical harmonics correct

associated legendre polynomialsexamplespherical harmonics

The '+ve' nodes will always be adjacent to the '-ve' modes in any vibration problem. In the example shown, upper 4 nodes are in one phase while the lower ones are in the opposite phase.

Best Answer

Hi chaitanya,
Good catch. Really good catch.
In addition to the signs of the lobes, I believe that Matlab's Y32 is functionally incorrect.
On Matlab's 'doc legendre' page there are a couple of issues. For spherical coordinates with Ylm's, most of the world uses
theta = elevation angle heading down from the z axis, 0 =< theta =< pi
Matlab is using
theta = elevation angle from the x-y plane, -pi/2 <= theta <= pi/2
Compared to the usual way of doing things, their choice of elevation angle has the effect of interchanging sin(theta) and cos(theta). Since they plug their cos(theta) into the associated legendre function, it appears that their result is not functionally correct.
They use
[Xm,Ym,Zm] = sph2cart(phi, theta, real(Y32))
The sp2cart function uses elevation from the x-y plane, which is appropriate for their theta. [In the sph2cart function itself the definition of phi and theta is swapped, which doesn't help]. But since real(Y32) can be negative, half the time that puts the point [x,y,z] on the opposite side of the origin from what you might think. That will have an effect on the signs of the lobes.
The code below doesn't use sph2cart and uses abs(real(Y32)) for the radius. The lobes do end up having alternating signs as a function of azimuthal angle phi.
delta = pi/60;
alt = 0:delta:pi;
az = 0:delta:2*pi;
[theta,phi] = meshgrid(alt,az);
l = 3;
Plm = legendre(l,cos(theta));
m = 2;
P32 = reshape(Plm(m+1,:,:), size(theta));
a = (2*l+1)*factorial(l-m);
b = 4*pi*factorial(l+m);
C = sqrt(a/b);
Y32 = C .* P32 .* exp(1i*m*phi);
R = abs(real(Y32));
Xm = R.*sin(theta).*cos(phi);
Ym = R.*sin(theta).*sin(phi);
Zm = R.*cos(theta);
surf(Xm,Ym,Zm,(real(Y32)))
title('$Y_3^2$ spherical harmonic','interpreter','latex')
xlabel('x')
ylabel('y')
absYsq = Y32.*conj(Y32);
normint = trapz(trapz(sin(theta).*absYsq))*delta^2 % should be 1
normint =
0.999999998921089
As a check, the normalization integral should equal 1, which it does.
If you ignore the signs of the lobes, do some appropriate rotations with the mouse and compare the shapes of Matlab's lobes vs. those in the code above, they're different. Also the normalization integral for Matlab, which includes a factor of cos(theta) instead of sin(theta), comes out wrong.
Had they used
Plm = legendre(l,sin(theta));
they would have gotten the correct result for Ylm, although their method of plotting might still cause some problems.