CoCalc Public Filestmp / TrapezoidRule.ipynb
Authors: Harald Schilly, ℏal Snyder, William A. Stein

# Basic Numerical Integration: the Trapezoid Rule

A simple illustration of the trapezoid rule for definite integration:

$\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).$
First, we define a simple function and sample it between 0 and 10 at 200 points

In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

In [2]:
def f(x):
return (x-3)*(x-5)*(x-7)+85

x = np.linspace(0, 10, 200)
y = f(x)


Choose a region to integrate over and take only a few points in that region

In [3]:
a, b = 1, 9
xint = x[np.logical_and(x>=a, x<=b)][::30]
yint = y[np.logical_and(x>=a, x<=b)][::30]


Plot both the function and the area below it in the trapezoid approximation

In [5]:
plt.plot(x, y, lw=2)
plt.axis([0, 10, 0, 140])
plt.fill_between(xint, 0, yint, facecolor='gray', alpha=0.4)
plt.text(0.5 * (a + b), 30,r"$\int_a^b f(x)dx$", horizontalalignment='center', fontsize=20);

{"metadata":{},"output_type":"display_data","png":"iVBORw0KGgoAAAANSUhEUgAAAXcAAAEACAYAAABI5zaHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3XlcVXX++PHXZbmAIJsoCi644Q64m9WE2WLppDXT4tf8\nWtM2ZZP5bVGbn6mTY6YtVpNO06Q5U+NWjltUbmGWprklIoggqwgqCLJetvv743Mvi4DL5cK5l/t+\nPh73cc49555z3t3wzYf3+ZzPB4QQQgghhBBCCCGEEEIIIYQQQgghhBBCCCFarZVANhDTwL6XgCrA\nv9a2OcBpIB64q9mjE0IIYZFbgcHUT+5dgG+BZGqSe3/gGOAKhACJgFOLRCmEEKKOayXfvcClBra/\nC7x6xbaJwBqgHEhBJfcRTYxPCCGEBSxpWU8EMoDjV2wPMm03ywCCLYxLCCFEE7jc4OfbAK8Bd9ba\nprvK5403HJEQQogmu9Hk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ayf3YCC91vsM07YbUrskc999VotNCCEchouVz2e0\nxklql2SaY1IOnU5Hbm4uR44csf7Jb8C5c+c0j8FWyHdRQ76LGtb4LqqqqnBxsXaqs306K59vFDAf\nVXMHmANUAW/V+kwi0NPK1xVCiNYuCeil1cVdTAGEAHrgGBbeUBVCCGFb7gFOoVroczSORQghhBBC\nCGEJqzzg1Ap0Ab4HYoETwAvahmMTnIGjwFatA9GYL/AlEAecRN3HclRzUP9GYoD/AI40weZKIBv1\n327mD+wAEoDtqJ8Vm+CMKtWEAK44dj2+IxBhWvdClbEc9bsw+z/gC2CL1oFobDXwB9O6C9AM/cXs\nQghwhpqEvg6Yplk0Le9WYDB1k/sS4FXT+ixgcUsH1ZibgG9rvZ9tegnYBIzVOggNdQZ2AmNw7Ja7\nDyqhCdVKPQX4oX7JbQXu0DSilhdC3eQeDwSa1jua3jeqJQcOs8oDTq1QCOo39AGN49DSe8ArqG6z\njqw7cAFYBRwBPgHaaBqRdnKBd4A0IBPIQzUAHFkgqlSDaRl4lc+2aHK3ygNOrYwXqr46AyjUOBat\nTADOo+rt1n7uwt64AEOA5aZlEY77121P4EVU4ycI9W9FpuypYeQaObUlk/tZ1I1Esy6o1rujcgW+\nAj5HlWUc1WjgPiAZWAPcDvxL04i0k2F6/WJ6/yUqyTuiYcA+IAeoADaiflYcWTaqHAPQCdUosgny\ngFMNHSqBvad1IDbmNhy75g7wAxBqWp9P3ae7HUk4qieZB+rfy2pguqYRtbwQ6t9QNfcynI0N3VAF\necDJ7BZUffkYqhxxlJohGxzZbUhvmXBUy/1XVGvVUXvLgOoZYu4KuRr1166jWIO611CGulf5OOom\n805ssCukEEIIIYQQQgghhBBCCCGEEEIIIYQQQgghhBBCCCGEEMIB/X+HJuq2U7ADmgAAAABJRU5E\nrkJggg==\n","text":["<matplotlib.figure.Figure at 0x7ff9b2045650>"]}

Compute the integral both at high accuracy and with the trapezoid approximation

In [6]:
from __future__ import print_function
from scipy.integrate import quad, trapz
integral, error = quad(f, 1, 9)
print("The integral is:", integral, "+/-", error)
print("The trapezoid approximation with", len(xint), "points is:", trapz(yint, xint))

The integral is: 680.0 +/- 7.54951656745e-12 The trapezoid approximation with 6 points is: 621.286411141
In [ ]: