Skip to main content

Section 16.4 Examples and questions

These are additional examples for reviewing the topic we have covered. When reading each example, try to find your own solution before clicking “Answer”. There are also questions for reviewing the concepts of this section.

For a given integer \(n \ge 2\text{,}\) find the Gauss-Legendre evaluation points and weights.

Answer

Combine the code from Example 15.4.2 (computation of Legendre roots) and Example 14.3.1 (computing the weights). This only requires some changes in variable names and in the orientation of vectors (row/column).

n = input('n = ');
p = [1];
q = [1 0];
for m = 1:n-1
    r = ((2*m+1)*[q 0] - m*[0 0 p])/(m+1);
    p = q;
    q = r;
end
x = roots(r)';
disp(x);    %  the evaluation points
i = (1:n)'; 
A = x.^(i-1);
b = (1-(-1).^i)./i;
w = A\b;
disp(w');   %  the weights

For a given integer \(n \ge 2\text{,}\) apply the Gauss-Legendre rule to the integrals \(\int_{-1}^1 e^x\,dx\) and \(\int_{-1}^1 (9x^2+1)^{-1}\,dx\text{.}\) In each case, find the difference between the approximate and exact values.

Answer

Not repeating the code from Example 16.4.1, assume it already ran and computed x, w. With exp(x)*w we get an approximation to the integral of \(e^x\text{.}\) And f(x)*w does the same for the second function, if we define it as f = @(x) (9*x.^2+1).^(-1).

approx = exp(x)*w;
exact = exp(1)-exp(-1);
disp(abs(approx-exact));

f = @(x) (9*x.^2+1).^(-1);
approx = f(x)*w;
exact = (2/3)*atan(3);
disp(abs(approx-exact));

The error is about \(8.2\cdot 10^{-10}\) for the first integral and about \(0.058\) for the second. Somehow, the rule is 100 million times more accurate for the first function than for the second, even though they are both perfectly smooth functions on the interval \([-1, 1]\text{.}\) The reason for such a different behavior is that \(e^x\) is easy to approximate by polynomials while \((9x^2+1)^{-1}\) is not so easy; it has to do with the Taylor series of these functions. This will come up again when we study approximation of functions.

One could theoretically derive an estimate for the error of Gauss-Legendre integration rule in terms of the derivative of \(f\) of order \(2n\text{.}\) But this is impractical, because useful estimates of, for example, 10th derivative, are rarely available. The following example approaches the error estimation from another point of view.

Suppose that \(f\) is a function on \([-1, 1]\) and there exists a polynomial \(p\) of degree \(9\) such that \(|f(x)-p(x)| \le 10^{-7}\) for all \(x\in [-1, 1]\text{.}\) Estimate the error of the 5-point Gauss-Legendre rule applied to \(f\text{.}\)

Answer

The integral triangle inequality yields

\begin{equation*} \left|\int_{-1}^1 f - \int_{-1}^1 p\right| \le \int_{-1}^1 |f-p| \le 2\cdot 10^{-7} \end{equation*}

Also by the triangle inequality,

\begin{equation*} \left|\sum_k w_k f(x_k) - \sum_k w_k p(x_k)\right| \le \sum_k w_k |f(x_k)-p(x_k)| \le 10^{-7} \sum_k w_k = 2\cdot 10^{-7} \end{equation*}

where the last step uses a remark at the end of Section 16.2. Finally, since the degree of \(p\) is \(9 < 2\cdot 5\text{,}\) we have \(\int_{-1}^1 p = \sum_k w_k p(x_k)\text{.}\) Combining the above, we conclude that

\begin{equation*} \left|\int_{-1}^1 f -\sum_k w_k f(x_k)\right| \le 4\cdot 10^{-7} \end{equation*}

In general, the estimates above show that if \(f\) can be approximated within \(\varepsilon\) by some polynomial of degree \(2n-1\text{,}\) then the error of \(n\)-point Gauss-Legendre rule is at most \(4\varepsilon\text{.}\)