Docsity

Assignment 4 Solutions - Numerical Analysis | Math 105, Assignments of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
Jim Lambers
Math 105A
Summer Session I 2003-04
Homework Assignment 4 Solution
Section 4.5
1. Use Romberg integration to compute R3,3for the integral
Z1.5
1
x2ln x dx.
Note: R3,3corresponds to N3(h) from Lecture 13, with h=b−a= 0.5.
Solution Let f(x) = x2ln xdenote the integrand. We begin by using the Trapezoidal Rule,
or, equivalently, the Composite Trapezoidal Rule
Zb
a
f(x)dx ≈h
2
f(a) +
n−1
X
j=1
f(xj) + f(b)
, h =b−a
n, xj=a+jh,
with n= 1 subintervals. Since h= (b−a)/n = (1.5−1)/1 = 0.5,we have
R1,1=0.5
2[f(1) + f(1.5)] = 0.22807412331084.
Next, we apply the Composite Trapezoidal Rule with n= 2 and h= (1.5−1)/2=0.25,
which yields
R2,1=0.25
2[f(1) + 2f(1.25) + f(1.5)] = 0.20120251138753.
Now, we can use Richardson Extrapolation to obtain a more accurate approximation,
R2,2=R2,1+R2,1−R1,1
3= 0.19224530741310.
Next, we use the Composite Trapezoidal Rule with n= 4 and h= (1.5−1)/4 = 0.125, and
obtain
R3,1=0.125
2[f(1) + 2[f(1.125) + f(1.25) + f(1.375)] + f(1.5)] = 0.19449447318109.
Now, we can apply Richardson Extrapolation to R2,1and R3,1to obtain
R3,2=R3,1+R3,1−R2,1
3= 0.19225846044561.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Assignment 4 Solutions - Numerical Analysis | Math 105 and more Assignments Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Jim Lambers Math 105A Summer Session I 2003- Homework Assignment 4 Solution

Section 4.

  1. Use Romberg integration to compute R 3 , 3 for the integral ∫ (^1). 5

1

x^2 ln x dx.

Note: R 3 , 3 corresponds to N 3 (h) from Lecture 13, with h = b − a = 0.5. Solution Let f (x) = x^2 ln x denote the integrand. We begin by using the Trapezoidal Rule, or, equivalently, the Composite Trapezoidal Rule ∫ (^) b

a

f (x) dx ≈ h 2

f (a) +

n∑− 1

j=

f (xj ) + f (b)

 (^) , h = b^ −^ a n

, xj = a + jh,

with n = 1 subintervals. Since h = (b − a)/n = (1. 5 − 1)/1 = 0. 5 , we have

R 1 , 1 =

[f (1) + f (1.5)] = 0. 22807412331084.

Next, we apply the Composite Trapezoidal Rule with n = 2 and h = (1. 5 − 1)/2 = 0.25, which yields R 2 , 1 =

[f (1) + 2f (1.25) + f (1.5)] = 0. 20120251138753.

Now, we can use Richardson Extrapolation to obtain a more accurate approximation,

R 2 , 2 = R 2 , 1 +

R 2 , 1 − R 1 , 1

Next, we use the Composite Trapezoidal Rule with n = 4 and h = (1. 5 − 1)/4 = 0.125, and obtain

R 3 , 1 =

[f (1) + 2[f (1.125) + f (1.25) + f (1.375)] + f (1.5)] = 0. 19449447318109.

Now, we can apply Richardson Extrapolation to R 2 , 1 and R 3 , 1 to obtain

R 3 , 2 = R 3 , 1 +

R 3 , 1 − R 2 , 1

Finally, we use Richardson Extrapolation with R 2 , 2 and R 3 , 2 to obtain a new approximation

R 3 , 3 = R 3 , 2 +

R 3 , 2 − R 2 , 2

  1. Use Romberg integration to approximate the integral in Exercise 1 to within 10−^6. Compute the Romberg table until either |Rn− 1 ,n− 1 − Rn,n| < 10 −^6 , or n = 10. Compare your result to the exact value of the integral, which is 0.192259357732796. Solution The following MATLAB function carries out Romberg integration to approximate the integral of a function f from a to b until |Rn− 1 ,n− 1 − Rn,n| < tol or n = 10.

% this function uses romberg integration to approximate % the integral of f from a to b, until consecutive % approximations agree to within tol function R=hw4prob453(f,a,b,tol) h=b-a; % compute initial approximation using Trapezoidal Rule R(1,1)=(h/2)(feval(f,a)+feval(f,b)); % build triangular array of approximations, with up to % ten rows for n=2:10, % use Composite Trapezoidal Rule sumf=0; xj=a+h/2; for j=1:2^(n-2), sumf=sumf+feval(f,xj); xj=xj+h; end R(n,1)=0.5R(n-1,1)+0.5hsumf; % repeated Richardson Extrapolation to improve the % order of accuracy to O(h^(2n)) for j=2:n, R(n,j)=R(n,j-1)+(R(n,j-1)-R(n-1,j-1))/(4^(j-1)-1); end % if consecutive approximations along the diagonal % are sufficiently close, quit if abs(R(n,n)-R(n-1,n-1)) Now, we can use Richardson Extrapolation to obtain a more accurate approximation,

