File Exchange

image thumbnail

Spherical Harmonics

version 1.0.0.0 (2.17 KB) by Daniel Ennis
This function generates the Spherical Harmonics basis functions of degree L and order M.

10 Downloads

Updated 05 Oct 2005

View License

This function generates the Spherical Harmonics basis functions of degree L and order M.

SYNTAX: [Ymn,THETA,PHI,X,Y,Z]=spharm4(L,M,RES,PLOT_FLAG);

INPUTS:

L - Spherical harmonic degree, [1x1]
M - Spherical harmonic order, [1x1]
RES - Vector of # of points to use [#Theta x #Phi points],[1x2] or [2x1]
PLOT_FLAG - Binary flag to generates a figure of the spherical harmonic surfaces (DEFAULT=1)

OUTPUTS:

Ymn - Spherical harmonics coordinates, [RES(1) x RES(2)]
THETA - Circumferential coordinates, [RES(1) x RES(2)]
PHI - Latitudinal coordinates, [RES(1) x RES(2)]
X,Y,Z - Cartesian coordinates of magnitude, squared, spherical harmonic surface points, [RES(1) x RES(2)]

NOTE: It is very important to keep the various usages of THETA and PHI straight. For this function THETA is the Azimuthal/Longitude/Circumferential coordinate and is defined on the interval [0,2*pi], whereas PHI is the Altitude/Latitude/Elevation and is defined on the interval [0,pi]. Also note that the conversion to cartesian coordinates requires that PHI be offset by pi/2 so that the conversion is on the interval [-pi/2,pi/2].

Cite As

Daniel Ennis (2019). Spherical Harmonics (https://www.mathworks.com/matlabcentral/fileexchange/8638-spherical-harmonics), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (24)

Howard Lam

Does not handle negative order.

Mark Thompson, you need to add

[Xr,Yr,Zr]=sph2cart(THETA,PHI + pi/2,real(Ymn));

in addition to [Xr,Yr,Zr]=sph2cart(THETA,PHI-pi/2,real(Ymn));

I've added phi+pi/2 and removed the power 2, then the harmonics are the formal ones. It was a plotting fact, mathematically there's no square power.

Andrew Shaw

Can someone please explain to me why the absolute, real and imaginary Ymn values are first squared (^2) prior to plotting? Is this just useful for plotting, or is this somehow related to the mathematical formulation for Ymn? Thanks!

Works well other than the error at line 92 unknown function setfig. Thanks!

Giorgos

Nice function.
An interesting improvement would be to use the Schmidt semi-normalised Legendre functions as to avoid the use of factorials, which would make the output much more accurate for high L values.

seda

Are there any function or example to generate cylindrical harmonics or normal modes of a cylinder??

Rush

just one question, let's say i want to find spherical harmonics with m=-1 and l=1, it gives me error statements. im sure it has to do with the modulus sign. if so, why do we put the modulus sign? thanx.

Jacek

Nice work .

One minor comment: the output argument Ymn has the elevation and azimuth dimensions flipped with respect to the input RES argument, due to the nature of meshgrid. Thus, the rows of the output Ymn refer to elevation, while the columns of the output Ymn refer to azimuth.

Jon Casey

Seo Lee

Nice start point. Note that the function is mainly for ploting Ylm's, since the output is a matrix of different angles (theta,phi).

bapitha anbalagan

sir,
i am final year M.E. student. i want the matlab coding for harmonic elimination in PWM AC chopper using genetic algorithm.

Anamika Gupto

Miguel Neto

B L

Great, math is correct, but Matlab 7 doesn't know what setfig is:
??? Undefined command/function 'setfig'.

Error in ==> spharm4 at 92
setfig(gcf,10,5,12);

Easy to tweak for my needs.

GOUTAM BISWAS

Tamar Kashti

Nice start point. Note that the function is mainly for ploting Ylm's, since the output is a matrix of different angles (theta,phi). The opposite convation of (theta,phi) is confusing.

Ting HUANG

There's something wrong with the dimension of Ymn.
"[THETA,PHI]=meshgrid(THETA,PHI);"
should be "[THETA,PHI]=ndgrid(THETA,PHI);" (line 53)

Ritter Dieter

Oliver Müller

This is a very good starting point for me and my work - thanx ;-)

GOUTAM BISWAS

Michael Kleder

Nice.

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