Docsity

Assignment 3 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 3 Solution
Section 3.4
3. Implement Algorithm 3.4 on page 146 of the text in MATLAB, and then use the algorithm
to construct the free cubic spline for the following data.
(a)
x f(x)
8.3 17.56492
8.6 18.50515
Solution The following MATLAB function implements Algorithm 3.4.
% this function computes the coefficients of the pieces
% of a cubic spline interpolant that fits the data prescribed
% by the vectors x and y, which represent x-values and y-values
% of the points that the spline must fit. the spline satisfies
% natural boundary conditions.
function [aj,bj,cj,dj]=hw3prob343(x,y)
% the coefficients a0, a1, ..., an are simply equal
% to the y-values
a=y;
n=length(x)-1;
% h contains the spacing between points
for i=1:n,
h(i)=x(i+1)-x(i);
end
% alpha contains the right-hand side of the system of
% equations that must be solved to obtain the
% coefficients c0, c1, c2, ..., cn
for i=1:n-1,
alpha(i)=(3/h(i+1))*(a(i+2)-a(i+1))-(3/h(i))*(a(i+1)-a(i));
end
% the following code solves this system
L(1)=1;
mu(1)=0;
z(1)=0;
for i=1:n-1,
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Assignment 3 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 3 Solution

Section 3.

  1. Implement Algorithm 3.4 on page 146 of the text in MATLAB, and then use the algorithm to construct the free cubic spline for the following data.

(a)

x f (x)

  1. 3 17. 56492
  2. 6 18. 50515 Solution The following MATLAB function implements Algorithm 3.4. % this function computes the coefficients of the pieces % of a cubic spline interpolant that fits the data prescribed % by the vectors x and y, which represent x-values and y-values % of the points that the spline must fit. the spline satisfies % natural boundary conditions. function [aj,bj,cj,dj]=hw3prob343(x,y) % the coefficients a0, a1, ..., an are simply equal % to the y-values a=y; n=length(x)-1; % h contains the spacing between points for i=1:n, h(i)=x(i+1)-x(i); end % alpha contains the right-hand side of the system of % equations that must be solved to obtain the % coefficients c0, c1, c2, ..., cn for i=1:n-1, alpha(i)=(3/h(i+1))(a(i+2)-a(i+1))-(3/h(i))(a(i+1)-a(i)); end % the following code solves this system L(1)=1; mu(1)=0; z(1)=0; for i=1:n-1,

L(i+1)=2(x(i+2)-x(i))-h(i)mu(i); mu(i+1)=h(i+1)/L(i+1); z(i+1)=(alpha(i)-h(i)z(i))/L(i+1); end L(n+1)=1; z(n+1)=0; c(n+1)=0; % given the coefficients c0, c1, ..., cn, the other % coefficients b0, b1, .... bn-1 and d0, d1, ..., dn- % can be computed using the formulas given in lecture for j=n:-1:1, c(j)=z(j)-mu(j)c(j+1); b(j)=(a(j+1)-a(j))/h(j)-h(j)(c(j+1)+2c(j))/3; d(j)=(c(j+1)-c(j))/(3*h(j)); end % output coefficients of pieces aj=a(1:n); bj=b; cj=c; dj=d;

In the following MATLAB session, this function is used to compute the coefficients of the spline that fits the given data.

x=[ 8.3 8.6 ]; y=[ 17.56492 18.50515 ]; [aj,bj,cj,dj]=hw3prob343(x,y)

aj =

bj =

cj =

0

dj =

Solution In the following MATLAB session, the function hw3prob343 from part (a) is used to compute the coefficients of the spline that fits the given data.

x=[ -0.5 -0.25 0 ]; y=[ -0.02475 0.3349375 1.101 ]; [aj,bj,cj,dj]=hw3prob343(x,y)

aj =

-0.02475000000000 0.

bj =

1.03237500000000 2.

cj =

0 4.

dj =

