{ "metadata": { "name": "", "signature": "sha256:cf22c7ef5c8506e7823efd50f804355e8583f5631a998725c8ed2f08dabb8b40" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Finite Differences Lab Example" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Aron Ahmadia (US Army ERDC) and David Ketcheson (KAUST)" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Teaching Numerical Methods with IPython Notebooks, SciPy 2014" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Creative
This example by Aron Ahmadia and David Ketcheson is licensed under a Creative Commons Attribution 4.0 International License. All code examples are also licensed under the [MIT license](http://opensource.org/licenses/MIT)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from IPython.core.display import HTML\n", "css_file = './example.css'\n", "HTML(open(css_file, \"r\").read())" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n" ], "metadata": {}, "output_type": "pyout", "prompt_number": 1, "text": [ "" ] } ], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Together with your teammate, write a Python script or function to do the following:\n", "\n", "1) Generate a set of $m+2$ equally-spaced points $0=x_0,x_1,x_2,\\dots,x_m+1=1$. \n", "2) Evaluate the following function at those points: \n", "\n", "$$f(x) = 1 + x + 3x^2 + 2\\sin(5x)$$\n", "\n", "3) Evaluate the finite difference approximation\n", "\n", "$$f''(x) \\approx \\frac{f(x_{i-1}) - 2 f(x_i) + f(x_{i+1})}{h^2}$$\n", "\n", "for $i=2,3,\\dots,m$, where $h$ is the spacing between successive points.\n", "\n", "4) Plot the finite difference approximation and the exact values of the second derivative.\n", "\n", "Try running your function with different values of $m$ and check that its output tends to the exact second derivative as $m$ is increased.\n", "\n", "There are at least 3 different ways to program step 3:\n", "\n", "- Using a loop\n", "- Using matrix-vector multiplication\n", "- Using array slicing\n", "\n", "Can you code all 3? Which is the fastest? You can time any line of code in IPython using %timeit." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now add the following to your function:\n", "\n", "1) Compute the error of your approximation in the following grid-function norms:\n", "\n", "- $L_1: h\\sum_i |E(x_i)|$\n", "- $L_2: \\sqrt{h\\sum_i \\left(E(x_i)\\right)^2}$\n", "- $L_\\infty$: $\\max_i |E(x_i)|$\n", "\n", "2) Loop over the following values of $m$: $1,2,4,8,16$. \n", "Compute the three error norms for each $m$-value.\n", "\n", "3) Estimate the convergence rate using the formula $E(h) \\approx Ch^p$. *Hint: take the absolute value of both sides and then take the log. What does the resulting equation describe, and what is the role of $p$?*" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }