# A function generating particle-in-a-box eigenfunctions

This function sequence is defined over $\left[-\frac{L}{2},\frac{L}{2}\right]$ (all elements are valued 0 outside this interval):

$$\psi_n(x)=\begin{cases}\sqrt{\frac2{L}}\sin{\left(\frac{n\pi x}{L}\right)} & n \text{ even} \\ \sqrt{\frac2{L}}\cos{\left(\frac{n\pi x}{L}\right)} & n \text{ odd}\end{cases}$$

I would like to create a Mathematica function that takes a natural number $N$ as a single parameter, and returns the corresponding function definition from the sequence. Looks like it boils down to substituting $n=N$ and substituting $\sin\oplus\cos=\begin{cases}\sin & N \text{ even} \\ \cos & N \text{ odd}\end{cases}.$

How can I achieve this?

• Could you please post the Mathematica code you've written so far? Dec 24, 2013 at 20:44

Do you mean this?

I like purely algebraic forms;

g[n_, L_, x_] :=
(1 + (-1)^n) Sqrt[2/L] Sin[n Pi x/L]/2 +
(1 - (-1)^n) Sqrt[2/L] Cos[n Pi x/L]/2


But Piecewise will work too:

f[n_, L_, x_] := Piecewise[{
{Sqrt[2/L] Sin[n Pi x/L], EvenQ[n]},
{Sqrt[2/L] Cos[n Pi x/L], OddQ[n]}
}]

• +1 for the un-traditional way of using Piecewise[] Dec 24, 2013 at 21:32
• So it need not be piecewise after all.. Beautiful. Thank you! Dec 24, 2013 at 21:42

You can exploit Mod[] + trigonometric identities for a very compact expression:

ψ[n_Integer?NonNegative, x_, L_: 1] :=
Sqrt[2/L] Sin[n π x/L + π Mod[n, 2]/2]

• …and you can use UnitBox[] to impose the condition of being zero outside the desired interval. Feb 28, 2016 at 2:26