Gauss quadrature integration rules: Calculation of quadrature points and weights

Clear["Global`*"]; Off[General::spell, General::spell1];

Polynomials

b[ξ_, n_]  :=  
      Table[(ξ^i), {i, 0, n}]
c [n_] :=  Table[(Subscript[a, i]), {i, 0, n}]
f[ξ_, n_] :=  c[n] . b[ξ, n]
f[x, 3]

Quadrature

W[m_]  :=  Table[(Subscript[w, i]), {i, 1, m}]
qp[m_]  :=  Table[ξ(Subscript[o, i]), {i, 1, m}]
W[2]
qp[2]

      
ev[m_, n_]  :=  
      Table[f[ξ(Subscript[o, i]), n], {i, 1, m}]
q[m_, n_]  :=  W[m] . ev[m, n]
q[2, 3]

Exact integral

(Subscript[int, e])[
        n_]  :=  (∫_(-1)^1) f[x, 
            n] \[DifferentialD]x
(Subscript[int, e])[2]

Error

error[m_, n_] :=  (Subscript[int, e])[n]  -  q[m, n]
error[2, 3]

sol  =  Solve[error[2, 3] == 0]

Let's give it a shot:

Extract the solution of interest

values  = sol[[4]]

weights  =  W[2]  /.  values
qpoints  =  qp[2]  /.  values

Compute the integral exactly and with the quadrature rule

(∫_(-1)^1) f[t, 3] \[DifferentialD]t

q[2, 3]  /.  values

{(Subscript[w, 1]) -> 1, 
        (Subscript[w, 2]) -> 1, ξ(Subscript[o, 2]) -> -(1/(Sqrt[3])), ξ(Subscript[o, 1]) -> 
          (1/(Sqrt[3]))};
quadrature[f_]  :=  (∑_(q = 1)^2) (Subscript[w, q]) f[ξ(Subscript[o, q])]  /.  
        values
quadrature[Cos] // N
(∫_(-1)^1) Cos[ξ] \[DifferentialD]ξ // 
      N