Computing the Weights in Simpson's Rule

In [1]:
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 [2]:
integrals = np.array([1, 1/2, 1/3])
In [3]:
nodes = np.linspace(0, 1, 3)
nodes
Out[3]:
array([ 0. ,  0.5,  1. ])
In [6]:
V = np.array([
1+0*nodes,
nodes,
nodes**2
])
V
Out[6]:
array([[ 1.  ,  1.  ,  1.  ],
       [ 0.  ,  0.5 ,  1.  ],
       [ 0.  ,  0.25,  1.  ]])

Now compute the weights:

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