Sometimes I tuck my knees into my chest and lean forward.
That's just how I roll.
\[ \small{\int_{a}^b f(x)dx \cong \sum_{k=1}^N c_k f(x_k), \,\, c_k = \Delta x = \frac{b-a}{N} } \]
On [-1, 1] with two nodes, GQ uses non-endpoint trapezoid:
\[ \small{ \begin{aligned} c_1 &= c_2 = 1, \, \, x_1 = -\frac{1}{\sqrt{3}}, \, \, x_2 = \frac{1}{\sqrt{3}} \\ \int_{a}^b f(x)dx &\cong \sum_{k=1}^n c_k f(x_k) = 1 \cdot f(x_1) + 1 \cdot f(x_2) \end{aligned} } \]
For this example, \( f(x) = 1.7x^3 + 3x^2 + 2.4x +4 \)
For this example, \( f(x) = 1.7x^3 + 3x^2 + 2.4x +4 \)
For \( n=2 \) data points, want GQ to be exact for polynomials of degree \( 2n-1 = 2(2)-1 = 3 \) or less on [-1,1]:
\[ \small{ \int_{-1}^1 P_3(x)dx = c_1 \cdot P_3(x_1) + c_2 \cdot P_3(x_2) } \]
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import legendre
for n in range(0,6):
Pn = legendre(n)
x = np.linspace(-1,1,100)
y = Pn(x)
plt.plot(x, y)
plt.xlabel('x')
plt.legend(('P0','P1','P2','P3','P4','P5'),loc=0)
plt.title('Legendre Polynomials')
plt.show()
On [-1, 1] with two nodes, GQ uses non-endpoint trapezoid:
\[ \small{ \begin{aligned} c_1 &= c_2 = 1, \, \, x_1 = -\frac{1}{\sqrt{3}}, \, \, x_2 = \frac{1}{\sqrt{3}} \\ \int_{a}^b f(x)dx &\cong \sum_{k=1}^n c_k f(x_k) = 1 \cdot f(x_1) + 1 \cdot f(x_2) \end{aligned} } \]
From the Scipy Special Functions Package:
from scipy.special.orthogonal import p_roots
[x,w] = p_roots(2)
print('x = ', x, ',','w = ', w)
x = [-0.57735027 0.57735027] , w = [1. 1.]
from scipy.special.orthogonal import p_roots
def GQ1(f,n): #n-pt GQ on [-1,1]
[x,w] = p_roots(n+1)
G = sum(w*f(x))
return G
def GQ2(f,n,a,b): #n-pt GQ on [a,b]
[x,w] = p_roots(n+1)
G = 0.5*(b-a)*sum(w*f(0.5*(b-a)*x+0.5*(b+a)))
return G
f = lambda x: 1.7*x**3 + 3*x**2 + 2.4*x + 4
GQ1(f,2)
10.0
GQ1(f,3)
10.0
f = lambda x: np.exp(-x**2)
GQ2(f,3,0,1)
0.7468244681309939