M371 - Alexiades
Lab 6: Quadrature
[ For each lab, create a new folder (Lab#) and work in it ]
Let L denote the integral of the log function
F(x) = log(x) ( = ln(x) )
over the interval (0,1).
1. Find the exact value of the integral L by hand.
2. Clearly, cannot use any (closed) rule that evaluates F at x=0. Try an open rule:
Implement the Midpoint Rule for integrals of the form I = ∫abf(x)dx
using M subintervals (user-specified M), in a code MidRule.
a. Inputs should be a, b, M
b. The integrand f(x) should be coded as a function subprogram that returns y=f(x).
c. The code should print out: M, value of MR = Midpoint Rule approximation of I, and the error I − MR , nicely.
d. Debug on ∫01 (2x−1) dx with M=4.
e. Test it on ∫01 3(1−x2)dx with M=1000.
f. Now apply it to L = ∫01 log(x)dx with M=10 and M=1000. What are the MR values and errors?
3. Another good option, especially for singular integrals like L, is Gaussian quadrature.
a. To apply Gauss-Legendre, the integral L must be transformed over [−1, 1].
Any interval a≤x≤b can be mapped to −1≤t≤1 with change of variable: x = (a+b)/2 + (b-a)/2 t
Change the variable in L, on paper, so that Gauss-Legendre can be applied.
b. Implement the 2-point Gauss-Legendre rule for integrals of the form ∫−11g(t)dt
in a code gLegendre2.
The evaluation points are ±1/√3 and weights=1.
c. Use it to approximate L. What is the value and error ?
4. Make a change of variable (on paper) to transform L to an integral over (0, Inf), of the form: ∫0∞ g(t)e-t dt
for which Gauss-Laguerre quadrature is appropriate. What is g(t) here?
5. Implement the 2-point Gauss-Laguerre rule
for integrals of the form
∫0∞ g(t)e-t dt
in a code gLaguerre2.
The evaluation points and weights are:
t1 = 2−√2 , t2 = 2+√2 , A1 = 0.853553390593 , A2 = 0.146446609407
Use it to approximate L. What is the value and error ?
6. Compare the results from 3 and 5. Can you explain why one of them
does so much better than the other on this integral ?
Submit, in a text file Lab6.txt (please include separators and labels A,B, ...) :
=====================================================
Lab6: Name, Date
=====================================================
A. Exact value of L (from 1.)
=====================================================
B. Midpoint Rule: results and errors (from 2.f.)
for M=10 and M=1000.
=====================================================
C. Gauss-Legendre 2 pt (from 3.c): result and error
=====================================================
D. Gauss-Laguerre 2 pt (from 5.): result and error. What is g(t) here?
=====================================================
E. Answer to 6.
=====================================================
F. Your MidRule code.
=====================================================
G. Your gLegendre2 code.
=====================================================
H. Your gLaguerre2 code, including formula for g(t).
=====================================================