# Best way to draw a quantum harmonic oscillator?

I'm writing up my notes from my photonics lectures, in LaTeX. Both so I can make sense of the notes, and so I can learn some LaTeX. To be frank doing it in LaTeX is quite a motivating reason to go through them!

The issue is, I need draw a quantum harmonics oscillator that appears like so:

This is what I have so far:

I'd like more control over the parabola so I can eventually make it larger and change the aspect ratio I've gotten where I am by guess work. As well I'd like to draw the wavefunctions and put them outside of the parabola with arrows (i.e. pins) but I'm not sure what the best way to draw the wave functions is. How can I draw them? They only need to be sin/cos functions.

Here is what I have so far:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-0.5,-2) -- (-0.5,0.5);
\draw (-2.69,-0.5) -- (2.5,-0.5) node[right] {E2};
\draw (-2.25,-1) -- (2.5,-1) node[right] {E1};
\draw (-1.75,-1.5) -- (2.5,-1.5) node[right] {E0};
\draw (-3,0) parabola[parabola height=-2cm] (2,0);
\end{tikzpicture}
\end{document}


I'll warn you I have tried drawing from functions (brought in through gnuplot), and it's never seemed to work, so if that's your answer be prepared for a thorough explanation!

# Edit the First

I now have this code

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (0,0) -- (0,4.5);

\def\lzero{(3,0.4) cos (4,0.6) sin (5,0.8) cos (6,0.6) sin (7,0.4)};
\fill[blue,opacity=0.2] \lzero;
\draw[blue!75!black] \lzero node[right,black] {$\Psi_0(x)$ \textendash ~Even};
\draw (-0.632,0.4) -- (2,0.4) node[right] {$E_0$};

\def\lone{(3,1.5) cos (3.66,1.4) sin (4.33,1.3) cos (5,1.5) sin (5.66,1.7) cos (6.33,1.6) sin (7,1.5)};
\fill[red,opacity=0.2] \lone;
\draw[red!75!black] \lone node[right,black] {$\Psi_1(x)$ \textendash ~Odd};
\draw (-1.224,1.5) -- (2,1.5) node[right] {$E_1$};

\def\ltwo{(3,2.6) cos (3.5,2.7) sin (4,2.8) cos (4.5,2.6) sin (5,2.4) cos (5.5,2.6) sin (6,2.8) cos (6.5,2.7) sin (7,2.6)};
\fill[green,opacity=0.2] \ltwo;
\draw[green!75!black] \ltwo node[right,black] {$\Psi_2(x)$ \textendash ~Even};
\draw (-1.61,2.6) -- (2,2.6) node[right] {$E_2$};

\draw (0,0) parabola (2,4);
\draw (0,0) parabola (-2,4);
\end{tikzpicture}
\end{document}


Which produces this:

Now I understand how to draw parabolas better, and how to make the wave functions, I'm much happier.

-
Perhaps it would be a good idea also to post the mathematical expressions for the wave-functions $\psi_0$,...,$\psi_7$ you want. –  student Feb 12 '12 at 10:12
I'm not too fussed by the wave function values. The main point of what I'm doing is showing sin and cos waves. –  Pureferret Feb 12 '12 at 12:00
Is your main point of interest the plotting of functions? Or the filling style? –  Jake Feb 12 '12 at 12:01
The plotting the functions, and having more control over the parabola (not 100% on how they should be drawn) –  Pureferret Feb 12 '12 at 12:25
Honestly, I would not use sine and cosine to imitate the eigen functions of the harmonic oscillator. If you take the proper functions as done by Jake and Herbert, you'd have learned the correct form of the eigen functions and how to plot these more complex formulas with latex. Since pgfplots is a dedicated plotting library, I also would use it for that job as Jake did instead of doing it with pure tikz. –  quinmars Feb 12 '12 at 15:25

You could approximate the wave functions as sin and cos parts:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.pathreplacing}

\begin{document}

\begin{tikzpicture}
\def\lzero{(-5,1) cos (-2.5,1.5) sin (0,2) cos (2.5,1.5) sin (5,1)};
\fill[blue,opacity=0.2] \lzero;
\draw[blue!75!black] \lzero node[right,black] {$\Psi_0(x)$};

\def\lone{(-5,2.5) cos (-3.33,2.25) sin (-1.66,2) cos (0,2.5) sin (1.66,3) cos (3.33,2.75) sin (5,2.5)}
\fill[red,opacity=0.2] \lone;
\draw[red!75!black] \lone node[right,black] {$\Psi_1(x)$};

\def\ltwo{(-5,4) cos (-3.75,4.25) sin (-2.5,4.5) cos (-1.25,4) sin (0,3.5) cos (1.25,4) sin (2.5,4.5) cos (3.75,4.25) sin (5,4)}
\fill[green,opacity=0.2] \ltwo;
\draw[green!75!black] \ltwo node[right,black] {$\Psi_2(x)$};

