Let evaluation points on \([-1, 1]\) be \(-1, -1/2, 0, 1/2, 1\text{.}\) What will their weights be?
Solution.
We need 5 linear equations involving integrals of \(x^0, x^1, \dots, x^4\text{.}\) If we number them by index \(i\) running from 1 to 5, then the equation has \(\int_{-1}^1 x^{i-1}\,dx\) on the right hand side, which is \((1-(-1)^i)/i\text{.}\) On the left we have the sum \(\sum_j w_j x_j^{i-1}\) which means the coefficients of the unknown \(w_j\) is \(x_j^{i-1}\text{.}\) We can create a matrix using outer exponentiation of a row vector by a column vector.
n = 5; x = linspace(-1, 1, n); i = (1:n)'; A = x.^(i-1); b = (1-(-1).^i)./i; w = A\b; disp(rats(w'))
Here
rats is used to display the solution as rational numbers (which they are) rather than the usual decimal approximation.
Having computed the weights as above, we can integrate any function f on [-1, 1] simply by executing
f(x)*w, which is the dot product of a vector with function values with the vector of weights. Try this for the exponential function.