6.50200000000000 -6. We conclude that the natural spline s(x) that fits the given data is described by the two polynomials

s 0 (x) = − 0 .02475 + 1.032375(x + 0.5) + 6.502(x + 0.5)^3 , x ∈ [− 0. 5 , − 0 .25], s 1 (x) = 0 .3349375 + 2.2515(x + 0.25) + 4.8765(x + 0.25)^2 − 6 .502(x + 0.25)^3 , x ∈ [− 0. 25 , 0].

(d)

x f (x)

  1. 1 − 0. 62049958
  2. 2 − 0. 28398668
  3. 3 0. 00660095
  4. 4 0. 24842440

Solution In the following MATLAB session, the function hw3prob343 from part (a) is used to compute the coefficients of the spline that fits the given data.

x=[ 0.1 0.2 0.3 0.4 ];

y=[ -0.62049958 -0.28398668 0.00660095 0.24842440 ];

[aj,bj,cj,dj]=hw3prob343(x,y)

aj =

-0.62049958000000 -0.28398668000000 0.

bj =

3.45508693333333 3.18521313333333 2.

cj =

0 -2.69873800000000 -2.

dj =

-8.99579333333334 -0.94630333333336 9.

We conclude that the natural spline s(x) that fits the given data is described by the three polynomials

s 0 (x) = − 0 .62049958 + 3.4550869¯3(x − 0 .1) − 8 .99579¯3(x − 0 .1)^3 , x ∈ [0. 1 , 0 .2], s 1 (x) = − 0 .28398668 + 3.1852131¯3(x − 0 .2) − 2 .698738(x − 0 .2)^2 − 0 .94630¯3(x − 0 .2)^3 , x ∈ [0. 2 , 0 .3], s 2 (x) = 0 .00660095 + 2.6170764¯3(x − 0 .3) − 2 .982629(x − 0 .3)^2 + 9 .94209¯6(x − 0 .3)^3 , x ∈ [0. 3 , 0 .4].

  1. Given the partition x 0 = 0, x 1 = 0.05, x 2 = 0.1 of [0, 0 .1] and f (x) = e^2 x:

(a) Find the cubic spline s with clamped boundary conditions that interpolates f. Solution The following MATLAB function is a modification of the function hw3prob described previously. This modified function computes a spline that satisfies clamped boundary conditions. % this function computes the coefficients of the pieces % of a cubic spline interpolant that fits the data prescribed % by the vectors x and y, which represent x-values and y-values % of the points that the spline must fit. the spline also % satisfies clamped boundary conditions, as its derivative at % the left and right endpoints must equal z0 and zn,

bj=b; cj=c(1:n); dj=d; In the following MATLAB session, this function is used to compute the coefficients of the spline that fits the given data.

x=[ 0 0.05 0.1 ]; y=exp(2x); z0=2exp(20); zn=2exp(2*0.1); [aj,bj,cj,dj]=hw3prob3421(x,y,z0,zn)

aj =

1.00000000000000 1.

bj =

2.00000000000000 2.

cj =

1.99830182432800 2.

dj =

1.40130811862174 1. We conclude that the clamped spline s(x) that fits the given data is described by the two polynomials

s 0 (x) = 1 + 2x + 1. 9983018 x^2 + 1. 4013081 x^3 , x ∈ [0, 0 .05], s 1 (x) = 1 .1051709 + 2.210340(x − 0 .05) + 2.2084980(x − 0 .05)^2 + 1 .5487625(x − 0 .05)^3 , x ∈ [0. 05 , 1].

(b) Find an approximation for

0 e

2 x (^) dx by evaluating ∫^0.^1 0 s(x)^ dx. Solution The following MATLAB code computes the integral of the spline.

s0=[ dj(1) cj(1) bj(1) aj(1) ]; s1=[ dj(2) cj(2) bj(2) aj(2) ]; s1=powerform(s1,[ x(2) x(2) x(2) ]);

