Skip to content

Commit

Permalink
'fin diff'
Browse files Browse the repository at this point in the history
  • Loading branch information
DPotoyan authored Oct 10, 2024
1 parent cd3705f commit 8acc636
Showing 1 changed file with 34 additions and 44 deletions.
78 changes: 34 additions & 44 deletions math/note05.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"\n",
"| Dirac notation for vectors | Dirac notation for functions |\n",
"| :----------------------------------------------------------: | :----------------------------------------------------------: |\n",
"| **Ket** $\\mid a \\rangle =(a_1,a_2,..)\\\\ $ **Bra** $\\bra{a} = \\begin{pmatrix}a_1 \\\\ a_2 \\\\ ...\\\\ \\end{pmatrix}$ | **Ket** $\\mid \\psi\\rangle=\\psi(x)\\\\$ $\\\\$ **Bra** $\\langle \\psi \\mid=\\psi(x)^*$ |\n",
"| **Ket** $\\mid a \\rangle =(a_1,a_2,..) \\\\ $ **Bra** $\\bra{a} = \\begin{pmatrix}a_1 \\\\ a_2 \\\\ ...\\\\ \\end{pmatrix}$ | **Ket** $\\mid \\psi\\rangle=\\psi(x)\\\\$ $\\\\$ **Bra** $\\langle \\psi \\mid=\\psi(x)^*$ |\n",
"| **Example** $\\\\ \\mid a \\rangle =(1, 2i)$ $\\\\\\bra{a} =\\begin{pmatrix} 1 \\\\ -2i \\\\ \\end{pmatrix}$ | **Example** $\\\\ \\mid \\psi \\rangle=e^{ix^2}$ $\\\\ \\langle \\psi \\mid = e^{-ix^2}$ |"
]
},
Expand Down Expand Up @@ -73,7 +73,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Numerical example of Applying the Derivative Matrix for $\\psi(x) = x^2$\n",
"#### Numerical example of Applying the Derivative Matrix for $\\psi(x) = x^2$\n",
"\n",
"Let’s consider the function $\\psi(x) = x^2$ sampled at 5 points $x = (0, 1, 2, 3, 4)$, giving us:\n",
"\n",
Expand Down Expand Up @@ -161,12 +161,25 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Centered Difference approximation\n",
"#### Centered Difference approximation\n",
"\n",
"- A much better approximation is obtained by using centered difference. \n",
"- A much better approximation is obtained by using centered difference (difference between one point to the left and one point to the right from a give point) \n",
"- And taking care of boundary points via higher order evaluation of derivatives. \n",
"\n",
"$$ \\frac{d\\psi}{dx} \\approx \\frac{\\psi(x_{i+1}) - \\psi(x_{i-1})}{2h} $$"
"$$ \\frac{d\\psi}{dx} \\approx \\frac{\\psi(x_{i+1}) - \\psi(x_{i-1})}{2h} $$\n",
"\n",
"This operation can be represented by a matrix. For $n = 5$ grid points, with improved boundary handling, the first derivative matrix $D_1$ using the centered difference method looks like:\n",
"\n",
"$$\n",
"D_1 = \\frac{1}{2h}\n",
"\\begin{pmatrix}\n",
"-3 & 4 & -1 & 0 & 0 \\\\\n",
"-1 & 0 & 1 & 0 & 0 \\\\\n",
"0 & -1 & 0 & 1 & 0 \\\\\n",
"0 & 0 & -1 & 0 & 1 \\\\\n",
"0 & 0 & 1 & -4 & 3\n",
"\\end{pmatrix}\n",
"$$\n"
]
},
{
Expand Down Expand Up @@ -207,59 +220,36 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Second Derivative as a Matrix\n",
"#### Second Derivative as a Matrix (Centered Difference)\n",
"\n",
"Similarly, the second derivative can be approximated by a central difference formula:\n",
"Similarly, the second derivative can be approximated by a **centered difference formula** for interior points:\n",
"\n",
"$$ \\frac{d^2\\psi}{dx^2} \\approx \\frac{\\psi(x_{i+1}) - 2\\psi(x_i) + \\psi(x_{i-1})}{h^2} $$\n",
"\n",
"The corresponding second derivative matrix $D_2$ for $n = 5$ grid points is:\n",
"At the **boundaries**, we use one-sided difference formulas to maintain accuracy:\n",
"\n",
"- At the first point $x_1$, we approximate using:\n",
"\n",
" $$ \\frac{d^2\\psi}{dx^2} \\bigg|_{x_1} \\approx \\frac{\\psi(x_1) - 2\\psi(x_2) + \\psi(x_3)}{h^2} $$\n",
"\n",
"- At the last point $x_n$, we approximate using:\n",
"\n",
" $$ \\frac{d^2\\psi}{dx^2} \\bigg|_{x_n} \\approx \\frac{\\psi(x_{n-2}) - 2\\psi(x_{n-1}) + \\psi(x_n)}{h^2} $$\n",
"\n",
"The corresponding second derivative matrix $D_2$ for $n = 5$ grid points, with this improved boundary handling, is:\n",
"\n",
"$$\n",
"D_2 = \\frac{1}{h^2}\n",
"\\begin{pmatrix}\n",
"-2 & 1 & 0 & 0 & 0 \\\\\n",
"1 & -2 & 1 & 0 & 0 \\\\\n",
"1 & -2 & 1 & 0 & 0 \\\\\n",
"0 & 1 & -2 & 1 & 0 \\\\\n",
"0 & 0 & 1 & -2 & 1 \\\\\n",
"0 & 0 & 0 & 1 & -2\n",
"0 & 0 & 1 & -2 & 1\n",
"\\end{pmatrix}\n",
"$$\n",
"\n",
"Applying this matrix to the vector $\\psi$ gives the second derivative of the function at each point."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"# Define the grid points and function\n",
"n = 5\n",
"x = np.linspace(0, 4, n) # Grid points from 0 to 4\n",
"h = x[1] - x[0] # Spacing between points\n",
"\n",
"psi = x**2 # Function psi(x) = x^2\n",
"\n",
"# Define the first derivative matrix D_1\n",
"D_1 = (1/h) * np.array([\n",
" [-1, 1, 0, 0, 0],\n",
" [0, -1, 1, 0, 0],\n",
" [0, 0, -1, 1, 0],\n",
" [0, 0, 0, -1, 1],\n",
" [0, 0, 0, 0, 0]\n",
"])\n",
"\n",
"print('second derivative matrix \\n', D_1 @ D_1)\n",
"\n",
"print('second derivative of x^2 \\n', D_1 @ D_1@x)"
"Applying this matrix to the vector $\\psi$ gives the second derivative of the function at each point, including the boundary points with improved accuracy."
]
},
{
Expand Down

0 comments on commit 8acc636

Please sign in to comment.