# The quantum harmonic oscillator


\begin{align}
H \psi_n = E_n \psi_n
\end{align}

where the Hamiltontial $H = T + V$ is defined as

\begin{align}
H &= \frac{-\hbar^2}{2m}\frac{\partial^2}{\partial x^2} + \frac{1}{2} k x^2
\end{align}

The wavefunction $\psi_n$ can be shown to be:

\begin{align}
\psi_n &= N_n H_n \left(\frac{x}{\alpha}\right) e^{- \frac{x^2}{2 \alpha^2}}
\end{align}
with 

\begin{align}
\alpha &= \left( \frac{\hbar^2}{m k} \right) \\
N_n &= \frac{1}{\sqrt{\alpha \pi^{1/2} 2^n  n!}}
\end{align}

and the energy $E_n = \hbar \omega \left(n + \frac{1}{2}\right)$ where $\omega = \sqrt{\frac{k}{m}}$ and $n$ is a non-negative integer number.

$H_n$ is a Hermite polynomail, you'll learn more on them in the following exercise

## Exercise 1

The first two hermite polynomials are given by 

\begin{align}
H_0(x) &= 1 \\
H_1(x) &= 2x
\end{align}

The rest of the Hermite polynomials can be found using the recursion relation 
\begin{align}
H_{n+1}(x) = 2xH_n(x) - 2 n H_{n-1}(x)
\end{align}

Eg. to find $H_{2}$ insert $n=1$ in the formula above and you get

\begin{align}
    H_2(x) = 2xH_1(x) - 2 H_{0}(x) = 4x^2 - 2
\end{align}

Make a function that generates the Hermite Polynomials. Using the recursion relation shown above.


To do this exercise do you need to make a recursive function, a recursive function is a function that calls itself untill a certain stop condition has been met. To read more about them check out https://www.programiz.com/python-programming/recursion or the internet. 


To help you get started on this exercise use the following as a blue print.

```Python
# To get started use the following as a blueprint
def hermite_polynomial(n, x):
    if n == 0:
        return 1 
    if n == 1:
        return 2*x
    else:
        return # Fill this in with the recursion relation
    
# Using sympy you can print out the result and compare it to the analytical solution
# that can be found on ExplainEverything WEXLRXLP

import sympy as sp

x = sp.symbols('x')
hermite_polynomial(2, x)
```

 ## Exercise 2
 
 Using the hermite polynomials function you made above, define the wavefunction for the Harmonic oscillator. 
 
 Check if the wavefunction is ortonormal, i.e., 
 
 \begin{align}
 \int_{-\infty}^\infty |\psi_n(x)|^2 dx &= 1 \\
 \int_{-\infty}^\infty \psi_n^*(x)\psi_m(x) dx &= 0
 \end{align}
 
 Hint: I recommend that sympy is used to complete the rest of the exercises
 
 Hint 2: To integrate from $\int_{-\infty}^\infty f(x) dx $ with sympy
           `sp.integrate(f(x), (x, -sp.oo, sp.oo))`
           

```Python
def wavefunction(n, m, k, x):
    """ n : the wave number
        m : Mass of the partice
        k : Spring costant
        x : position of the particle
    """
    return
```

## Exercise 3

Step 1:

Plot the potential $V(x) = \frac{1}{2} k x^2$ 

Step 2:

Plot the potential $V(x)$ together with some energy levels of $E_n = \hbar \omega \left(n + \frac{1}{2}\right)$ 

Step 3:

Plot the potential $V(x)$ together with $|\psi_n|^2$ for a few values of $n$. What happens when $n$ becomes large?

## Exercise 4

In the lecture on the [virial theorem] expectation values

\begin{align}
\langle x \rangle &= 0 \\
\langle x^2 \rangle &= \hbar \omega (n + 0.5)/k \\
\langle p \rangle &= 0 \\
\langle p^2 \rangle &= m \hbar \omega (n + 0.5)
\end{align}

using physical intuition and the Virial theorem. Here I want you to make
functions that calculates these by brute force.

That is,
\begin{align}
\langle x \rangle &= \langle \psi_n|x|\psi_n\rangle \\
\langle x^2 \rangle &= \langle \psi_n|x^2|\psi_n\rangle \\
\langle p \rangle &= \langle \psi_n|p|\psi_n\rangle \\
\langle p^2 \rangle &= \langle \psi_n|p^2|\psi_n\rangle \\
\end{align}

Hint: $\langle \psi_n |a|\psi_n\rangle = \int_{-\infty}^{\infty} \psi_n^*(x)a\psi_n(x) dx$

Hint 2: To differentiate twice in sympy use 
```Python 
sp.diff(f(x), x, 2)```

[virial theorem]: https://expl.ai/ZLHXGRL

```Python
def expectation_x(n, m, k):
    
    return 

def expectation_x_squared(n, m, k):
    
    return

def expectation_p(n, m, k):
    
    return

def expecation_p_squared(n, m, k):
    
    return
```


```python

```

c6b2f2f041447aa97099fc46c55798e7c53bd39e
ipynb
Jupyter Notebook
harmonic_oscillator.ipynb
KJE2001/seminars
b42c5c0a945795f6f625db35cafefa6662e5ce7e
MIT
2017-02-04T01:34:33.000Z
2021-06-12T12:27:37.000Z
harmonic_oscillator.ipynb
KJE2001/seminars
b42c5c0a945795f6f625db35cafefa6662e5ce7e
MIT
2020-03-30T11:00:35.000Z
2020-05-12T05:42:24.000Z
harmonic_oscillator.ipynb
KJE2001/seminars
b42c5c0a945795f6f625db35cafefa6662e5ce7e
MIT
2016-04-26T20:42:43.000Z
2022-02-06T11:12:57.000Z
Qwen/Qwen-72B
1. YES
2. YES