S0=polyint(s0); S1=polyint(s1); int1=polyval(S0,x(2))-polyval(S0,x(1)); int2=polyval(S1,x(3))-polyval(S1,x(2)); int=int1+int

int =

The polyint function computes the coefficients of the antiderivative of a given polyno- mial that is expressed in power form. Then, polyval is used to apply the Fundamental Theorem of Calculus and evaluate the antiderivative at the limits of integration. (c) Use Theorem 3.13 to estimate max 0 ≤x≤ 0. 1 |f (x) − s(x)| and ∣∣ ∣∣

0

f (x) dx −

0

s(x) dx

Solution By Theorem 3.13,

max 0 ≤x≤ 0. 1 |f (x) − s(x)| ≤

5 M

max 0 ≤j≤n− 1 h^4 j ,

where hj = xj+1 − xj for j = 0, 1 and M is an upper bound for |f (4)(x)| on [0, 0 .1]. Since f (4)(x) = 16e^2 x, we have M = 16e^0.^2 = 19.542444. Since h = 0.05, it follows that

max 0 ≤x≤ 0. 1

|f (x) − s(x)| ≤

(0.05)^4 ≈ 1. 6 × 10 −^6.

Because (^) ∣ ∣∣ ∣

∫ (^) b

a

f (x) dx

∣ ≤^ (b^ −^ a) max a≤x≤b |f^ (x)|, it follows that ∣ ∣∣ ∣

0

f (x) dx −

0

s(x) dx

0

f (x) − s(x) dx

≤ 0. 1 max 0 ≤x≤ 0. 1 |f (x) − s(x)|

≈ 1. 6 × 10 −^7.

(d) Determine the cubic spline S with free boundary conditions, and compare S(0.02), s(0.02), and e^0.^04 = 1.04081077. Solution The following MATLAB code computes the spline S and evaluates both splines S(x) and s(x) at x = 0.04. The variable s0 represents the first piece of the clamped spline s, and it was computed using the code from the solution to part (b).

(b)

x f (x) f ′(x)

  1. 1 16. 94410
  2. 3 17. 56492
  3. 5 18. 19056
  4. 7 18. 82091 Solution To compute f ′(8.1), we use the formula

f ′(x 0 ) ≈

− 3 f (x 0 ) + 4f (x 0 + h) − f (x 0 + 2h) 2 h

with h = 0.2, which yields f ′(8.1) ≈ 3 .092050. We use this same formula with h = − 0. 2 to compute f ′(8.7) ≈ 3 .163525. To compute f ′(8.3) and f ′(8.5), we use the centered-difference formula

f ′(x 0 ) ≈ f (x 0 + h) − f (x 0 − h) 2 h

with h = 0.02, to obtain f ′(8.3) ≈ 3 .116150 and f ′(8.5) ≈ 3 .139975.

(c)

x f (x) f ′(x)

  1. 9 − 4. 827866
  2. 0 − 4. 240058
  3. 1 − 3. 496909
  4. 2 − 2. 596792 Solution To compute f ′(2.9), we use the formula

f ′(x 0 ) ≈ − 3 f (x 0 ) + 4f (x 0 + h) − f (x 0 + 2h) 2 h

with h = 0.1, which yields f ′(2.9) ≈ 5 .101375. We use this same formula with h = − 0. 1 to compute f ′(3.2) ≈ 9 .786010. To compute f ′(3.0) and f ′(3.1), we use the centered-difference formula

f ′(x 0 ) ≈ f (x 0 + h) − f (x 0 − h) 2 h

with h = 0.1, to obtain f ′(3.0) ≈ 6 .654785 and f ′(3.1) ≈ 8 .216330.

(d)

x f (x) f ′(x)

  1. 0 3. 6887983
  2. 1 3. 6905701
  3. 2 3. 6688192
  4. 3 3. 6245909

Solution To compute f ′(2.0), we use the formula

f ′(x 0 ) ≈ − 3 f (x 0 ) + 4f (x 0 + h) − f (x 0 + 2h) 2 h

