{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Finite Differences for Boundary Value Problems\n",
"\n",
"Copyright (C) 2010-2020 Luke Olson
\n",
"Copyright (C) 2020 Andreas Kloeckner\n",
"\n",
"\n",
"MIT License
\n",
"Permission is hereby granted, free of charge, to any person obtaining a copy\n",
"of this software and associated documentation files (the \"Software\"), to deal\n",
"in the Software without restriction, including without limitation the rights\n",
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n",
"copies of the Software, and to permit persons to whom the Software is\n",
"furnished to do so, subject to the following conditions:\n",
"\n",
"The above copyright notice and this permission notice shall be included in\n",
"all copies or substantial portions of the Software.\n",
"\n",
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n",
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n",
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n",
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n",
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n",
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n",
"THE SOFTWARE.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as pt\n",
"\n",
"import scipy.sparse as sps"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We'll solve\n",
"\n",
"$u''+1000(1+x^2)u=0$ on $(-1,1)$\n",
"\n",
"with $u(-1)=3$ and $u(1)=-3$."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"#n = 9\n",
"n = 200\n",
"\n",
"mesh = np.linspace(-1, 1, n)\n",
"h = mesh[1] - mesh[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use `sps.diags(values, offsets=..., shape=(n, n))` to make a centered difference matrix."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create `second_deriv` as a matrix to apply the second derivative. Can only do that for the interior points!\n",
"\n",
"* change `shape` and offsets\n",
"* Take `h` into account"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"second_deriv = sps.diags(\n",
" [1,-2,1],\n",
" offsets=np.array([-1,0,1])+1,\n",
" shape=(n-2, n))/h**2\n",
"\n",
"\n",
"if n < 10:\n",
" print(second_deriv.todense())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make a matrix for the lower-order term."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"factor = sps.diags(\n",
" [1000*(1 + mesh[1:]**2)],\n",
" offsets=[1],\n",
" shape=(n-2, n))\n",
"\n",
"if n < 10:\n",
" print(mesh[1:-1])\n",
" print()\n",
" print(factor.todense())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Build the matrix for the interior:"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"A_int = second_deriv+factor\n",
"\n",
"if n < 10:\n",
" print(A_int.todense())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Glue on the rows for the boundary conditions:"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"A = sps.vstack([\n",
" sps.coo_matrix(([1], ([0],[0])), shape=(1, n)),\n",
" A_int,\n",
" sps.coo_matrix(([1], ([0],[n-1])), shape=(1, n)),\n",
" ])\n",
"A = sps.csr_matrix(A)\n",
"\n",
"if n < 10:\n",
" print(A.todense())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, assemble the right-hand side as `rhs`:\n",
"\n",
"Pay special attention to the boundary conditions. What entries of `rhs` do they correspond to?"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To wrap up, solve and plot:"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"import scipy.sparse.linalg as sla\n",
"\n",
"sol = sla.spsolve(A, rhs)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"pt.plot(mesh, sol)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}