{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework 5\n",
"\n",
"Student's Name: PLEASE INSERT YOUR NAME HERE"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Directions:__ Add work to this notebook to solve the problems below. \n",
"\n",
"Check your work. I give some tests that should be passed if you get it correct.\n",
"\n",
"**Your notebook should run without printing errors and without user input. If this is not the case, points will be deducted from your grade.**\n",
"\n",
"Problem Sources:\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": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Standard imports\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import math as m\n",
"from mpmath import mp, iv"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Trapezoid rule\n",
"\n",
"Write a function `trapezoid_rule(f, a, b, N)` which takes as input a function $f$ defined on $[a,b]$ (where $a$ and $b$ are floats with $a**10**-8:\n",
" print(\"Your trapezoid rule returns an incorrect answer \" + \\\n",
" \"when f(x)=sin(x), a=0, b=pi, and N={}.\".format(N))\n",
" print(\"The correct answer should be {}.\".format(sol))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Example of use of trapezoid rule\n",
"\n",
"Use your `trapezoid_rule` function to estimate \n",
"$$\\ln 5 = \\int_1^5 \\frac{1}{x}~dx.$$\n",
"\n",
"Create a list `trap_rule_ln_5` whose entry `trap_rule_ln_5[k]` contains the result obtained by applying the trapezoid rule with $N=2^k$ equal-sized subintervals of $[1,5]$. The list should have length $11$ and contains values `trap_rule_ln_5[k]` for $k \\in \\{0,1,2, \\ldots, 10\\}.$\n",
"\n",
"Compute errors and observe that the errors decrease by a factor of approximately $1/4$ each time $k$ increases by one.\n",
"\n",
"(This problem is close to TAK exercise 4 from § 3.4.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Using the three rules\n",
"\n",
"Use the midpoint, trapezoid, and Simpson's rule to estimate\n",
"$$N_{0,1}(2) = \\frac{1}{2} + \\frac{1}{\\sqrt{2 \\pi}} \\int_0^2 \\exp(\\tfrac{-x^2}{2})~dx$$\n",
"using $N=10$ subintervals. Store the result in the variables `N_mid`, `N_trap`, and `N_simp`.\n",
"\n",
"You may write your own implementations of these methods or use versions in TAK or in Prof. Hooper's notebook.\n",
"\n",
"(The actual integral evaluates to approximately $0.9772498680518$. Which is most accurate?)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. Using data from sensors\n",
"\n",
"A sensor was used to track the speed of a bicyclist during the first five seconds of a race. Measurements times and velocities are stored as a list of pairs $(t,v)$ which are stored in the list `t_v_table` below. Here times is measured in seconds, and velocity in meters per second. \n",
"\n",
"Use numerical integration to determine the distance she covered in meters. Store the result of your calculation in the variable `distance`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"t_v_table = [(0.0, 0.0),\n",
" (0.5, 7.51),\n",
" (1.0, 8.10),\n",
" (1.5, 8.93),\n",
" (2.0, 9.32),\n",
" (2.5, 9.76),\n",
" (3.0, 10.22),\n",
" (3.5, 10.56),\n",
" (4.0, 11.01),\n",
" (4.5, 11.22),\n",
" (5.0, 11.22)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5. Error control\n",
"\n",
"Let $f$ be a function with a continuous second derivative on $[a,b]$. Let $T_N$ be the result obtained by applying the Trapesoid rule to estimate the integral of $f$ over $[a,b]$ using $N$ equal-sized subintervals. The error in this integral estimate is then \n",
"$$E_N = T_N - \\int_a^b f(x)~dx.$$\n",
"Recall that our error formula guarantees that \n",
"$$E_N =\\frac{-(b-a)^3 f''(y)}{12 N^2}$$\n",
"for some $y \\in (a,b)$. Therefore \n",
"$$|E_N| < \\frac{(b-a)^3 M}{12 N^2},$$\n",
"if $M$ satisfies $|f''(x)|0$, and an $\\epsilon>0$, all given as floats. The function should returns the smallest $N$ such that it can be guaranteed that the error satisfies $|E_N|<\\epsilon$ for any function $f$ such that $|f''(x)|**