# coding: utf-8 # # 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 # In[8]: V = np.array([ 1+0*nodes, nodes, nodes**2 ]).T V # Now compute the weights: # In[11]: la.inv(V).T.dot(integrals) # In[ ]: