For each of the following exercises create the specified files with your answer(s). Submit your homework using the course submit utility.
P(x) = e-x(x5 - 4x4 + 5x3 - 2x2 + x - 1)on the interval [0,infinity]. Get the exact result from Maple.
Here are the Gauss-Laguerre abscissas and weights for n=3
x_i c_i
0.415774558 .7110930104
2.294280360 .2785177333
6.289945083 .0103892566
You may use Maple to do the arithmetic.
Please give your calculation, explanation, and results in a text file glag3.txt.
Hint: Remember that with Laguerre polynomials, the factor e-x is part of the integration weight here, so it isn't included in the rhs expression: c1 f(x1) + c2 f(x2) + c3 f(x3).
int(exp(-x)*f(x),x=0..infinity) = int(exp(-x)*R(x),x=0..infinity)
The error function erf(x) is frequently used in statistics. It is the integral of a normalized Gaussian distribution. In Maple notation
erf(x) = 2/sqrt(Pi) * int((exp(-t^2), t=0..x)The normalized Gaussian has standard deviation 1/sqrt(2) and mean 0. Given a random variable normally distributed in this way, the error function gives the probability of getting a value in the range [-x,x]. Because of the symmetry of the normal distribution, the result can be found by evaluating the integral from 0 to x and doubling the result, as was done in the definition of erf(x).
You should carry out the integral using two numerical methods:
For this exercise you will submit the following files:
| File | Purpose of procedure |
|---|---|
| erf.cc | Supervises the integration, reads parameters, writes results |
| simp.cc | Does composite Simpson's rule integration |
| Makefile | Rule for compiling the above. |
All computations are to be done in double precision where possible.
So we can check your code, the main program should
2/sqrt(Pi) * exp(-t^2)
Please don't forget to #include <cmath> to get the correct
exp.
You will use this procedure to evaluate the composite Simpson's rule integral from zero to x. However, it is supposed to be general enough to handle any integrand, any (even) number of intervals, and any end points, so you can use it for other applications.
This procedure should be coded as a function-type subprogram, so that it can be called from the main program as follows:
asimp = simp(n,a,b,normdist);The result of this call is the evaluation of the integral of the function normdist over the range a to b by means of an n-interval (n+1-point) Simpson's rule. (The first point is at x = a, the last point at x = b, with n equal intervals (b-a)/n between.)
You should design your subprogram so that it will integrate any function that is provided to it. See the handout to see how this is done. That is the reason for setting up the call to simp so that the name of the function that evaluates the integrand is passed as a dummy argument.
a = 0; b = x; agleg = gleg8(a,b,normdist);
Copy the program gleg8.cc into the directory where you keep your files for this assignment and include gleg8.o in the list of object files to be linked to make the executable. Remember that make is smart enough to know that if gleg8.o doesn't exist, but gleg8.cc exists, it can be generated with g++, and so it will compile it automatically, without your having to say so explicitly.
(a) Run your program, using n = 8 and x = 2. Compare the results of the two numerical methods with the "exact" result from Maple. Be sure to use at least 10 digit accuracy in the Maple evalf.
(b) Estimate the error for your Simpson's rule result. Use the Richardson method to do that.
Do this by repeating the calculation with n = 4. We then have two equations in two unknowns:
I = S(4) + k h^4 = S(4) + 16*e
I = S(8) + k (h/2)^4 = S(8) + e
where S(4) is the numerical result for the n = 4
integration and S(8) is the numerical result for the n =
8 integration. I is the "exact" integral (actually, just
an improved estimate of the exact integral because we neglected
corrections of higher order in h in this formula) and the terms in k
are the corrections that we use to estimate the error. Treat these as
two equations in two unknowns and calculate the value of e and
I. e is your error estimate for I.
(c) Does the error estimate in (b) give an reasonable accounting (order of magnitude) of the difference between I and the Maple answer?
Please put your calculations and results in a file erf.txt.