Computing the weights in Simpson's rule

In [3]:
import numpy as np
import numpy.linalg as la

We found the integrals:

$$ \begin{align*} \int_0^1 1 dx &= 1\\ \int_0^1 x dx &= \frac 12 \\ \int_0^1 x^2 dx &= \frac 13 \\ \end{align*} $$
In [4]:
integrals = np.array([1, 1/2, 1/3])
In [6]:
nodes = np.linspace(0, 1, 3)
nodes
Out[6]:
array([ 0. ,  0.5,  1. ])
In [8]:
V = np.array([
1+0*nodes,
nodes,
nodes**2
]).T
V
Out[8]:
array([[ 1.  ,  0.  ,  0.  ],
       [ 1.  ,  0.5 ,  0.25],
       [ 1.  ,  1.  ,  1.  ]])

Now compute the weights:

In [11]:
la.inv(V).T.dot(integrals)
Out[11]:
array([ 0.16666667,  0.66666667,  0.16666667])
In [ ]: