Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ourse

* 'master' of https://github.com/ketch/finite-difference-course:
  Update first FD notebook.
  • Loading branch information
ketch committed Jul 9, 2014
2 parents 19448bb + f685768 commit d8947fd
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions ipython/Week_1_Finite_differences.ipynb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"metadata": {
"name": "Week_1_Finite_differences"
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
Expand Down Expand Up @@ -244,8 +244,8 @@
"Here ${\\mathcal O}(h^4)$ indicates that the rest of the terms in the series vanish at least as quickly as $h^4$ when $h\\to 0$ (see Appendix A of the text).\n",
"Substituting this series in our forward difference formula gives\n",
"\\begin{align}\n",
"\\frac{f(x+h) - f(x)}{h} & = \\frac{f(x) + h f'(x) + \\frac{1}{2}h^2 f''(x) + \\frac{1}{6} h^3 f'''(x) + {\\mathcal O}(h^4) - f(x)}{h} \\\\\\\\\n",
"& = f'(x) + \\frac{1}{2}h f''(x) + \\frac{1}{6} h^2 f'''(x) + {\\mathcal O}(h^3) \\\\\\\\\n",
"\\frac{f(x+h) - f(x)}{h} & = \\frac{f(x) + h f'(x) + \\frac{1}{2}h^2 f''(x) + \\frac{1}{6} h^3 f'''(x) + {\\mathcal O}(h^4) - f(x)}{h} \\\\\n",
"& = f'(x) + \\frac{1}{2}h f''(x) + \\frac{1}{6} h^2 f'''(x) + {\\mathcal O}(h^3) \\\\\n",
"& = f'(x) + \\frac{1}{2}h f''(x) + {\\mathcal O}(h^2).\n",
"\\end{align}\n",
"\n",
Expand Down Expand Up @@ -278,29 +278,29 @@
"source": [
"To begin, suppose we are given three function values: $f(x_0-h), f(x_0), f(x_0+h)$, which we'll denote by $(f_1,f_2,f_3)$. We wish to find a polynomial that passes through these three points. As you may know, a set of $n$ values uniquely defines a polynomial of degree $n-1$, so we will look for a quadratic polynomial. To make the computation simpler, we'll write it this way: $p(x) = a + b (x-x_0) + c (x-x_0)^2$. We know that $p$ and $f$ must agree at the three given points, which means\n",
"\\begin{align}\n",
"a + b(-h) + c (-h)^2 & = f_1 \\\\\\\\\n",
"a + b(0) + c(0)^2 & = f_2 \\\\\\\\\n",
"a + b (h) + c (h)^2 & = f_3 \\\\\\\\\n",
"a + b(-h) + c (-h)^2 & = f_1 \\\\\n",
"a + b(0) + c(0)^2 & = f_2 \\\\\n",
"a + b (h) + c (h)^2 & = f_3 \\\\\n",
"\\end{align}\n",
"\n",
"or simply\n",
"\n",
"\\begin{align}\n",
"a - hb + h^2c & = f_1 \\\\\\\\\n",
"a & = f_2 \\\\\\\\\n",
"a + hb + h^2c & = f_3 \\\\\\\\\n",
"a - hb + h^2c & = f_1 \\\\\n",
"a & = f_2 \\\\\n",
"a + hb + h^2c & = f_3 \\\\\n",
"\\end{align}\n",
"\n",
"We can rewrite this system of equations in matrix form:\n",
"\n",
"\\begin{align}\n",
"\\begin{pmatrix}\n",
"1 & -h & h^2 \\\\\\\\\n",
"1 & 0 & 0 \\\\\\\\\n",
"1 & h & h^2 \\\\\\\\\n",
"1 & -h & h^2 \\\\\n",
"1 & 0 & 0 \\\\\n",
"1 & h & h^2 \\\\\n",
"\\end{pmatrix}\n",
"\\begin{pmatrix} a \\\\\\\\ b \\\\\\\\ c \\end{pmatrix}\n",
"& = \\begin{pmatrix} f_1 \\\\\\\\ f_2 \\\\\\\\ f_3 \\end{pmatrix}\n",
"\\begin{pmatrix} a \\\\ b \\\\ c \\end{pmatrix}\n",
"& = \\begin{pmatrix} f_1 \\\\ f_2 \\\\ f_3 \\end{pmatrix}\n",
"\\end{align}\n",
"\n",
"This is a linear system that we can solve for the coefficients $a,b,c$ in terms of $h$, and the values of $f$. The result is\n",
Expand Down Expand Up @@ -332,28 +332,25 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Extra credit: show how this approach can be generalized to find finite difference formulas using values of $f(x)$ at arbitrary points and approximating any order derivative. Write a short Python script that implements your approach. Check that it gives the same results as the approach described in the text.**"
"<font color='red'>\n",
"Derive a formula for $f''(x)$ based on the values $f(x), f(x-h), f(x-3h)$ by determining the interpolating polynomial and differentiating it. How accurate is your formula?\n",
"</font>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**One thing that is not clear to me after completing the reading and the notebooks is...** (fill in the blank)"
"**Can you think of a similar approach that would allow you to derive rules for numerical quadrature (integration)?**"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"source": []
"outputs": []
}
],
"metadata": {}
Expand Down

0 comments on commit d8947fd

Please sign in to comment.