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?

  • $\begingroup$ Could you please post the Mathematica code you've written so far? $\endgroup$ Dec 24 '13 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]}
  • 3
    $\begingroup$ +1 for the un-traditional way of using Piecewise[] $\endgroup$ Dec 24 '13 at 21:32
  • 1
    $\begingroup$ So it need not be piecewise after all.. Beautiful. Thank you! $\endgroup$
    – user76568
    Dec 24 '13 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]
  • $\begingroup$ …and you can use UnitBox[] to impose the condition of being zero outside the desired interval. $\endgroup$ Feb 28 '16 at 2:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.