Skip to content

Commit

Permalink
'cetered difference'
Browse files Browse the repository at this point in the history
  • Loading branch information
DPotoyan authored Oct 10, 2024
1 parent d8e651d commit cd3705f
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions math/note05.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
"\n",
"#### Example: First Derivative as a Matrix\n",
"\n",
"Suppose we discretize a function $\\psi(x)$ over a set of points $\\{x_1, x_2, \\dots, x_n\\}$ with spacing $h$. We can approximate the first derivative using the finite difference formula:\n",
"- Suppose we discretize a function $\\psi(x)$ over a set of points $\\{x_1, x_2, \\dots, x_n\\}$ with spacing $h$. We can approximate the first derivative using the **finite difference**:\n",
"\n",
"$$ \\frac{d\\psi}{dx} \\approx \\frac{\\psi(x_{i+1}) - \\psi(x_i)}{h} $$\n",
"\n",
"This operation can be represented by a matrix. For $n = 5$ grid points, the first derivative matrix $D_1$ looks like:\n",
"- This operation can be represented by a matrix. For $n = 5$ grid points, the first derivative matrix $D_1$ in finite difference apporxmation looks like:\n",
"\n",
"$$\n",
"D_1 = \\frac{1}{h}\n",
Expand All @@ -66,7 +66,7 @@
"\\end{pmatrix}\n",
"$$\n",
"\n",
"For a function represented as a vector $\\psi = (\\psi(x_1), \\psi(x_2), \\dots, \\psi(x_5))$, applying this matrix gives the derivative at each point (except the boundary)."
"- For a function represented as a vector $\\psi = (\\psi(x_1), \\psi(x_2), \\dots, \\psi(x_5))$, applying this matrix gives the derivative at each point (except the boundary)."
]
},
{
Expand Down Expand Up @@ -113,7 +113,7 @@
"\\end{pmatrix}\n",
"$$\n",
"\n",
"This approximates the first derivative of \\( \\psi(x) = x^2 \\) at each of the sampled points:\n",
"This approximates the first derivative of $\\psi(x) = x^2$ at each of the sampled points:\n",
"\n",
"- At $x = 1$, the derivative is approximately 1 (exact value: 2).\n",
"- At $x = 2$, the derivative is approximately 3 (exact value: 4).\n",
Expand Down Expand Up @@ -150,7 +150,57 @@
"# Apply the first derivative matrix to the function vector\n",
"derivative_psi = D_1 @ psi\n",
"\n",
"print('derivative of x^2', derivative_psi) "
"print('derivative of x^2 using finite difference matrix', derivative_psi) \n",
"\n",
"# Exact derivative of psi = x^2 is 2*x\n",
"exact_derivative_psi = 2 * x\n",
"print('\\nExact derivative of x^2:\\n', exact_derivative_psi)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Centered Difference approximation\n",
"\n",
"- A much better approximation is obtained by using centered difference. \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} $$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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",
"psi = x**2 # Function psi(x) = x^2\n",
"\n",
"# Define the centered difference approximation for the first derivative matrix\n",
"D_1_centered = (1/(2*h)) * np.array([\n",
" [-3, 4, -1, 0, 0], # One-sided difference for the first point (improved boundary handling)\n",
" [-1, 0, 1, 0, 0], # Centered difference for interior points\n",
" [0, -1, 0, 1, 0],\n",
" [0, 0, -1, 0, 1],\n",
" [0, 0, 1, -4, 3] # One-sided difference for the last point (improved boundary handling)\n",
"])\n",
"\n",
"# Apply the centered difference matrix to the function vector\n",
"derivative_psi_centered = D_1_centered @ psi\n",
"\n",
"# Print the results\n",
"print('First derivative using centered difference approximation:\\n', derivative_psi_centered)\n",
"\n",
"# Exact derivative of psi = x^2 is 2*x\n",
"exact_derivative_psi = 2 * x\n",
"print('\\nExact derivative of x^2:\\n', exact_derivative_psi)"
]
},
{
Expand Down Expand Up @@ -242,7 +292,7 @@
"# Calculate the second derivative of x^2 using the centered difference method\n",
"second_derivative_centered = D_2_centered @ psi\n",
"\n",
"print('second derivative of x^2 by using centered difference \\n', D_1 @ D_1@x)"
"print('second derivative of x^2 by using centered difference \\n', second_derivative_centered)"
]
},
{
Expand Down

0 comments on commit cd3705f

Please sign in to comment.