{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework 7"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"INSERT YOUR NAME HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Directions:__ Add work to this notebook to solve the problems below.\n",
"\n",
"Be careful to name the objects you create as described in the problem. Some problems may include some tests you can run to test your code.\n",
"\n",
"This assignment will not be collected.\n",
"\n",
"Some problems are derived from problems in these books:\n",
"* __LL__: *Programming for Computations - Python* by Svein Linge and Hans Petter Langtangen, 2nd edition.\n",
"* __L__: *A Primer on Scientific Programming with Python* by Hans Petter Langtangen, 2nd edition.\n",
"* __TAK__: *Applied Scientific Computing With Python* by Peter R. Turner, Thomas Arildsen, and Kathleen Kavanagh."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Standard imports"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Solving a Bessel equation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(Before starting this problem read Example 7, which starts at the bottom of page 94 of TAK. You may want to go back and read Example 1 on page 36.)\n",
"\n",
"Consider solving the Bessel equation\n",
"$$x^2 y'' + x y' + (x^2 - 1) y = 0$$ \n",
"subject to the boundary conditions $y(1)=1$ and $y(15)=0$ using numerical derivatives with $N = 280$ steps between $[1, 15]$.\n",
"You should begin by rewriting the differential equation in the form\n",
"$$y'' + \\frac{1}{x} y' + (1-\\frac{1}{x^2}) y = 0.$$\n",
"Next, use finite difference approximations to the derivatives and collect like terms to obtain a tridiagonal linear system for the unknown discrete values of y. \n",
"\n",
"Use the function `tridiagonal_solve` from the previous assignment to compute the solution $y$. Graph the solution."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Some help:* The interval is divided into $280$ steps by the points in `x` below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"N = 280\n",
"x = np.linspace(1,15,N+1)\n",
"x"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Step size is then:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"h = x[1] - x[0]\n",
"print(f'h = {h:.2f}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We are looking to find a numpy array $y$ which can be obtained by multiplying by a tridiagonal matrix to get some vector we know. Then we can use `tridiagonal_solve` to find $y$. Here the entries of $y$, $y_i$, should be given by $y_i=y(x_i)$, where $x_i$ is the numpy array above. Thus $y(x_i+h)=y_{i+1}$ and $y(x_i-h)=y_{i-1}$ for $i=1, \\ldots, N-1$.\n",
"\n",
"Observe that \n",
"$$\\left[\\begin{array}{rrrrrr}\n",
"a_0 & b_0 & & & & \\\\\n",
"c_0 & a_1 & b_1 & & & \\\\\n",
" & c_1 & a_2 & b_2 & & \\\\\n",
" & & \\ddots & \\ddots & \\ddots & \\\\\n",
" & & & c_{N-2} & a_{N-1} & b_{N-1} \\\\\n",
" & & & & c_{N-1} & a_{N}\n",
"\\end{array}\\right]\n",
"\\left(\\begin{array}{r}\n",
"y_0 \\\\\n",
"y_1 \\\\\n",
"y_2 \\\\\n",
"\\vdots \\\\\n",
"y_{N-1} \\\\\n",
"y_{N}\\end{array}\\right)=\n",
"\\left(\\begin{array}{r}\n",
"a_0 y_0 + b_0 y_1 \\\\\n",
"c_0 y_0 + a_1 y_1 + b_1 y_2 \\\\\n",
"c_1 y_1 + a_2 y_2 + b_2 y_3 \\\\\n",
"\\vdots \\\\\n",
"c_{N-2} y_{N-2} + a_{N-1} y_{N-1} + b_{N-1} y_{N} \\\\\n",
"c_{N-1} y_{N-1} + a_{N} y_{N}\n",
"\\end{array}\\right)=\\left(\\begin{array}{r}\n",
"r_0 \\\\\n",
"r_1 \\\\\n",
"r_2 \\\\\n",
"\\vdots \\\\\n",
"r_{N-1} \\\\\n",
"r_{N}\\end{array}\\right).\n",
"$$\n",
"You can find the values of $a$, $b$, $c$ and $r$ by analyzing the differential equation and the boundary conditions. (Probably you want to make the boundary conditions correspond to the first and last equation.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Another differential equation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Solve $y'' + 2x y' + 2y = 3e^{−x} − 2xe^{−x}$ subject to $y(−1) = e + 3/e$, $y (1) = 4/e$ using the second-order finite difference method as in the above problem.\n",
"Consider $N = 10, 20, 40$ in $[−1, 1]$. Plot these solutions and the true solution $y = e^{−x} + 3e^{−x^2}$. Also, on a separate set of axes, plot the three error curves.\n",
"Estimate the order of the truncation error for this method. Is it what you would\n",
"expect?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. A third differential equation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Consider the differential equation \n",
"$$y''(x) + \\sin(x) y(x) = x^2$$\n",
"on the interval $[0, \\pi]$ subject to the boundary conditions $y(0)=1$ and $y(\\pi)=0$.\n",
"\n",
"Given an integer $n \\geq 3$, consider cutting the interval into $n$ equal-sized subintervals. You are interested in approximating the solution $y$ at the points \n",
"$$x_0 = 0, x_1 = \\frac{\\pi}{n}, \\ldots, x_{n-1}=\\frac{(n-1)\\pi}{n}, x_n=\\pi.$$\n",
"Set $y_i = y(x_i)$. Using numerical derivatives, find an $n+1 \\times n+1$ matrix $M$ and a vector $v \\in {\\mathbb R}^{n+1}$ such that solving $M y= v$ leads to an approximate solution $y=(y_0, \\ldots, y_{n-1})$ for the differential equation.\n",
"\n",
"Write a function `linearization(n)` which returns a pair of numpy arrays `(m,v)`. Here `m` should represent the matrix $M$ from the previous paragraph and `v` should reprensent the vector $v$.\n",
"\n",
"Write a function `solution(n)` which returns the $y$ that solves the equation $My=v$."
]
},
{
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}