File Exchange

image thumbnail

Spherical Harmonics

version 2.0.1 (240 KB) by Javier Montalt Tordera
Compute spherical harmonic functions.


Updated 28 Apr 2020

From GitHub

View Version History

View license on GitHub

This contribution includes a single MATLAB function ('harmonicY') that computes spherical harmonics of any degree and order, evaluated at arbitrary inclination, azimuth and radius. Capabilities include the computation of surface/solid, complex/real and normalized/unnormalized spherical harmonics.

Documentation is provided in the form of a live script with examples, as well as an HTML page for convenience and/or compatibility.

Cite As

Javier Montalt Tordera (2021). Spherical Harmonics (, GitHub. Retrieved .

Comments and Ratings (10)

Ivan Powis

The real SH returned by harmonicY appear to omit the factors of sqrt(2) that appear in the formulae for transforming complex to real harmonics (for m =/= 0) (eg for convenience the Wiki page This seems to originate in the different normalization preceding the Legendre and sine/cosine/exponential terms in the definition of the real and complex functions, that is the sqrt[(2l+1)(l-m)!/((l+m)! nPi)] where n is 4 in the complex harmonics, but 2 in the real ones in various sources I have consulted.

Jonathan Wolf

Hi Javier, thank you for the code and especially for pointing out phase and normalization conventions (which are crucial but hardly anyone mentions them explicitly)!

Geneva Porter

Hi @Thamid Opi, thank you for pointing this out. Negative m, or sine type, harmonics could be previously obtained using the option 'type', but I believe it was a confusing design. I have uploaded a new version which should work as you expect. Just remember to set the option 'type' to 'real'. I also updated the example in doc.mlx accordingly. I hope this will help.

Thamid Opi

This is great but was curious if it would be possible to plot negative order? When you input a negative value for m, it just outputs the positive m plot.

Arjun Yennemadi

Thank you! Your program helped me a lot with my project.

pengxin chen

Good job!

Hi Yunus, thank you for your message. I am not sure if I understood you correctly but I presume you were trying to calculate spherical harmonics of large degree and/or order. Unfortunately this function was not designed with such applications in mind, but I updated it and it should now be able to handle those numbers. Naturally, this will only work in the normalized mode, as unnormalized spherical harmonics overflow the double-precision range for n > 150. Please do let me know if this helps!

Yunus Emre SARI

Hi Javier

Thank you for your work. For EGM2008 Nmax = 2196 does not receive factorial expressions I have the same problem in the code I wrote what can we do in it valid?

Andre Paixao

MATLAB Release Compatibility
Created with R2018a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!