R 2 , 2 = R 2 , 1 +

R 2 , 1 − R 1 , 1

Next, we use the Composite Trapezoidal Rule with n = 4 and h = (5 − 1)/4 = 1, and obtain

R 3 , 1 =

[f (1) + 2[f (2) + f (3) + f (4)] + f (5)] = 11. 5157.

Now, we can apply Richardson Extrapolation to R 2 , 1 and R 3 , 1 to obtain

R 3 , 2 = R 3 , 1 +

R 3 , 1 − R 2 , 1

Finally, we use Richardson Extrapolation with R 2 , 2 and R 3 , 2 to obtain a new approximation

R 3 , 3 = R 3 , 2 +

R 3 , 2 − R 2 , 2

This is a sixth-order-accurate approximation, and it is the most accurate approximation we can obtain, since to proceed any further using Richardson Extrapolation, we would need to use the Composite Trapezoidal Rule with 9 quadrature nodes, for which we do not have sufficient data.

  1. Show that the approximation Rk, 2 is the same as that given by the Composite Simpson’s rule described in Theorem 4.4 with h = hk. Solution For each k = 1, 2 ,... , the approximation Rk, 1 computed using the Composite Trapezoidal Rule uses nk + 1 quadrature nodes x 0 , x 1 ,... , xnk , where nk = 2k−^1 and xj = a + jhk for j = 0, 1 ,... , nk. Therefore, we have

Rk, 2 = Rk, 1 +

Rk, 1 − Rk− 1 , 1 3 =

Rk, 1 −

Rk− 1 , 1

hk 2

f (a) + 2

n∑k − 1

j=

f (a + jhk) + f (b)

hk− 1 2

f (a) + 2

nk ∑− 1 − 1

j=

f (a + jhk− 1 ) + f (b)

hk 2

f (a) + 2

2 k∑−^1 − 1

j=

f (a + jhk) + f (b)

2 hk 2

f (a) + 2

2 k∑−^2 − 1

j=

f (a + 2jhk) + f (b)

hk 3

[f (a) + f (b)] +

2 hk 3

(^2) ∑k−^2

j=

f (a + (2j − 1)hk) +

2 k∑−^2 − 1

j=

f (a + 2jhk)

hk 3

2 k∑−^2 − 1

j=

f (a + 2jhk)

hk 3 [f (a) + f (b)] +

hk 3

(^2) ∑k−^2

j=

f (a + (2j − 1)hk) + 2

2 k∑−^2 − 1

j=

f (a + 2jhk)

hk 3

f (a) + 4

n ∑k / 2

j=

f (x 2 j− 1 ) + 2

nk ∑ / 2 − 1

j=

f (x 2 j ) + f (b)

which is precisely the formula for the Composite Simpson’s Rule with h = hk.

Section 4.

  1. Compute the Simpson’s rule approximations to S(a, b), S(a, (a + b)/2), and S((a + b)/ 2 , b) for the integral (^) ∫ 1 0

x^2 e−x^ dx,

and verify that

1 15

∣∣S(a, b) − S

a,

a + b 2

− S

a + b 2 , b

∫ (^) b

a

f (x) dx − S

a,

a + b 2

− S

a + b 2 , b

where the exact value of the integral is 0.16060279414279. Solution Using S(a, b) =

h 3 [f (a) + 4f (a + h) + f (b)],

where h = (b − a)/2, we have

S(a, b) = S(0, 1)

[f (0) + 4f (0.5) + f (1)]

is equivalent to using the Composite Simpson’s Rule on [0, π/4] with n = 4 subintervals. We obtain

I 2 = π/ 8 6 [f (0) + 4f (π/16) + f (π/8)] + π/ 8 6 [f (π/8) + 4f (3π/16) + f (π/4)]

= π/ 16 3

[f (0) + 4f (π/16) + 2f (π/8) + 4f (3π/16) + f (π/4)] = 2. 58770145345862.

Now, we need to determine whether the approximation I 2 is sufficiently accurate. Because the error in the Composite Simpson’s Rule is O(h^4 ), where h is the width of each subinterval used in the rule, it follows that the actual error in I 2 satisfies

|I 2 − I(f )| ≈

|I 2 − I 1 |,

where If (f ) is the exact value of the integral of f. We find that the relation

|I 2 − I(f )| ≈

|I 2 − I 1 | = 2. 67 × 10 −^4 < 10 −^3

is satisfied, we have achieved sufficient accuracy on this interval and we do not need to subdivide it further. The more accurate approximation I 2 can serve as our approximation to the integral over the original interval [0, π/4].

Section 4.

  1. Approximate the integral (^) ∫ π/ 4

0

x^2 sin x dx

using Gaussian quadrature with n = 2, and compare your result to the exact value, which is 0.08875528443527. Solution On the interval [− 1 , 1], the nodes for Gaussian quadrature with n = 2 are

r 2 , 0 = 0. 5773502692 , r 2 , 1 = − 0. 5773502692 ,