with h = 0.1, which yields f ′(2.0) ≈ 0 .13533150. We use this same formula with h = − 0 .1 to compute f ′(2.3) ≈ − 0 .5546700. To compute f ′(2.1) and f ′(2.2), we use the centered-difference formula

f ′(x 0 ) ≈

f (x 0 + h) − f (x 0 − h) 2 h

with h = 0.1, to obtain f ′(2.1) ≈ − 0 .09989550 and f ′(2.2) ≈ − 0 .3298960.

  1. Consider the following table of data:

x 0. 2 0. 4 0. 6 0. 8 1. 0 f (x) 0. 9798652 0. 9177710 0. 8080348 0. 6386093 0. 3843735

(a) Use Eq. (4.7) to approximate f ′(0.2). Solution Using x 0 = 0.2 and h = 0.2, we obtain

f ′(0.2) ≈

12 h [− 25 f (x 0 ) + 48f (x 0 + h) − 36 f (x 0 + 2h) + 16f (x 0 + 3h) − 3 f (x 0 + 4h)]

≈

[− 25 f (0.2) + 48f (0.4) − 36 f (0.6) + 16f (0.8) − 3 f (1.0)]

≈

[−25(0.9798652) + 48(0.9177710) − 36(0.8080348) + 16(0.6386093) −

3(0.3843735)]

(b) Use Eq. (4.7) to approximate f ′(1.0). Solution Using x 0 = 1.0 and h = − 0 .2, we obtain

f ′(1.0) ≈

12 h

[− 25 f (x 0 ) + 48f (x 0 + h) − 36 f (x 0 + 2h) + 16f (x 0 + 3h) − 3 f (x 0 + 4h)]

≈

[− 25 f (1.0) + 48f (0.8) − 36 f (0.6) + 16f (0.4) − 3 f (0.2)]

≈

[−25(0.3843735) + 48(0.6386093) − 36(0.8080348) + 16(0.9177710) −

3(0.9798652)]

Solution We have

N 1 (h) = f (x 0 + h) − f (x 0 − h) 2 h = (0.4 + e^0.^4 ) − (− 0 .4 + e−^0.^4 )

  1. 8 = 2. 02688081450704 ,

N 1 (h/2) = (0.2 + e^0.^2 ) − (− 0 .2 + e−^0.^2 )

  1. 4 = 2. 00668001270547 ,

N 1 (h/4) = (0.1 + e^0.^1 ) − (− 0 .1 + e−^0.^1 )

  1. 2 = 2. 00166750019844 ,

N 2 (h) = N 1 (h/2) +

N 1 (h/2) − N 1 (h) 3 = 1. 99994641210495 ,

N 2 (h/2) = N 1 (h/4) +

N 1 (h/4) − N 1 (h/2) 3 = 1. 99999666269610 ,

N 3 (h) = N 2 (h/2) + N 2 (h/2) − N 2 (h) 15 = 2. 00000001273551.

(c) f (x) = 2x^ sin x, x 0 = 1.05, h = 0. 4 Solution We have

N 1 (h) = f (x 0 + h) − f (x 0 − h) 2 h =

21.^45 sin 1. 45 − 20.^65 sin 0. 65

  1. 8 = 2. 20316569739156 ,

N 1 (h/2) = 21.^25 sin 1. 25 − 20.^85 sin 0. 85

  1. 4 = 2. 25723724341336 ,

N 1 (h/4) = 21.^15 sin 1. 05 − 20.^95 sin 0. 95

  1. 2 = 2. 27067416730635 , N 2 (h) = N 1 (h/2) + N 1 (h/2) − N 1 (h) 3 = 2. 27526109208729 , N 2 (h/2) = N 1 (h/4) + N 1 (h/4) − N 1 (h/2) 3

N 3 (h) = N 2 (h/2) +

N 2 (h/2) − N 2 (h) 15 = 2. 27514594526068.

