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].
Daniel Ennis (2019). Spherical Harmonics (https://www.mathworks.com/matlabcentral/fileexchange/8638-spherical-harmonics), MATLAB Central File Exchange. Retrieved .
Create scripts with code, output, and formatted text in a single executable document.
Howard Lam (view profile)
Does not handle negative order.
Guillermo Aparicio (view profile)
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.
Guillermo Aparicio (view profile)
Andrew Shaw (view profile)
Mark Thompson (view profile)
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!
Adriana Cisco (view profile)
Works well other than the error at line 92 unknown function setfig. Thanks!
Giorgos (view profile)
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 (view profile)
Are there any function or example to generate cylindrical harmonics or normal modes of a cylinder??
Rush (view profile)
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.
Etai Rosenkrantz (view profile)
Jacek (view profile)
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.
Nice start point. Note that the function is mainly for ploting Ylm's, since the output is a matrix of different angles (theta,phi).
sir,
i am final year M.E. student. i want the matlab coding for harmonic elimination in PWM AC chopper using genetic algorithm.
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.
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.
There's something wrong with the dimension of Ymn.
"[THETA,PHI]=meshgrid(THETA,PHI);"
should be "[THETA,PHI]=ndgrid(THETA,PHI);" (line 53)
This is a very good starting point for me and my work - thanx ;-)
Nice.