MATLAB Is MATLAB's Implementation of Spherical Harmonics Incorrect?

Click For Summary
The discussion centers on the potential inaccuracies in MATLAB's implementation of spherical harmonics, particularly regarding the use of the Legendre function, which may lead to incorrect results for certain input values. The original poster discovered that their function, along with MATLAB's implementations, produced erroneous outputs when evaluating spherical harmonics for specific angles. A critical issue identified is the reliance on the cosine of theta, which should be replaced with the sine function for accurate calculations. The poster seeks guidance on correcting this implementation, expressing concern about the implications of these errors on published research. The conversation also highlights the importance of valid input ranges for theta, suggesting that negative values may not be appropriate.
t14g0
Messages
1
Reaction score
0
Hey guys, This is my first post here, so I will apologize in advance in case I'm posting this in the wrong section.

I wrote a very simple function to calculate spherical harmonics in matla, and I used this function during 3 years. Yesterday I found that the function was actually wrong, and looking up on MATLAB function database I realized that EVERY implementation were wrong.

This is the code i use, and is very similar to the rest of the codes I found online:

function Y = spherical_harmonic(l,m,THETA,PHI)

Lmn=legendre(l,cos(THETA),'norm');
mm = m;
m = abs(m);

if l~=0
Lmn=squeeze(Lmn(m+1,:,:));
end

C=(-1)^m / sqrt(2*pi);
Y = C * Lmn .* exp(i*m*PHI);

if mm<0
Y = (-1)^m * conj(Y);
end

Let me exemplify one test: if you try to find the spherical harmonics for m=1, l=1, theta = pi/2 and phi = 0 it will give the correct answer (-0.3455). Now try to find the spherical harmonics for m=1, l=1, theta = - pi/2 and phi = 0, it will give you the same answer (-0.3455). But the correct answer is (0.3455). You can verify this by getting the closed formula solution for the spherical harmonics of l=1 and m=1 (sine dependent).

The thing is, using the implementation given by MATLAB of the legendre function the cosine of theta will always be squared. The correct way to implement this was to replace 1-cos(theta)^2 for sine(theta)^2, and do the rest of the operations. But for this I would have to implement the spherical harmonics using a symbolic language. Mathematica does this way, and always gives you the correct values (I don't know if they have a table with the closed formula solution for every combination of l and m, or if they do some magical trick inside, but the thing is, they version of the code works).

I don't know how to make it right, does anyone have any tips for me?

P.S: I know of a LOT of people who uses MATLAB to compute spherical harmonics and published a lot of papers with their code. I wonder if this "error" had some implications with the results.
 
Physics news on Phys.org
Hi - I noticed you reposted this entire thread:

In general if you make a post and notice few replies, you should post a reply with any updates/additional information you have to encourage people to reply. It's also not uncommon to just post a reply like "bump - anyone have any ideas?", which will move your post back up to the top of the page.

Cheers!
 
Shouldn't the correct input values for theta range between 0 and pi? Hence -pi/2 isn't a valid input for theta.
 
Yes, this was also pointed out in the repost of this thread by Dr.Claude:

https://www.physicsforums.com/threads/matlab-spherical-harmonics.764112/
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
0
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K