Debian Codegraph @

tests/inputs/Trapezoid_Rule.ipynb

raw
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Basic Numerical Integration: the Trapezoid Rule"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "A simple illustration of the trapezoid rule for definite integration:\n",
15 "\n",
16 "$$\n",
17 "\\int_{a}^{b} f(x)\\, dx \\approx \\frac{1}{2} \\sum_{k=1}^{N} \\left( x_{k} - x_{k-1} \\right) \\left( f(x_{k}) + f(x_{k-1}) \\right).\n",
18 "$$\n",
19 "<br>\n",
20 "First, we define a simple function and sample it between 0 and 10 at 200 points"
21 ]
22 },
23 {
24 "cell_type": "code",
25 "execution_count": 1,
26 "metadata": {
27 "collapsed": false
28 },
29 "outputs": [],
30 "source": [
31 "%matplotlib inline\n",
32 "import numpy as np\n",
33 "import matplotlib.pyplot as plt"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": 2,
39 "metadata": {
40 "collapsed": false
41 },
42 "outputs": [],
43 "source": [
44 "def f(x):\n",
45 " return (x-3)*(x-5)*(x-7)+85\n",
46 "\n",
47 "x = np.linspace(0, 10, 200)\n",
48 "y = f(x)"
49 ]
50 },
51 {
52 "cell_type": "markdown",
53 "metadata": {},
54 "source": [
55 "Choose a region to integrate over and take only a few points in that region"
56 ]
57 },
58 {
59 "cell_type": "code",
60 "execution_count": 3,
61 "metadata": {
62 "collapsed": true
63 },
64 "outputs": [],
65 "source": [
66 "a, b = 1, 8 # the left and right boundaries\n",
67 "N = 5 # the number of points\n",
68 "xint = np.linspace(a, b, N)\n",
69 "yint = f(xint)"
70 ]
71 },
72 {
73 "cell_type": "markdown",
74 "metadata": {},
75 "source": [
76 "Plot both the function and the area below it in the trapezoid approximation"
77 ]
78 },
79 {
80 "cell_type": "code",
81 "execution_count": 4,
82 "metadata": {
83 "collapsed": false
84 },
85 "outputs": [],
86 "source": [
87 "plt.plot(x, y, lw=2)\n",
88 "plt.axis([0, 9, 0, 140])\n",
89 "plt.fill_between(xint, 0, yint, facecolor='gray', alpha=0.4)\n",
90 "plt.text(0.5 * (a + b), 30,r\"$\\int_a^b f(x)dx$\", horizontalalignment='center', fontsize=20);"
91 ]
92 },
93 {
94 "cell_type": "markdown",
95 "metadata": {},
96 "source": [
97 "Compute the integral both at high accuracy and with the trapezoid approximation"
98 ]
99 },
100 {
101 "cell_type": "code",
102 "execution_count": 5,
103 "metadata": {
104 "collapsed": false
105 },
106 "outputs": [
107 {
108 "name": "stdout",
109 "output_type": "stream",
110 "text": [
111 "The integral is: 565.2499999999999 +/- 6.275535646693696e-12\n",
112 "The trapezoid approximation with 5 points is: 559.890625\n"
113 ]
114 }
115 ],
116 "source": [
117 "from __future__ import print_function\n",
118 "from scipy.integrate import quad\n",
119 "integral, error = quad(f, a, b)\n",
120 "integral_trapezoid = sum( (xint[1:] - xint[:-1]) * (yint[1:] + yint[:-1]) ) / 2\n",
121 "print(\"The integral is:\", integral, \"+/-\", error)\n",
122 "print(\"The trapezoid approximation with\", len(xint), \"points is:\", integral_trapezoid)"
123 ]
124 }
125 ],
126 "metadata": {
127 "kernelspec": {
128 "display_name": "Python 3",
129 "language": "python",
130 "name": "python3"
131 },
132 "language_info": {
133 "codemirror_mode": {
134 "name": "ipython",
135 "version": 3
136 },
137 "file_extension": ".py",
138 "mimetype": "text/x-python",
139 "name": "python",
140 "nbconvert_exporter": "python",
141 "pygments_lexer": "ipython3",
142 "version": "3.4.2"
143 }
144 },
145 "nbformat": 4,
146 "nbformat_minor": 0
147 }