(d) f (x) = x^3 cos x, x 0 = 2.3, h = 0. 4 Solution We have

N 1 (h) = f (x 0 + h) − f (x 0 − h) 2 h =

  1. 73 cos 2. 7 − 1. 93 cos 1. 9
  2. 8 = − 19. 47176104025629 ,

N 1 (h/2) =

  1. 53 cos 2. 5 − 2. 13 cos 2. 1
  2. 4 = − 19. 60622304555391 ,

N 1 (h/4) =

  1. 43 cos 2. 4 − 2. 23 cos 2. 2
  2. 2 = − 19. 63685413553630 , N 2 (h) = N 1 (h/2) + N 1 (h/2) − N 1 (h) 3 = − 19. 65104371398645 , N 2 (h/2) = N 1 (h/4) + N 1 (h/4) − N 1 (h/2) 3 = − 19. 64706449886376 ,

N 3 (h) = N 2 (h/2) +

N 2 (h/2) − N 2 (h) 15 = − 19. 64679921785558.

  1. In calculus, we learn that e = limh→ 0 (1 + h)^1 /h.

(a) Determine approximations to e corresponding to h = 0.04, 0.02, and 0.01. Solution Setting h = 0.04, we obtain N 1 (h) = (1 + h)^1 /h = 1. 041 /^0.^04 = 2. 66583633148742 , N 1 (h/2) = 1. 021 /^0.^02 = 2. 69158802907361 , N 1 (h/4) = 1. 011 /^0.^01 = 2. 70481382942153.

Using the quadrature formula, we obtain ∫ (^1)

− 1

x^0 dx ≈

− 1

x^1 dx ≈

− 1

x^2 dx ≈

− 1

x^3 dx ≈

− 1

x^4 dx ≈

Since the exact value of

− 1 x

(^4) dx is 2/5, we conclude that the degree is 3.

  1. Approximate the following integrals using formulas (4.23), (4.24), (4.27) and (4.30). Are the accuracies of the approximations consistent with the error formulas? Which of parts (d) and (e) give the better approximation?

(d)

1

1 x dx

Solution The exact value is ln 10 = 2.302585093. Using formula (4.23) with x 0 = 1, x 1 = 10, and h = 9, we obtain

∫ (^10)

1

x

dx ≈

[f (1) + f (10)]

≈ (4.5)[1 + 0.1] ≈ 4. 95.

The error term is

− h^3 12 f ′′(ξ) = −

ξ^3

where 1 < ξ < 10. It follows that the error lies in the interval (− 121. 5 , − 0 .1215). The actual error is − 2 .64741490700595, which is consistent with the error formula. Using formula (4.24) with x 0 = 1, x 1 = 5.5, x 2 = 10, and h = 4.5, we obtain

∫ (^10)

1

x dx ≈

[f (1) + 4f (5.5) + f (10)]

≈ 1 .5[1 + 4/ 5 .5 + 0.1] ≈ 2. 7409.

The error term is

− h^5 90

f (4)(ξ) = −

(4.5)^5

ξ^5

where 1 < ξ < 10. It follows that the error lies in the interval (− 492. 075 , − 0 .00492075). The actual error is − 0 .43832399791505, which is consistent with the error formula.

Using formula (4.27) with x− 1 = 1, x 0 = 5.5, x 1 = 10, and h = 4.5, we obtain

∫ (^10)

1

x

dx ≈ 2(4.5)f (5.5)

≈ 9 / 5. 5 ≈ 1. 63.

The error term is h^3 3 f ′′(ξ) =

(4.5)^3

ξ^3

where 1 < ξ < 10. It follows that the error lies in the interval (0. 06075 , 60 .75). The actual error is 0.66622145663041, which is consistent with the error formula.

The error term is

− h^5 90

[f (4)(ξ 1 ) + f (4)(ξ 2 )] = −

(2.25)^5

[

ξ 15

ξ 25

]

