Professional Documents
Culture Documents
by the integral of the spline. Is the result more accurate than the result of Simpsons rule?
Answer:
Let the spline on the second interval x [0, 1] be
S2 (x) = 2 + b x + c x 2 + d x 3 .
By the symmetry of the boundary conditions, the corresponding spline on the first interval
is
S1 (x) = S2 (x) = 2 b x + c x 2 d x 3 .
The interpolation conditions require
S1 (1) = S2 (1) = 2 + b + c + d = 1 .
is satisfied automatically. Solving the linear equations for c and d, we obtain c = 3/2
and d = 1/2. Therefore
3 1
S2 (x) = 2 x 2 + x 3
2 2
The integral approximation is then
Z1 Z1
2dx 1 1 13
2 S2 (x) d x = 2 2 + = = 3.25
1 + x2 2 8 4
1 0
1
(b) Let Sp[ f ] be the approximation of the integral
Zb
f (x) d x (3)
a
by the integral of the natural cubic spline defined at the knots a = x1 < x2 < . . . < xn = b.
Prove that
n1
X h 3 + h 3k
Sp[ f ] = C T [ f ] ck k1 , (4)
12
k=2
where h k = xk+1 xk , ck are the coefficients in the spline equation
Sk (x) = f (xk ) + bk (x xk ) + ck (x xk )2 + dk (x xk )3 for xk x < xk+1 , (5)
and C T [ f ] is the composite trapezoidal rule.
Answer:
The integral of the spline is the sum of the local integrals:
Zb x
n1 Zk+1
X
Sp[ f ] = S(x) d x = Sk (x) d x .
a k=1 x
k
Integrating the natural cubic spline (5) on the interval [xk , xk+1 ], we obtain
xk+1
h2 h3 h4
Z
Sk (x) d x = f k h k + bk k + ck k + dk k .
2 3 4
xk
hk h3
= ( f k+1 + f k ) k (ck + ck+1 ) .
2 12
The first term in the last expression is exactly the trapezoidal rule on the interval [xk , xk + 1].
Therefore,
n1 3
X hk
Sp[ f ] = C T [ f ] (ck + ck+1 ) .
12
k=1
Recalling that the natural cubic spline has c1 = cn = 0, we can transform the last sum as follows:
n1 3 n1 3 n1 3 n1 3 n1 3 n1
X h k
X h k
X h k
X h k
X h k1 X h 3k1 + h 3k
(ck + ck+1 ) = ck + ck+1 = ck + ck = ck .
12 12 12 12 12 12
k=1 k=1 k=1 k=2 k=2 k=2
The final expression takes the form (4).
2
2. (a) Suppose that function (1) is known at three points: x1 = 1, x2 = 0, and x3 = 1 together
with its derivatives. Interpolate the function using Hermite interpolation and approximate
integral (2) by the integral of the interpolation polynomial. Is the result more accurate
than the result of Simpsons rule?
Answer:
The derivative of f (x) is
4x
f 0 (x) = .
(1 + x 2 )2
The divided-difference table for Hermite interpolation is then
1
P(x) = 1 + (x + 1) + 0 (x + 1)2 (x + 1)2 x + (x + 1)2 x 2 + 0 (x + 1)2 x 2 (x 1)
2
3 2 1 4
= 2 x + x
2 2
Integrating this polynomial, we obtain
Z1 Z1
1 1 16
P(x) d x = 2 P(x) d x = 2 (2 + ) = = 3.2
2 10 5
1 0
This result is more accurate than the result of Simpsons integration (10/3 = 3.3333 . . .)
(b) Let C H [ f ] be the approximation of integral (3) by the integral of the piece-wise cubic
polynomial defined by applying Hermite interpolation at each of the intervals [xk , xk+1 ],
a = x1 < x2 < . . . < xn = b.
Prove that
n1
h 21 h 2n1 X h 2k h 2k1
C H [ f ] = C T [ f ] + b1 bn + bk , (6)
12 12 12
k=2
0
where h k = xk+1 xk , bk = f (xk ), and C T [ f ] is the composite trapezoidal rule.
3
Answer:
The cubic Hermite interpolation in the interval [xk , xk+1 ] follows from the divided-difference
table
x f[] f[, ] f[, , ] f[, , ]
xk fk
xk fk bk
f k+1 f k f k+1 f k 1
xk+1 f k+1 hk hk bk hk
xk+1 f k+1 bk+1 bk+1 fk+1h k fk h1k bk+1 + bk 2 fk+1h k fk h12
k
(x xk )2
f k+1 f k
Pk (x) = f k + bk (x xk ) + bk
hk hk
2
f k+1 f k (x xk ) (x xk+1 )
+ bk+1 + bk 2 .
hk h 2k
Integrating on the interval [xk , xk+1 ], we obtain
xk+1
Z Zh k
Pk (x) d x = Pk (t + xk ) dt
xk 0
Zh k Zh k
f k+1 f k 1
= f k + bk t dt + bk t dt
hk hk
0 0
Zh k
f k+1 f k 1
+ bk+1 + bk 2 t 2 (t h k ) dt
hk h 2k
0
2 2
f k+1 f k h 2k
hk f k+1 f k hk
= f k + bk + bk bk+1 + bk 2
2 hk 3 hk 12
2
hk h
= ( f k+1 + f k ) + k (bk bk+1 ) .
2 12
The first term in the last expression is exactly the trapezoidal rule on the interval [xk , xk + 1].
The total integral is, therefore,
x
n1 Zk+1 n1 2
X X h k
C H[ f ] = Pk (x) d x = C T [ f ] + (bk bk+1 ) .
12
k=1 x k=1
k
4
3. (a) The Gauss-Lobatto quadrature rule has the form
Z1 n1
X
f (x) d x w1 f (1) + wn f (1) + wk f (xk ) = Lo[ f ] , (7)
1 k=2
where the abscissas xk and weights wk are chosen so that f (x) is integrated exactly if it is
a polynomial of order 2 n 3 or less.
Derive the abscissas and weights for n = 3 and n = 4 and apply the Gauss-Lobatto rule to
integral (2). Are the results more accurate than the result of Simpsons rule?
Hint: Use the symmetry of the interval to constrain the abscissas and weights.
Answer:
First, let us consider the case n = 3. By the symmetry of the interval, the point x2 has to
be in the center, and we can immediately set x2 = 0. Integrating polynomials of the first
four powers, we obtain
I [1] = 2 = w1 + w2 + w3
I [x] = 0 = w1 + w3
2
I [x 2 ] = = w1 + w3
3
I [x 3 ] = 0 = w1 + w3
The last two equations define w1 = w3 = 1/3. From the first equation, we get
w2 = 2 w1 w3 = 4/3
I [1] = 2 = w1 + 2 w + w4
I [x] = 0 = w1 + w4
2
I [x 2 ] = = w1 + 2 w x 2 + w4
3
I [x 3 ] = 0 = w1 + w4
2
I [x 4 ] = = w1 + 2 w x 4 + w4
5
I [x 5 ] = 0 = w1 + w4
5
Subtracting the fifth equation from the third one yields
2 2 4
2 w x 2 (1 x 2 ) = =
3 5 15
Dividing by the previously equation, we get
1
x2 =
5
Therefore, x2 = 1/ 5 and x3 = 1/ 5. Next,
2 4
2 w (1 x 2 ) = w=
5 3
and w = 5/6. Solving the remaining equations for w1 and w4 , we get w1 = w4 = 1/6.
The rule is then
1 1 1
Lo4 [ f ] = f (1) + 5 f + 5 f + f (1) .
6 5 5
Applying it to the example problem yields
" #
1 2 28
Lo4 [ f ] = 1+5 1
= = 3.1111 . . .
3 1+ 5 9
where the abscissas xk and weights wk are chosen so that f (x) is integrated exactly if it is
a polynomial of order 2 n 1 or less.
Derive the abscissas and weights for n = 1 and n = 2. Test your formulas by approximat-
ing with
2 2
= 2 0(3/2) 2 La[ x] ,
(9)
where
Z
0(x) = t x1 et dt (10)
0
Answer:
First, let us consider the case n = 1. Integrating polynomials of the first two powers and applying
the general formula
Z
I [x ] = ex x k d x = k!
k
6
we obtain
I [1] = 1 = w1
I [x] = 1 = w1 x1
7
The rule is then
2+ 2 2 2
La2 [ f ] = f 2 2 + f 2+ 2 .
4 4
In the example problem, we get
q q
2+ 2 2 2
La2 [ x] = 2 2+ 2+ 2
p4 q 4
q p
q
2+ 2 2 2 2+ 2
= 2+ 2 2 2+ 2+ 2 =
4 4 2
and 2
2 La2 [ x] = 2 + 2 3.41421
4. (Programming) Implement the adaptive quadrature method. Test your program by computing
integral (2) with the precision of six significant decimal digits. Plot or tabulate the values of x
and f (x) that were involved in the computation.
The algorithm of adaptive integration can be defined either recursively
8
or sequentially
Both algorithms involve the rule R( f , a, b) for computing integral (3), the minimum allowed
interval xtol and the requested precision itol. They are initialized with I = R( f , a, b) and
h = b a. The sequential algorithm operates a queue of intervals using a pair of functions
PUSH and POP.
Run your program taking R to be the trapezoidal rule. What is the appropriate value of constant
?
Answer:
An appropriate value for follows from Richardsons extrapolation. The error of the trapezoidal
rule is
I [ f ] T [ f ] = A h 3 + O(h 5 )
where A does not depend on h. The composite trapezoidal rule applied on two intervals has the
error
h3
I [ f ] C T [ f ] = 2 A (h/2)3 + O(h 5 ) = A + O(h 5 )
4
Multiplying the second equation by and the first equation by 1 and adding them together,
we get
h3
I [ f ] R[ f ] = (1 ) A h 3 + A + O(h 5 ) ,
4
where
R[ f ] = (1 ) T [ f ] + C T [ f ] = T [ f ] + (C T [ f ] T [ f ]) .
The error is minimized if
1+ =0
4
9
or
4
=
3
The combination
4
R[ f ] = T [ f ] + (C T [ f ] T [ f ])
3
is equivalent to Simpsons rule.
1.8
1.6
1.4
1.2
5. (Programming) The length of a parametric curve {x(t), y(t)} is given by the integral
Zb q
2
[x 0 (t)]2 + y 0 (t) dt
(11)
a
3
x(t) = cos t + cos 3 t ; (12)
2
3
y(t) = sin t sin 3 t , (13)
2
defined on the interval 0 t 2 .
Estimate the length of this curve to six significant decimal digits applying a numerical method
of your choice.
Identify the method and plot the {x, y} points involved in the computation.
Answer:
10
The integrand simplifies as follows:
s
32 3
q
2
f (t) = [x 0 (t)]2 + y 0 (t) =
+ 1 + 3 (cos t cos 3 t sin t sin 3 t) = 5 4 cos 4 t
2 2
This function is periodic with the period of /2. Therefore, we can integrate it on the interval
[0, /2] and multiply the result by four:
Z2 Z/2
f (t) dt = 4 f (t) dt
0 0
Applying the adaptive integration method of the previous problem leads to the following distri-
bution of points:
11
Solution:
C program:
/* Function: adaptive_recursive
----------------------------
Adaptive integration by the recursive algorithm.
Uses the trapezoidal rule, upgraded to Simpsons
by one step of Richardsons extrapolation.
f(x) - integrand function
a,b - integration limits
fa = f(a)
fb = f(b)
h = b-a
I - the trapezoidal estimate of the integral
*/
double adaptive_recursive(double (*f)(double x),
double a, double b,
double fa, double fb,
double h, double I)
{
double c, fc, I1, I2, D;
h /= 2;
if (h < xtol) {
fprintf(stderr,"Adaptive integration did not converge.\n");
return I;
}
c = a + h; /* midpoint */
fc = f(c);
/* Function: adaptive_nonrecursive
-------------------------------
Adaptive integration by the non-recursive algorithm.
Uses the trapezoidal rule, upgraded to Simpsons
by one step of Richardsons extrapolation.
f(x) - integrand function
a,b - integration limits
12
fa = f(a)
fb = f(b)
h = b-a
I - the trapezoidal estimate of the integral
*/
double adaptive_nonrecursive(double (*f)(double x),
double a, double b,
double fa, double fb,
double h, double I)
{
double c, fc, I1, I2, D, J;
/* Stack is implemented with a linked list */
struct Stack {
double a,b,fa,fb,h,I;
struct Stack* next;
} *p, *first;
J = 0.;
a = p->a; fa = p->fa;
b = p->b; fb = p->fb;
h = p->h/2; I = p->I;
if (h < xtol) {
fprintf(stderr,"Adaptive integration did not converge.\n");
return I;
}
c = a+h;
fc = f(c);
13
return J;
}
y = 2./(1.+x*x);
printf("%f %f\n",x,y);
return y;
}
f = sqrt(5.-4.*cos(4.*t));
return f;
}
/* main program */
int main(void) {
double a, b, fa, fb, h;
double I;
a = -1.; fa=func4(a);
b = 1.; fb=func4(b);
h = b-a; I = 0.5*h*(fa+fb);
a = 0.; fa=func5(a);
b = 0.5*pi; fb=func5(b); /* integrate to p/2 due to periodicity */
h = b-a; I = 0.5*h*(fa+fb);
return 0;
}
14