This page was generated from notebooks/L5/2_integration.ipynb.
Binder badge
You can directly download the pdf-version of this page using the link below.
download

Numerical Integration#

File as PDF

Our second topic today will be about numerical integration, which is useful in determining of course the integrals of functions at certain positions. Here we will only refer to 3 different methods with increasing accuracy.

[2]:
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
%config InlineBackend.figure_forsmat = 'retina'

plt.rcParams.update({'font.size': 10,
                     'lines.linewidth': 1,
                     'lines.markersize': 5,
                     'axes.labelsize': 10,
                     'xtick.labelsize' : 9,
                     'ytick.labelsize' : 9,
                     'legend.fontsize' : 8,
                     'contour.linewidth' : 1,
                     'xtick.top' : True,
                     'xtick.direction' : 'in',
                     'ytick.right' : True,
                     'ytick.direction' : 'in',
                     'figure.figsize': (4, 3),
                     'figure.dpi': 150 })

Box method#

box

The simplest method for the numerical integration of a function f(x) is the box method. There you approximate the function in a certain intervall Δx by a horizontal line at the function value of the left edge of the intervall for example.

abf(x)if(xi)Δx

So lets write a function for that:

[3]:
def f(x):
    return(x)
[4]:
def int_box(f,a,b,N):
    x=np.linspace(a,b,N)
    y=f(x)
    return(np.sum((x[1]-x[0])*y))
[7]:
int_box(f,0,1,100)
[7]:
0.5050505050505051
[11]:
acc=[]
for N in range(10,10000,100):
    acc.append(int_box(f,0,1,N))
plt.loglog(range(10,10000,100),np.array(acc)-0.5)
plt.show()
../../_images/notebooks_L5_2_integration_9_0.png

Trapezoid method#

image0

The trapezoid method is taking the next step of function approximation in the interval Δx. It is approximating it with a linear function.

abf(x)dx=i=1Nf(xi)+f(xi1)2Δx

which is actually the same as

abf(x)dx=[f(x0+f(xN))2+i=1N1f(xi)]Δx

We will use the first formula for coding it, and you may try the second yourself.

[12]:
def int_trap(f,a,b,N):
    x=np.linspace(a,b,N)
    y=f(x)
    return(np.sum((y[1:]+y[:-1])*(x[1]-x[0])/2))

[13]:
## value from the box method
int_box(f,0,1,100)
[13]:
0.5050505050505051
[14]:
## value from the tapez method
int_trap(f,0,1,100)
[14]:
0.5000000000000001

The trapez method therefore seems to give a better accuracy than the box method for the same number of steps.

Simpson method#

The Simpson method now continues with approximating the function now with a collection of parabolas.

abf(x)dxi=1N12x2i1x2i+1gi(x)dx

where the function gi(x) is a parabola

gi(x)=[A]x2+[B]x+[C]

where the [A],[B],[C] depends only on the function values at the edges of the slice.

Simpson

After some extensive algebra, which we do not want to do in detail, we arrive at

abf(x)dxΔx3i=oddN1(f(xi1)+f(xi)+f(xi+1))

as a simple formula on how to calculate the integral of a function using the Simpson method. Note that this method requires N being an odd number, which generates an even number of slices. There is a correction for odd number of slices, which we do not consider here.

[15]:
def int_simp(f,a,b,N):
    x=np.linspace(a,b,N)
    y=f(x)
    return(np.sum((y[0:-2:2]+4*y[1:-1:2]+y[2::2])*(x[1]-x[0])/3))
[16]:
## value from the tapez method
int_trap(f,0,1,100)
[16]:
0.5000000000000001
[17]:
## value from the box method
int_box(f,0,1,100)
[17]:
0.5050505050505051
[18]:
## value from the simpson method
## take care, N needs to be odd
int_simp(f,0,1,99)
[18]:
0.5

It turns out, that the Simpson rule is indeed the best among the three methods we have considered. The error is the box method goes as Δx, the one of the trapezoid method as Δx2, while the simpson method provides and accuracy going with Δx4. Thus doubling the number of integration points decreases the error by a factor of 16.