Spherical harmonics arise in the solution to Laplace's equation and are used to represent functions defined on the surface of a sphere. Use
legendre
to compute and visualize the spherical harmonic for
.
The equation for spherical harmonics includes a term for the Legendre function, as well as a complex exponential:
First, create a grid of values to represent all combinations of
(colatitude angle) and
(azimuthal angle). Here, the colatitude
ranges from 0 at the North Pole, to
at the Equator, and to
at the South Pole.
Calculate
on the grid for
.
Since
legendre
computes the answer for all values of
,
Plm
contains some extra function values. Extract the values for
and discard the rest. Use the
reshape
function to orient the results as a matrix with the same size as
phi
and
theta
.
Calculate the spherical harmonic values for
.
Convert the spherical coordinates to Cartesian coordinates. Here,
becomes the latitude angle that ranges from
at the North Pole, to 0 at the Equator, and to
at the South Pole. Plot the spherical harmonic for
using both the positive and negative real values.