# Help with Real Spherical Harmonics

I am trying to code a 3D representation of the position wavefunction probability of the Hydrogen atom.

I am having issues with the angular terms of the equation, which is represented as the real part of a spherical harmonic function.

The equation is:

And the Associated Legendre function is:

I do the corresponding derivative (d/d_theta of cos(theta) of the function) for modes l=0 to l=3 through these tabulated equations:

The final term: e^(i * m * phi) I represent with: sin(|m| * phi) if m < 0 and cos(m * phi) if m > 0.

After converting to cartesian coordinates, I end up with a 3D visual that looks like this:

The x-z axis is actually quite good, as it matches what is expected from 2D cross-sections I found online:

However, there is a discontinuity in the y-axis which should not be there.

By the way, theta ranges from 0-2pi and phi ranges from 0-pi. r is a constant in this case.

Is there anywhere in the math which I did incorrectly?

• The representation of $e^{im\varphi}$ looks not very natural, because $e^{im\varphi}=\cos (m\varphi)+i\sin (m\varphi)$. Commented Sep 5 at 6:15
Representation of the real part of spherical harmonics seems strange only for $$m<0$$. Both $$m$$ and $$\phi$$ are real, so $$\mathrm{Re}(e^{im\phi}) = \cos(m\phi)$$. Though, you used $$m=1$$ in your visual, so this couldn't have caused the problem.
The problem arises from your conventions for $$\theta$$ and $$\phi$$. As stated in the question, you took $$\theta$$ as polar angle (ranging from 0 to $$2\pi$$) and $$\phi$$ as azimuthal angle (ranging from 0 to $$\pi$$). In physics, the convention is: $$\phi$$ for polar angle and $$\theta$$ for azimuthal angle. This convention is also used in the equations (see Griffiths "Introduction to Quantum Mechanics" for comparison). Changing to the correct variables should eliminate the discontinuity.