# Adaptive Integral in Python

Continuing my efforts to learn Python for Math, I wrote a function for adaptive integration. It uses 10-point Gauss quadrature. To check the accuracy, the algorithm uses a bipartition of the interval. So each step takes 30 evaluations of the function, one for the complete step and two for the check. If the accuracy is not high enough, the step size will decreased. Otherwise, the function will try to double the step size in the next step.

Here is the complete code.

def legendre (n):
""" Get the Legendre Polynomials

n : maximal degree

Return: list of Lenegdre Polynomals
"""
p0 = np.poly1d([1])
if n<=0:
return [p0]
p1 = np.poly1d([1,0])
px = p1
if n==1:
return [p0,p1]
v=[p0,p1]
for i in range(1,n):
p = ((2*i+1)*px*p1 - i*p0) / (i+1)
p0 = p1
p1 = p
v.append(p1)
return v

def make_gauss(n):
""" Compute the Coefficiens for Gauss Integration

n : Number of points

return: x,a
x : Gauss popints
a : Gauss coefficients
"""
v = legendre(n)
x = np.roots(v[n])
A = np.array([v[i](x) for i in range(n)])
w = np.zeros(n)
for i in range(n):
p = np.polyint(v[i])
w[i] = p(1)-p(-1)
return x,np.linalg.solve(A,w)

gauss_x5,gauss_a5 = make_gauss(5)

def gauss5 (f,a,b):
""" Gauss Integrate with 5 points

f : function of one variable
a,b : Inteterval bounds

returns: Integral
"""
return np.sum(f((a+b)/2+(b-a)/2*gauss_x5)*gauss_a5)*(b-a)/2

gauss_x10,gauss_a10 = make_gauss(10)

def gauss10 (f,a,b):
""" Gauss Integrate with 10 points

f : function of one variable
a,b : Inteterval bounds

returns: Integral
"""
return np.sum(f((a+b)/2+(b-a)/2*gauss_x10)*gauss_a10)*(b-a)/2

def integrate (f,a,b,n=1,eps=1e-14):
""" Adaptive Integral using 10 point Gauss

f : function of one variable
a,b : Interval bounds
n=1 : Initial step size is (b-a)/n
eps : Desired absolute accuracy

returns: Integral
"""
h = (b-a)/n
x = a
I = gauss10(f,x,x+h)
I1 = gauss10(f,x,x+h/2)
I2 = gauss10(f,x+h/2,x+h)
res = 0.
while x<b-eps:
## print(h)
if np.abs(I-(I1+I2))>h/(b-a)*eps:
h = h/2
I = I1;
I1 = gauss10(f,x,x+h/2)
I2 = gauss10(f,x+h/2,x+h)
else:
x = x+h
h=2*h
if x+h>=b-eps:
h=b-x
res = res+I1+I2
I = gauss10(f,x,x+h)
I1 = gauss10(f,x,x+h/2)
I2 = gauss10(f,x+h/2,x+h)
return res

This algorithm is usually fast enough and very accurate. The implementation is by no means optimal. E.g., if a factor of 2 is used in the step size, a lot of computations are redone. Moreover, it might not be optimal to try to double the step size in each step. However, it works for interactive use, and that is what it is intended for.

Let me try to explain the code a bit. The function legendre() computes the Legendre polynomials using the recursion formula. These polynomials are orthogonal with respect to the weight w=1. The function returns a list of the polynomials. It makes use of the polynomial handling in Python (in old style).

The function make_gauss() computes the zeros of the Legendre polynomials using a function from Python, and coefficients a[k], such that the following is exact for all polynomials p up to degree n-1:

$$\sum_{k=1}^n a_k p(x_k) = \int\limits_{-1}^1 p(t) \,dt$$

It sets up n linear equations from this requirement using the Legendre polynomials we just computed as test cases and solves the equations. To compute the correct integral for polynomials, there is a function in Python.

Now, we can define the functions for Gauss integration with 5 and 10 points, and use it for our adaptive integration.

For the demonstration, I created a very easy to use function fplot() which plots a function adaptively using the code of the last posting.

def fplot (f,a,b,n=100,nmax=100000):
""" Simple plot of a function

a,b : interval bounds
n=100 : initital step size (a-b)/n
nmax=100000 : minimal step size (a-b)/nmax

"""
x,y = adaptive_ev(f,a,b,n,nmax)
fig,ax = plt.subplots(figsize=(8,8))
ax.grid(True)
ax.plot(x,y)

I now have put all this and the code of the last posting into a Python module. When using the Python notebook interface in a Browser, the file should be placed in the same directory where the „.ipynb“ file. The Python file can even be opened and edited in the Python notebook IDE.

Then it can be used as follows.

import renestools as rt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

def f(x):
return np.abs(x)

rt.fplot(f,-np.sqrt(2),1,n=1000)
print(rt.integrate(f,-np.sqrt(2),1))
1.5

This is not easy to integrate numerically, since the function is not smooth. The result is exact to the last digit. Even the plot is not trivial. With the arbitrary step size, the point x=0 might not be met exactly, and the corner may look round.

Here is another example using the Gaussian distribution. All digits agree. Note, that the Gauss function is almost zero for large x.

def f(x):
return np.exp(-x**2)

print(rt.integrate(f,-20,20))
print(np.sqrt(np.pi))

1.7724538509055159
1.7724538509055159

You can even make the integral to a function and plot it adaptively.

def F(x):
return rt.integrate(f,0,x)/np.sqrt(np.pi)

rt.fplot(F,0,5)

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.