
















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















Jim Lambers Math 105A Summer Session I 2003- Homework Assignment 3 Solution
(a)
x f (x)
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)
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].
(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)| ≤
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)| ≤
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)
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)
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)
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.
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)]
≈
(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)]
≈
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 )
N 1 (h/2) = (0.2 + e^0.^2 ) − (− 0 .2 + e−^0.^2 )
N 1 (h/4) = (0.1 + e^0.^1 ) − (− 0 .1 + e−^0.^1 )
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
N 1 (h/2) = 21.^25 sin 1. 25 − 20.^85 sin 0. 85
N 1 (h/4) = 21.^15 sin 1. 05 − 20.^95 sin 0. 95
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 =
N 1 (h/2) =
N 1 (h/4) =
N 3 (h) = N 2 (h/2) +
N 2 (h/2) − N 2 (h) 15 = − 19. 64679921785558.
(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.
(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)(ξ) = −
ξ^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 ′′(ξ) =
ξ^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 )] = −
ξ 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 +
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 )] =
ξ 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 +
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)]
≈
The error term is
95 h^5 144 [f (4)(ξ 1 ) + f (4)(ξ 2 )] =
ξ 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.
(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));