where 1 < ξ 1 < 5 .5 and 5. 5 < ξ 2 < 10. It follows that the error lies in the inter- val (− 15. 38039914860045 , − 0 .00320917203794). The actual error is − 0 .10531587700370, which is consistent with the error formula. Using formula (4.27) with x− 1 = 1, x 0 = 3.25, x 1 = 5.5, and h = 2.25 for the first integral, and x− 1 = 5.5, x 0 = 7.75, x 1 = 10, and h = 2.25 for the second integral, we obtain ∫ (^5). 5

1

x

dx +

  1. 5

x

dx ≈ 2(2.25)f (3.25) + 2(2.25)f (7.75)

≈ 4. 5 / 3 .25 + 4. 5 / 7. 75 ≈ 1. 96526054590571.

The error term is h^3 3 [f ′′(ξ 1 ) + f ′′(ξ 2 )] =

(2.25)^3

[

ξ 13

ξ^32

]

where 1 < ξ 1 < 5 .5 and 5. 5 < ξ 2 < 10. It follows that the error lies in the interval (0. 05323612415477 , 7 .63939237415477). The actual error is 0.33732454708834, which is consistent with the error formula. Using formula (4.30) with x− 1 = 1, x 0 = 1.9, x 1 = 2.8, x 2 = 3.7, x 3 = 4.6, x 4 = 5.5, and h = 0.9 for the first integral, and x− 1 = 5.5, x 0 = 6.4, x 1 = 7.3, x 2 = 8.2, x 3 = 9.1, x 4 = 10, and h = 0.9 for the second integral, we obtain

∫ (^5). 5

1

x

dx +

  1. 5

x

dx ≈

[11f (1.9) + f (2.8) + f (3.7) + 11f (4.6)] +

5(0.9) 24 [11f (6.4) + f (7.3) + f (8.2) + 11f (9.1)]

≈

[11/ 1 .9 + 1/ 2 .8 + 1/ 3 .7 + 11/ 4 .6] +

[11/ 6 .4 + 1/ 7 .3 + 1/ 8 .2 + 11/ 9 .1]

The error term is

95 h^5 144 [f (4)(ξ 1 ) + f (4)(ξ 2 )] =

95(0.9)^5

[

ξ 15

ξ^52

]

where 1 < ξ 1 < 5 .5 and 5. 5 < ξ 2 < 10. It follows that the error lies in the interval (0. 00195117659907 , 9 .35128268234907). The actual error is 0.05358448878348, which is consistent with the error formula. We see that the formulas used in part (e) give a better approximation.

Section 4.

  1. Write a MATLAB function that accepts as input a MATLAB function f(x), two real numbers a and b, and a positive integer n, and then uses the Composite Trapezoidal rule with the given value of n to compute the integral of f(x) from a to b. Use your function with the indicated values of n to approximate the following integrals.

(a)

1 x^ ln^ x dx,^ n^ = 4 (b)

− 2 x

(^3) ex (^) dx, n = 4

(c)

0

2 x^2 +4 dx,^ n^ = 6 (d)

∫ (^) π 0 x (^2) cos x dx, n = 6

(e)

0 e

2 x (^) sin 3x dx, n = 8

(f)

1

x x^2 +4 dx,^ n^ = 8 (g)

3 √^1 x^2 − 4 dx,^ n^ = 8 (h)

∫ (^3) π/ 8 0 tan^ x dx,^ n^ = 8 Note: Your function must use MATLAB’s feval function to evaluate the integrand at a given point. To learn how to use feval, you may either read the help page by typing help feval in MATLAB, or consult the M-file fevalsamples.m that is posted on the Code page of the site. Solution The following MATLAB function approximates the integral of a given function f from a to b using the Composite Trapezoidal Rule with n subintervals.

function t=hw3prob441(f,a,b,n) h=(b-a)/n; y=0; xj=a; for j=1:n-1, xj=xj+h; y=y+feval(f,xj); end t=(h/2)(feval(f,a)+2y+feval(f,b));