This function generates the Spherical Harmonics basis functions of degree L and order M.
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)
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 .
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.
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!
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.
Are there any function or example to generate cylindrical harmonics or normal modes of a cylinder??
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.
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).
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
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.
should be "[THETA,PHI]=ndgrid(THETA,PHI);" (line 53)
This is a very good starting point for me and my work - thanx ;-)