{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# p08: Eigenvalues of harmonic oscillator \n", "\n", "$$\n", "-u'' + x^2 u = \\lambda u, \\qquad x \\in \\mathbb{R}\n", "$$\n", "\n", "The exact eigenvalues are $1,2,5,7,\\ldots$." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%config InlineBackend.figure_format='svg'\n", "from numpy import pi,arange,linspace,sin,zeros,diag,sort\n", "from scipy.linalg import toeplitz\n", "from numpy.linalg import eig" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-------- N = 6 --------\n", " 0.46147291699547\n", " 7.49413462105052\n", " 7.72091605300656\n", " 28.83248377834012\n", "-------- N = 12 --------\n", " 0.97813728129861\n", " 3.17160532064719\n", " 4.45593529116679\n", " 8.92452905811993\n", "-------- N = 18 --------\n", " 0.99997000149931\n", " 3.00064406679583\n", " 4.99259532440772\n", " 7.03957189798150\n", "-------- N = 24 --------\n", " 0.99999999762904\n", " 3.00000009841086\n", " 4.99999796527328\n", " 7.00002499815654\n", "-------- N = 30 --------\n", " 0.99999999999998\n", " 3.00000000000074\n", " 4.99999999997560\n", " 7.00000000050862\n", "-------- N = 36 --------\n", " 0.99999999999999\n", " 3.00000000000000\n", " 5.00000000000000\n", " 6.99999999999999\n" ] } ], "source": [ "L = 8.0\n", "for N in range(6,37,6):\n", " h = 2.0*pi/N; x = h*linspace(1,N,N); x = L*(x-pi)/pi\n", " col = zeros(N)\n", " col[0] = -pi**2/(3.0*h**2) - 1.0/6.0\n", " col[1:] = -0.5*(-1.0)**arange(1,N)/sin(0.5*h*arange(1,N))**2\n", " D2 = (pi/L)**2 * toeplitz(col)\n", " evals,evecs = eig(-D2 + diag(x**2))\n", " eigenvalues = sort(evals)\n", " print(\"-------- N = %d --------\" % N)\n", " for e in eigenvalues[0:4]:\n", " print(\"%20.14f\" % e)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.8" } }, "nbformat": 4, "nbformat_minor": 4 }