Skip to content

Commit 189d126

Browse files
committed
Further cleanup of 07_differentiation notebook
1 parent 620623a commit 189d126

File tree

1 file changed

+120
-28
lines changed

1 file changed

+120
-28
lines changed

07_differentiation.ipynb

Lines changed: 120 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@
6363
"\n",
6464
"However, for our purposes here it's somewhat more convenient to use the *Newton Polynomial* Basis\n",
6565
"\n",
66-
"$$n_j(x) = \\prod^{j-1}_{i=0} (x - x_i)$$"
66+
"\\begin{align}\n",
67+
" n_0 &= 1\\\\\n",
68+
" n_j(x) &= \\prod^{j-1}_{i=0} (x - x_i)\\quad\\mathrm{for}\\quad j>0 \\\\\n",
69+
"\\end{align}\n"
6770
]
6871
},
6972
{
@@ -96,7 +99,7 @@
9699
"source": [
97100
"Note: The Newton polynomials have some features common to both the monomials and the Lagrange polynomials\n",
98101
"\n",
99-
"* Like the monomials they are of monic polynomials of increasing degree in $x$, i.e. $n_2(x)$ is quadratic\n",
102+
"* Like the monomials they are monic polynomials of increasing degree in $x$, i.e. $n_2(x)$ is quadratic\n",
100103
"* Like the Lagrange polynomials, some Newton polynomials vanish identically at the interpolation points $x_i$. In particular\n",
101104
"\n",
102105
"$$\n",
@@ -118,10 +121,10 @@
118121
"\n",
119122
"$$P_N(x) = \\sum^N_{j=0} a_j n_j(x)$$\n",
120123
"\n",
121-
"as the Linear system\n",
124+
"as the $(N+1)\\times (N+1)$ linear system\n",
122125
"\n",
123126
"$$\n",
124-
" P_N(x_i) = y_i\n",
127+
" P_N(x_i) = y_i\\quad\\mathrm{for}\\quad i=0,\\ldots,N\n",
125128
"$$"
126129
]
127130
},
@@ -279,12 +282,12 @@
279282
},
280283
"outputs": [],
281284
"source": [
282-
"\n",
283285
"# Calculate a polynomial in Newton Form\n",
284286
"data = numpy.array([[-2.0, 1.0], [-1.5, -1.0], [-0.5, -3.0], [0.0, -2.0], [1.0, 3.0], [2.0, 1.0]])\n",
285287
"x = numpy.linspace(-2.0, 2.0, 100)\n",
286288
"basis = newton_basis(x, data[:,0])\n",
287-
"P = P_newton(x, data)"
289+
"P = P_newton(x, data)\n",
290+
"N = data.shape[0] - 1"
288291
]
289292
},
290293
{
@@ -358,14 +361,23 @@
358361
"cell_type": "markdown",
359362
"metadata": {
360363
"slideshow": {
361-
"slide_type": "subslide"
364+
"slide_type": "fragment"
362365
}
363366
},
364367
"source": [
365-
"We know from Lagrange's Theorem that the remainder term looks like\n",
366-
"\n",
367-
"$$R_N(x) = (x - x_0)(x - x_1)\\cdots (x - x_{N})(x - x_{N+1}) \\frac{f^{(N+1)}(c)}{(N+1)!}$$\n",
368+
"And we know from Lagrange's Theorem that the remainder term looks like\n",
368369
"\n",
370+
"$$R_N(x) = (x - x_0)(x - x_1)\\cdots (x - x_{N})(x - x_{N+1}) \\frac{f^{(N+1)}(c)}{(N+1)!}$$"
371+
]
372+
},
373+
{
374+
"cell_type": "markdown",
375+
"metadata": {
376+
"slideshow": {
377+
"slide_type": "subslide"
378+
}
379+
},
380+
"source": [
369381
"noting that we need to require that $f(x) \\in C^{N+1}$ on the interval of interest. Taking the derivative of the interpolant $P_N(x)$ then leads to \n",
370382
"\n",
371383
"$$P_N'(x) = [y_0, y_1] + ((x - x_1) + (x - x_0)) [y_0, y_1, y_2] + \\cdots + \\left(\\sum^{N-1}_{i=0}\\left( \\prod^{N-1}_{j=0,~j\\neq i} (x - x_j) \\right )\\right ) [y_0, y_1, \\ldots, y_N]$$"
@@ -375,7 +387,7 @@
375387
"cell_type": "markdown",
376388
"metadata": {
377389
"slideshow": {
378-
"slide_type": "subslide"
390+
"slide_type": "fragment"
379391
}
380392
},
381393
"source": [
@@ -401,13 +413,26 @@
401413
"cell_type": "markdown",
402414
"metadata": {
403415
"slideshow": {
404-
"slide_type": "subslide"
416+
"slide_type": "fragment"
405417
}
406418
},
407419
"source": [
408420
"If we let $\\Delta x = \\max_i |x_k - x_i|$ we then know that the remainder term will be $\\mathcal{O}(\\Delta x^N)$ as $\\Delta x \\rightarrow 0$ thus showing that this approach converges and we can find arbitrarily high order approximations (ignoring floating point error)."
409421
]
410422
},
423+
{
424+
"cell_type": "markdown",
425+
"metadata": {
426+
"slideshow": {
427+
"slide_type": "fragment"
428+
}
429+
},
430+
"source": [
431+
"### <font color='red'>Caution</font>\n",
432+
"\n",
433+
"High order does not necessarily imply high-accuracy!"
434+
]
435+
},
411436
{
412437
"cell_type": "code",
413438
"execution_count": null,
@@ -486,6 +511,7 @@
486511
{
487512
"cell_type": "markdown",
488513
"metadata": {
514+
"hide_input": true,
489515
"slideshow": {
490516
"slide_type": "slide"
491517
}
@@ -720,7 +746,7 @@
720746
"source": [
721747
"$$\\begin{aligned}\n",
722748
" P_2'(x) &= [f(x_n), f(x_{n+1})] + ((x - x_n) + (x - x_{n+1})) [f(x_n), f(x_{n+1}), f(x_{n-1})] \\\\\n",
723-
" &= \\frac{f(x_{n+1}) - f(x_n)}{x_{n+1} - x_n} + ((x - x_n) + (x - x_{n+1})) \\left ( \\frac{f(x_{n-1}) - f(x_{n+1})}{(x_{n-1} - x_{n+1})(x_{n-1} - x_n)} - \\frac{f(x_{n+1}) - f(x_n)}{(x_{n+1} - x_n)(x_{n-1} - x_n)} \\right )\n",
749+
" &= \\frac{f(x_{n+1}) - f(x_n)}{x_{n+1} - x_n} + ((x - x_n) + (x - x_{n+1}))\\\\ &* \\left ( \\frac{f(x_{n-1}) - f(x_{n+1})}{(x_{n-1} - x_{n+1})(x_{n-1} - x_n)} - \\frac{f(x_{n+1}) - f(x_n)}{(x_{n+1} - x_n)(x_{n-1} - x_n)} \\right )\n",
724750
"\\end{aligned}$$"
725751
]
726752
},
@@ -777,8 +803,9 @@
777803
"cell_type": "code",
778804
"execution_count": null,
779805
"metadata": {
806+
"hide_input": true,
780807
"slideshow": {
781-
"slide_type": "skip"
808+
"slide_type": "subslide"
782809
}
783810
},
784811
"outputs": [],
@@ -794,7 +821,7 @@
794821
"\n",
795822
"# Compute derivative\n",
796823
"f_prime_hat = numpy.empty(x_hat.shape)\n",
797-
"f_prime_hat[1:-1] = (f(x_hat[2:]) - f(x_hat[:-2])) / (2 * delta_x)\n",
824+
"f_prime_hat[1:-1] = (f(x_hat[2:]) - f(x_hat[:-2])) / (2. * delta_x)\n",
798825
"\n",
799826
"# Use first-order differences for points at edge of domain\n",
800827
"f_prime_hat[0] = (f(x_hat[1]) - f(x_hat[0])) / delta_x # Forward Difference at x_0\n",
@@ -807,7 +834,7 @@
807834
"cell_type": "code",
808835
"execution_count": null,
809836
"metadata": {
810-
"hide_input": false,
837+
"hide_input": true,
811838
"slideshow": {
812839
"slide_type": "fragment"
813840
}
@@ -818,11 +845,14 @@
818845
"plt.rcdefaults()\n",
819846
"axes = fig.add_subplot(1, 1, 1)\n",
820847
"\n",
821-
"axes.plot(x, f_prime(x), 'k')\n",
848+
"axes.plot(x, f_prime(x), 'k',label=\"$f'(x)$\")\n",
822849
"axes.plot(x_hat, f_prime_hat, 'ro')\n",
850+
"axes.plot(x, f(x),'g--',label=\"$f(x)$\")\n",
851+
"axes.plot(x_hat, f(x_hat), 'go')\n",
823852
"axes.set_xlim((x[0], x[-1]))\n",
824853
"# axes.set_ylim((-1.1, 1.1))\n",
825854
"axes.grid()\n",
855+
"axes.legend()\n",
826856
"\n",
827857
"plt.show()"
828858
]
@@ -884,7 +914,7 @@
884914
{
885915
"cell_type": "markdown",
886916
"metadata": {
887-
"hide_input": false,
917+
"hide_input": true,
888918
"slideshow": {
889919
"slide_type": "subslide"
890920
}
@@ -907,16 +937,66 @@
907937
"source": [
908938
"Say we want to derive the second order accurate, first derivative approximation that we just did, this requires the values $(x_{n+1}, f(x_{n+1})$ and $(x_{n-1}, f(x_{n-1})$. We can express these values via our Taylor series approximation above as\n",
909939
"\n",
910-
"$$\\begin{aligned}\n",
940+
"\\begin{aligned}\n",
911941
" f(x_{n+1}) &= f(x_n) + (x_{n+1} - x_n) f'(x_n) + \\frac{(x_{n+1} - x_n)^2}{2!} f''(x_n) + \\frac{(x_{n+1} - x_n)^3}{3!} f'''(x_n) + \\mathcal{O}((x_{n+1} - x_n)^4) \\\\\n",
912-
" &= f(x_n) + \\Delta x f'(x_n) + \\frac{\\Delta x^2}{2!} f''(x_n) + \\frac{\\Delta x^3}{3!} f'''(x_n) + \\mathcal{O}(\\Delta x^4)\n",
913-
"\\end{aligned}$$\n",
914-
"\n",
942+
"\\end{aligned}"
943+
]
944+
},
945+
{
946+
"cell_type": "markdown",
947+
"metadata": {
948+
"slideshow": {
949+
"slide_type": "fragment"
950+
}
951+
},
952+
"source": [
953+
"or\n",
954+
"\\begin{aligned}\n",
955+
"&= f(x_n) + \\Delta x f'(x_n) + \\frac{\\Delta x^2}{2!} f''(x_n) + \\frac{\\Delta x^3}{3!} f'''(x_n) + \\mathcal{O}(\\Delta x^4)\n",
956+
"\\end{aligned}"
957+
]
958+
},
959+
{
960+
"cell_type": "markdown",
961+
"metadata": {
962+
"slideshow": {
963+
"slide_type": "subslide"
964+
}
965+
},
966+
"source": [
915967
"and\n",
916968
"\n",
917-
"$$f(x_{n-1}) = f(x_n) + (x_{n-1} - x_n) f'(x_n) + \\frac{(x_{n-1} - x_n)^2}{2!} f''(x_n) + \\frac{(x_{n-1} - x_n)^3}{3!} f'''(x_n) + \\mathcal{O}((x_{n-1} - x_n)^4) $$\n",
918-
"\n",
919-
"$$ = f(x_n) - \\Delta x f'(x_n) + \\frac{\\Delta x^2}{2!} f''(x_n) - \\frac{\\Delta x^3}{3!} f'''(x_n) + \\mathcal{O}(\\Delta x^4) $$"
969+
"\\begin{align}\n",
970+
"f(x_{n-1}) &= f(x_n) + (x_{n-1} - x_n) f'(x_n) + \\frac{(x_{n-1} - x_n)^2}{2!} f''(x_n) + \\frac{(x_{n-1} - x_n)^3}{3!} f'''(x_n) + \\mathcal{O}((x_{n-1} - x_n)^4) \n",
971+
"\\end{align}"
972+
]
973+
},
974+
{
975+
"cell_type": "markdown",
976+
"metadata": {
977+
"slideshow": {
978+
"slide_type": "fragment"
979+
}
980+
},
981+
"source": [
982+
"\\begin{align} \n",
983+
"&= f(x_n) - \\Delta x f'(x_n) + \\frac{\\Delta x^2}{2!} f''(x_n) - \\frac{\\Delta x^3}{3!} f'''(x_n) + \\mathcal{O}(\\Delta x^4)\n",
984+
"\\end{align}"
985+
]
986+
},
987+
{
988+
"cell_type": "markdown",
989+
"metadata": {
990+
"slideshow": {
991+
"slide_type": "subslide"
992+
}
993+
},
994+
"source": [
995+
"Or all together (for regularly spaced points),\n",
996+
"\\begin{align} \n",
997+
"f(x_{n+1}) &= f(x_n) + \\Delta x f'(x_n) + \\frac{\\Delta x^2}{2!} f''(x_n) + \\frac{\\Delta x^3}{3!} f'''(x_n) + \\mathcal{O}(\\Delta x^4)\\\\\n",
998+
"f(x_{n-1})&= f(x_n) - \\Delta x f'(x_n) + \\frac{\\Delta x^2}{2!} f''(x_n) - \\frac{\\Delta x^3}{3!} f'''(x_n) + \\mathcal{O}(\\Delta x^4)\n",
999+
"\\end{align}"
9201000
]
9211001
},
9221002
{
@@ -933,7 +1013,18 @@
9331013
" f'(x_n) + R(x_n) = A f(x_{n+1}) + B f(x_n) + C f(x_{n-1})\n",
9341014
"$$\n",
9351015
"\n",
936-
"where $R(x_n)$ is our error. Plugging in the Taylor series approximations we find\n",
1016+
"where $R(x_n)$ is our error. "
1017+
]
1018+
},
1019+
{
1020+
"cell_type": "markdown",
1021+
"metadata": {
1022+
"slideshow": {
1023+
"slide_type": "fragment"
1024+
}
1025+
},
1026+
"source": [
1027+
"Plugging in the Taylor series approximations we find\n",
9371028
"\n",
9381029
"$$\\begin{aligned}\n",
9391030
" f'(x_n) + R(x_n) &= A \\left ( f(x_n) + \\Delta x f'(x_n) + \\frac{\\Delta x^2}{2!} f''(x_n) + \\frac{\\Delta x^3}{3!} f'''(x_n) + \\mathcal{O}(\\Delta x^4)\\right ) \\\\\n",
@@ -985,7 +1076,7 @@
9851076
"cell_type": "markdown",
9861077
"metadata": {
9871078
"slideshow": {
988-
"slide_type": "subslide"
1079+
"slide_type": "skip"
9891080
}
9901081
},
9911082
"source": [
@@ -1072,8 +1163,9 @@
10721163
"cell_type": "code",
10731164
"execution_count": null,
10741165
"metadata": {
1166+
"hide_input": true,
10751167
"slideshow": {
1076-
"slide_type": "skip"
1168+
"slide_type": "fragment"
10771169
}
10781170
},
10791171
"outputs": [],

0 commit comments

Comments
 (0)