and the corresponding weights are

c 2 , 0 = 1, c 2 , 1 = 1.

To use these nodes and weights, we can transform the original integral into an integral over [− 1 , 1] using the change of variable

x =

[ (^) π 4 t + π 4

]

π 8 (t + 1), t ∈ [− 1 , 1],

which yields (^) ∫ π/ 4

0

x^2 sin x dx =

− 1

( (^) π 8

(t + 1)

sin

( (^) π 8

(t + 1)

) (^) π 8

dt.

Evaluating this integral using Gaussian quadrature yields the approximation ∫ (^) π/ 4

0

x^2 sin x dx =

− 1

( (^) π 8 (t + 1)

sin

( (^) π 8 (t + 1)

) (^) π 8 dt

≈ c 2 , 0

( (^) π 8 (r 2 , 0 + 1)

sin

( (^) π 8 (r 2 , 0 + 1)

) (^) π 8

c 2 , 1

( (^) π 8 (r 2 , 1 + 1)

sin

( (^) π 8 (r 2 , 1 + 1)

) (^) π 8 ≈ (1)

( (^) π 8

sin

( (^) π 8

) (^) π 8

( (^) π 8

sin

( (^) π 8

) (^) π 8 ≈ 0. 08926301565865 ,

which has an absolute error of 5. 077 × 10 −^4. An equivalent approach is to apply the above change of variable to the nodes r 2 , 0 and r 2 , 1 to obtain the Gaussian quadrature nodes for [0, π/8],

˜r 2 , 0 = 0. 61942400222800 , ˜r 2 , 1 = 0. 16597416116945 ,

and scale the weights by (π/4)/2 to obtain the new weights

˜c 2 , 0 = ˜c 2 , 1 = π/ 8 ,

and then compute ∫ (^) π/ 4

0

x^2 sin x dx ≈ ˜c 2 , 0

˜r^22 , 0 sin ˜r 2 , 0

  • ˜c 2 , 1

r˜ 22 , 1 sin ˜r 2 , 1

  1. Repeat Exercise 1 with n = 4.

Solution On the interval [− 1 , 1], the nodes for Gaussian quadrature with n = 4 are

r 4 , 0 = 0. 8611363116 , r 4 , 1 = 0. 3399810436 , r 4 , 2 = − 0. 3399810436 , r 4 , 3 = − 0. 8611363116 ,

and scale the weights by (π/4)/2 to obtain the new weights

˜c 4 , 0 = ˜c 4 , 3 = 0. 13660227823522 , ˜c 4 , 1 = ˜c 4 , 2 = 0. 25609680346350 ,

and then compute ∫ (^) π/ 4

0

x^2 sin x dx ≈

∑^3

i=

˜c 4 ,i

˜r 42 ,i sin ˜r 4 ,i

Section 4.

  1. Use Algorithm 4.4 with n = m = 14 to approximate ∫

R

e−(x+y)^ dA

where R is the region in the plane bounded by the curves y = x^2 and y =

x. Implement the algorithm using MATLAB. Solution The following MATLAB function implements Algorithm 4.4.

% this function computes the integral of f(x,y) on the % domain a <= x <= b, and c(x) <= y <= d(x), where f is % assumed to be an inline function of x and y, and % c and d are assumed to be inline functions of x. % The Composite Simpson’s Rule with n subintervals in the % x direction and m subintervals in the y direction. function J=hw4prob489(f,a,b,c,d,m,n) h=(b-a)/n; J1=0; J2=0; J3=0; for i=0:n, x=a+ih; HX=(d(x)-c(x))/m; K1=f(x,c(x))+f(x,d(x)); K2=0; K3=0; for j=1:m-1, y=c(x)+jHX; Q=f(x,y); if rem(j,2)==0,

K2=K2+Q;

else K3=K3+Q; end end % L is the approximate integral of f(xi,y) from % c(xi) to d(xi), where xi=a+ih L=(K1+2K2+4K3)HX/3; if i==0 | i==n, J1=J1+L; elseif rem(i,2)==0, J2=J2+L; else J3=J3+L; end end J=h(J1+2J2+4J3)/3;

Using this function, we obtain the approximation 0.14791026834524.

Section 4.

  1. Write a MATLAB function that approximates an integral of a given function f (x) on an given interval [a, b], using the Composite Simpson’s rule with a given value of n. Then, use the transformation t = x−^1 and your function to approximate the improper integral ∫ (^) ∞

1

x^2 + 9

dx

with n = 4. Solution Using the transformation t = x−^1 , we obtain ∫ (^) ∞

1

x^2 + 9

dx = −

1

t^2

t−^2 + 9

dt =

0

1 + 9t^2

dt.

The following MATLAB function implements the Composite Simpson’s Rule ∫ (^) b

a

f (x) dx ≈ h 3

f (a) + 2

(n/ ∑2)− 1

j=

f (x 2 j ) + 4

∑^ n/^2

j=

f (x 2 j− 1 ) + f (b)

where h = (b − a)/n and xj = a + jh for j = 0, 1 ,... , n.