\draw (0,0) parabola (5,10);
\draw (0,0) parabola (-5,10);

\begin{scope}[decoration={border, segment length=1cm, amplitude=1mm, angle=90}]
\draw[postaction={decorate,draw}] (-5,0) -- (5.1,0);
\draw[postaction={decorate,draw}] (0,0) -- (0,10.1);
\end{scope}

\end{tikzpicture}

\end{document}


The problem are the ''fade in'' and ''fade out'' parts on the left and right. I tried to mimic this by dividing the width in equal parts, but letting the amplitude rise slower on the edges. It would probably be better if you could provide a formula for computing the values of the wave functions, then one could use more points to make it look better. Also, the amount of cos and sin parts rises with every level, so giving a computation formula would greatly enhance the ability to extend this to higher levels.

-
Also, for my purposes I don't need the wave function (sin and cos) part to be accurate, as I only want to show the energy level and the sort of wave function. I don't understand how you've drawn the cos and sin parts. Other wise I can more or less work out what is happening. –  Pureferret Feb 12 '12 at 12:12
I've managed to get the image I want by following your answer so I've accepted it as correct. –  Pureferret Feb 12 '12 at 13:42

For plotting functions, I find pgfplots to be much more comfortable than using the "raw" TikZ plotting capabilities. Here's a plot of the first four wave functions, as described in http://galileo.phys.virginia.edu/classes/751.mf1i.fall02/SimpleHarmonicOscillator.htm

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[domain=-3.5:3.5, samples=50,no markers, hide axis,y=1cm,thick]
\end{axis}
\end{tikzpicture}

\end{document}

-

I have no idea what value you are using for Alpha, the reason why I set it to 0.5. You can modify it. The wave functions should be self explenatory.

Run the example with xelatex or the sequence latex->dvips->psp2df

\documentclass{article}
\parindent=0pt
\usepackage{pst-plot}
\begin{document}

\begin{pspicture}(-6,-1)(6,7)\psset{plotpoints=200}
\pstVerb{/Alpha 0.5 def } %the value for Alpha
\psplot[algebraic,fillstyle=solid,fillcolor=blue!15,linecolor=blue]{-6}{6}%
{(Alpha/Pi)^0.25*2.728^(-(Alpha*x^2)/2)}
\rput(0,1.5){%
\psplot[algebraic,fillstyle=solid,fillcolor=red!15,linecolor=red]{-6}{6}%
{(Alpha/Pi)^0.25*sqrt(2*Alpha)*x*2.728^(-(Alpha*x^2)/2)}}
\rput(0,3){%
\psplot[algebraic,fillstyle=solid,fillcolor=green!15,linecolor=green]{-6}{6}%
{(Alpha/Pi)^0.25*(2*Alpha*x^2-1)/sqrt(2)*2.728^(-(Alpha*x^2)/2)}}
\rput(0,4.5){%
\psplot[algebraic,fillstyle=solid,fillcolor=cyan!15,linecolor=cyan]{-6}{6}%
{(Alpha/Pi)^0.25*(2*(sqrt(Alpha)*x)^3-3*sqrt(Alpha)*x)/sqrt(3)*2.728^(-(Alpha*x^2)/2)}}
\psaxes[labels=none,ticksize=-4pt 0,
linecolor=black!40]{->}(0,0)(-5,-1)(5,7)[$x$,0][$\Psi$,0]
\end{pspicture}

\end{document}


-

With tkz-fct and gnuplot. Some remarks : I need to add something to avoid repeating the domain, then when several functions are used, you can reference them by a letter a,b,c etc. But it's primary. Perhaps I need to create somethings like \tkzDrawAreagf

\documentclass{article}
\usepackage{tkz-fct}
\begin{document}
\begin{tikzpicture}[xscale=.75,yscale=1.25]
\tkzInit[xmin=-8,xmax=8,ymin=0,ymax=70,ystep=10]
\tkzDrawXY

\tkzFct[domain = -8:8]{10}
\tkzFct[domain = -8:8]{10+5*sin(x)}
\tkzDrawAreafg[between=a and b,color  = purple!20,domain = -8:8]
\tkzDrawAreafg[between=b and a,color  = purple!20,domain = -8:8]

\tkzFct[domain = -8:8]{30}
\tkzFct[domain = -8:8]{30+5*sin(1.5*x)}
\tkzDrawAreafg[between=c and d,color  = orange!20,domain = -8:8]
\tkzDrawAreafg[between=d and c,color  = orange!20,domain = -8:8]

\tkzFct[domain = -8:8]{50}
\tkzFct[domain = -8:8]{50+5*sin(2*x)}
\tkzDrawAreafg[between=e and f,color  = blue!20,domain = -8:8]
\tkzDrawAreafg[between=f and e,color  = blue!20,domain = -8:8]

\tkzFct[domain = -8:8]{x*x}
\end{tikzpicture}
\end{document}


-