Is MATLAB's Implementation of Spherical Harmonics Incorrect?

In summary, the conversation is about a user who created a function to calculate spherical harmonics in MATLAB, but recently discovered that it was incorrect. They found that other implementations of the function in MATLAB were also incorrect. The correct way to implement the function would require the use of a symbolic language like Mathematica. The user is seeking tips on how to correct the function and wondering if this error has affected the results of others who have used MATLAB to compute spherical harmonics.
  • #1
t14g0
1
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
  • #2
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!
 
  • #3
Shouldn't the correct input values for theta range between 0 and pi? Hence -pi/2 isn't a valid input for theta.
 
  • #4
Yes, this was also pointed out in the repost of this thread by Dr.Claude:

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

1. What are Spherical Harmonics?

Spherical Harmonics are a set of mathematical functions used to describe the spherical symmetry of a three-dimensional object. They are commonly used in physics, mathematics, and engineering to represent the shape and orientation of objects or fields in spherical coordinates.

2. How are Spherical Harmonics used in MATLAB?

MATLAB has built-in functions for calculating and manipulating Spherical Harmonics, such as sphharm and sph2cart. These functions allow users to easily generate plots, perform calculations, and solve problems involving spherical objects or fields.

3. What are the applications of Spherical Harmonics?

Spherical Harmonics are used in a variety of fields, including quantum mechanics, electromagnetics, geophysics, and computer graphics. They are particularly useful for solving problems involving spherical objects or fields, such as the motion of planets, the shape of molecules, or the radiation patterns of antennas.

4. Can Spherical Harmonics be used for non-spherical objects?

Yes, Spherical Harmonics can be used to describe the symmetry of non-spherical objects as well. This is because the functions are defined in terms of the spherical coordinates, which can be applied to any shape or orientation.

5. Are there any limitations to using Spherical Harmonics in MATLAB?

While Spherical Harmonics are a powerful tool for solving problems involving spherical symmetry, they may not be the best choice for more complex or irregular shapes. In these cases, other mathematical techniques, such as Fourier series or wavelets, may be more suitable.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
572
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
137
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top