98
Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 December 21, 2016 1 Department of Mathematics & Statistics, University of Guelph

Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

  • Upload
    lycong

  • View
    234

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Homework and Computer Solutions forMath*2130 (W17).

MARCUS R. GARVIE 1

December 21, 2016

1Department of Mathematics & Statistics, University of Guelph

Page 2: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

STOP!

Before looking at the answers the best study strategy is

to:

1. First read your lecture notes for the relevant section.

2. Then attempt the questions without first looking at

the answers.

3. Finally, if you are stuck, look at the general approach

in the relevant answer and try again.

Remember, some struggle is often necessary for effectivelearning.

1

Page 3: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Chapter 1

Basic Tools

1.4 Taylor’s Theorem

1. What is the third-order Taylor polynomial for f(x) =√x+ 1, about

x0 = 0?Solution:

P3(x) = f(0) + xf ′(0) +x2

2!f ′′(0) +

x3

3!f ′′′(0) (1.1)

f(0) = 1

f ′(x) =1

2(x+ 1)−

12 so f ′(0) =

1

2

f ′′(x) = −1

4(x+ 1)−

32 so f ′′(0) = −1

4

f ′′′(x) =3

8(x+ 1)−

52 so f ′′′(0) =

3

8

Substituting in (1.1) yields

P3(x) = 1 +1

2x− 1

8x2 +

1

16x3.

2. Given that

R(x) =|x|6

6!eξ

2

Page 4: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

for x ∈ [−1, 1], where ξ is between x and 0, find and upper bound for|R|, valid for all x ∈ [−1, 1], that is independent of x and ξ.

Solution: Note first that x ∈ [−1, 1]⇐⇒ |x| ≤ 1.

so|x|6 · eξ

6!≤ 1 · e1

6!=

e

6!≈ 0.00378.

We also used the fact that the function eξ is a monotonic increasingfunction of ξ.

3. Given that

R(x) =|x|4

4!

(−1

1 + ξ

)for x ∈

[− 1

2,1

2

], where ξ is between x and 0, find and upper bound

for |R|, value for all x ∈[− 1

2,1

2

], that is independent of x and ξ.

Solution: We use the following facts

(i) x ∈[− 1

2,1

2

]⇐⇒ |x| ≤ 1

2

(ii) for 0 < z <1

2, y = z4 is increasing, thus for z1 < z2, we have that

z41 < z4

2 , e.g., z4 <

(1

2

)4

(iii)1

|1 + ξ|for ξ ∈

[−1

2,1

2

]is maximized for ξ that minimizes |1 + ξ|,

i.e. when ξ = −1

2. Thus

|x|4

4!

(−1

1 + ξ

)≤ |x|4

4!|1 + ξ|≤

(12

)44!|1 + ξ|

≤(

12

)44!∣∣1 +

(−1

2

)∣∣ =

(12

)44!1

2

=

(12

)34

=1

192.

3

Page 5: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

4. What is the fourth-order Taylor polynomial for1

x+ 1about x0 = 0?

Solution:

P4(x) = f(0) + xf ′(0) +x2

2!f ′′(0) +

x3

3!f ′′′(0) +

x4

4!f (iv)(0) (1.2)

f(0) = 1

f ′(x) = −(x+ 1)−2 so f ′(0) = −1

f ′′(x) = 2(x+ 1)−3 so f ′′(0) = 2

f ′′′(x) = −6(x+ 1)−4 so f ′′′(0) = −6

f (iv)(x) = 24(x+ 1)−5 so f (iv)(0) = 24

Substituting in (1.2) yields

P4(x) = 1− x+ x2 − x3 + x4.

5. Find the Taylor polynomial of third-order for sin(x) using x0 =π

2.

Solution:

P3(x) = f(π

2

)+(x− π

2

)f ′(π

2

)+

(x− π

2

)22!

f ′′(π

2

)+

(x− π

2

)33!

f ′′′(π

2

)(1.3)

f(π/2) = 1

f ′(x) = cos(x) so f ′(π/2) = 0

f ′′(x) = − sin(x) so f ′′(π/2) = −1

f ′′′(x) = − cos(x) so f ′′′(π/2) = 0

Substituting into (1.3) yields

P3(x) = 1−(x− π

2

)22

.

4

Page 6: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

6. For the function below construct the third-order Taylor polynomial ap-proximation, using x0 = 0, and then estimate the error by computing anupper bound on the remainder, over the given interval: f(x) = ln(1+x),

x ∈[−1

2,1

2

].

Solution:

P4(x) = f(0) + xf ′(0) +x2

2!f ′′(0) +

x3

3!f ′′′(0) +

x4

4!f (iv)(ξ) (1.4)

for ξ between 0 and x.

f(x) = ln(1 + x) so f(0) = ln(1) = 0

f ′(x) =1

1 + xso f ′(0) = 1

f ′′(x) = −(x+ 1)−2 so f ′′(0) = −1

f ′′′(x) = 2(x+ 1)−3 so f ′′′(0) = 2

f (iv)(x) = −6(x+ 1)−4 = − 6

(1 + x)4so f (iv)(ξ) = − 6

(1 + ξ)4

Substituting into (1.4) yields

ln(1 + x) = x− x2

2+x3

3− x4

4(1 + ξ)4.

So P3(x) = x − x2

2+x3

3. For the error bound, first note that x ∈[

−1

2,1

2

]⇐⇒ |x| ≤ 1

2.

|R3(x)| ≤ |x|4

4(1 + ξ)4≤

(12

)44(1 + ξ)4

≤(

12

)44(1 +

(−1

2

))4 ≤(

12

)44(

12

)4 =1

4

7. Find the Taylor Series expansion of f(x) = 3√x of degree 2 about

x0 = 8. Using the remainder term estimate the maximum absoluteerror on the interval [7, 9].

5

Page 7: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution: Calculate

f(x) = x1/3 =⇒ f(8) = 81/3 = 2,

f ′(x) =1

3x−2/3 =⇒ f ′(8) =

1

12,

f ′′(x) = −2

9x−5/3 =⇒ f ′′(8) = − 1

144,

f ′′′(x) =10

27x−8/3.

Thus the 2nd degree polynomial is

T2(x) = f(8) + (x− 8)f ′(8) +(x− 8)2

2!f ′′(8)

= 2 +(x− 8)

12− (x− 8)2

2!

1

144

= 2 +(x− 8)

12− (x− 8)2

288.

Using the Lagrange form of the remainder

R2(x) =(x− 8)3

3!f ′′′(ξ) =

(x− 8)3

3!

10

27ξ−8/3

=5(x− 8)3

81ξ8/3, ξ between x and 8.

I.e., we maximize

|R2(x)| = 5|x− 8|3

81|ξ|8/3, ξ between x and 8.

Now note that as x is between 7 and 9 so is ξ. Thus as 1ξ8/3

is decreasing

on [7, 9], it follows that 1|ξ|8/3 is maximized on [7, 9] by 1

78/3 . Also, powers

of x (or powers of (x− c), for some c) are maximized at the end points.Just think of the general shape of x2 or x3. So |x−8|3 is maximized on[7, 9], at x = 7 or x = 9. Here, we get the same answer for both values.Thus

|R2(x)| ≤ 5|9− 8|3

81 · 78/3or

5|7− 8|3

81 · 78/3

= 29/84238 = 3.4426× 10−4 (4 d.p.).

6

Page 8: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

8. Construct a Taylor polynomial approximation that is accurate to within10−3, over the indicated interval, for the following function, using x0 =0: f(x) = e−x, x ∈ [0, 1].

Solution: As in an example from the lecture notes, we need n such that

|Rn(x)| ≤ 10−3

for all x ∈ [0, 1].∣∣∣∣ xn+1

(n+ 1)!e−ξ∣∣∣∣ =

|x|n+1

(n+ 1)!e−ξ ≤ 1 · e0

(n+ 1)!=

1

(n+ 1)!

Plugging in values of n to make1

(n+ 1)!≤ 10−3, we find that n = 6

is the minimum value to do this (get about 1.984 × 10−4), thus wemust find P6(x). Since f (n)(x) = (−1)ne−x for all n we have thatf (n)(0) = (−1)n for all n. Thus

P6(x) = 1− x+x2

2− x3

3!+x4

4!− x5

5!+x6

6!.

9. For each function below, use the Mean Value Theorem to find a valueM such that

|f(x1)− f(x2)| ≤M |x1 − x2|that is valid for all x1, x2 in the stated interval: (a) f(x) = ln(1 + x)on the interval [−1, 1], (b) f(x) = sin(x) on the interval [0, π].

Solution: From the notes,

|f(x2)− f(x1)| ≤ |f ′(ξ)| · |x2 − x1|.

For (a): [x1, x2] = [−1, 1] and ξ ∈ [−1, 1].

f(x) = ln(1 + x) =⇒ f ′(x) =1

1 + x

=⇒ |f ′(ξ)| =∣∣∣∣ 1

1 + ξ

∣∣∣∣ ≤ ∣∣∣∣ 1

1− 1

∣∣∣∣ =∞

7

Page 9: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

So M does not exist.

For (b): [x1, x2] = [0, π] and ξ ∈ [0, π].

f(x) = sin(x) =⇒ f ′(x) = cos(x)

=⇒ |f ′(ξ)| = | cos(x)| ≤ 1 = M

10. A function is called monotone on an interval if its derivative is strictlypositive or strictly negative on the interval. Suppose f is continuousand monotone on the interval [a, b], and f(a)f(b) < 0; prove that thereis exactly one value α ∈ [a, b] such that f(α) = 0.

Solution: If f is monotone on [a, b] and f(a)f(b) < 0 then either

f(a) < 0 and f(b) > 0 or f(a) > 0 and f(b) < 0.

Thus, as f ∈ C([a, b]), by the Intermediate Value Theorem ∃α ∈ [a, b]such that f(α) = 0.

11. (MATLAB) Use the program GRAPH_PRES to estimate the (true) max-imum absolute error in using the Taylor polynomial of Question 7 toapproximate 3

√x on [7, 9].

Solution: Just plot the exact error given by |T2(x)− 3√x| over the interval

[7, 9]. Visually estimate the maximum absolute error and compare thisvalue with what you estimated was the maximum theoretical error usingR2(x). So from the graph below:

8

Page 10: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

we see that the true maximum error on [7, 9] is about 2.6× 10−4. Thetheoretical maximum error is estimated to be ≤ 3.4 × 10−4, which isnot bad.

12. (MATLAB) Use the program GRAPH_MANY_PRES to graphically investi-gate how well the Taylor series Tn(x) (for n = 1, 2, . . . ) approximatesthe functions f(x) near x = x0 (see Questions 1, 4, 5, 6, 7, 8).

Solution: On a given interval [a, b] containing x0 plot Tn(x), n = 1, 2, . . . ,with y = f(x), and observe how the fit between the Taylor series nearx0 gets better as n increases. An example (see Question 4) is givenbelow:

9

Page 11: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

1.5 Error and Asymptotic Error

1. Use Taylor’s Theorem to show that

√1 + x = 1 +

1

2x+O(x2),

for x sufficiently small.

Solution: x0 = 0 and

f(x) = f(0) + xf ′(0) +x2

2f ′′(0) + ...

= f(0) + xf ′(0) +O(x2) (1.5)

Calculating coefficients:

f(x) = (1 + x)12 =⇒ f(0) = 1

f ′(x) =1

2(1 + x)

12 =⇒ f ′(0) =

1

2

Substituting into (1.5) yields

√1 + x = 1 +

1

2x+O(x2).

10

Page 12: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

2. Use Taylor’s Theorem to show that

ex = 1 + x+O(x2),

for x sufficiently small.

Solution: x0 = 0 and

f(x) = f ′(x) = ex

=⇒ f(0) = f ′(0) = 1

Thus, as in #1, ex = 1 + x+O(x2).

3. Use Taylor’s Theorem to show that

1− cos(x)

x=

1

2x+O(x3),

for x sufficiently small.

Solution: x0 = 0 and

cos(x) = 1− 1

2!x2 +

1

4!x4 − ...

=⇒ 1− cos(x) =1

2!x2 − 1

4!x4 + ...

=⇒ 1− cos(x)

x=

1

2!x− 1

4!x3 + ...

=1

2x+O(x3).

4. Show thatsin(x) = x+O(x3).

Solution: x0 = 0 and

sin(x) = x− 1

3!x3 + ... = x+O(x3).

11

Page 13: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

5. Recall the summation formula

1 + r + r2 + r3 + ...+ rn =n∑k=0

rk =1− rn+1

1− r, r 6= 1.

Use this to prove that for |r| < 1

n∑k=0

rk =1

1− r+O(rn+1),

Hint: What is the definition of O notation?

Solution:n∑k=0

rk =1− rn+1

1− r=

1

1− r− rn+1

1− r, .

Need to show that

rn+1

1− r= O(rn+1), for n large.

Using the definition of O notation and the fact that |r| < 1,

limn→∞

∣∣∣∣ rn+1

1−r

rn+1

∣∣∣∣ = limn→∞

∣∣∣∣ 1

1− r

∣∣∣∣ =1

1− r<∞.

6. Use the above result to show that 9 terms are all that is needed tocompute

S =∞∑k=0

e−k

to within 10−4 absolute accuracy.

Solution:

n∑k=0

rk =1− rn+1

1− r=

1

1− r− rn+1

1− r= S − rn+1

1− r, for |r| < 1.

12

Page 14: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Need to show that∣∣∣∣S − n∑k=0

rk∣∣∣∣ =

∣∣∣∣ rn+1

1− r

∣∣∣∣ =rn+1

1− r< 10−4,

for r =1

eand n = 9.

rn+1

1− r=

e−10

1− e−1= 7.2× 10−5 < 10−4.

7. Recall the summation formula

S =n∑k=1

k =n(n+ 1)

2.

Use this to show that

n∑k=0

k =1

2n2 +O(n).

Solution:n∑k=0

k =n(n− 1)

2=n2

2+n

2=

1

2n2 +O(n).

8. Use the definition of O to show that if y = yh +O(hp), then

hy = hyh +O(hp+1).

Solution: Given y = yh +O(hp), i.e. limh→0

∣∣∣∣y − yphp

∣∣∣∣ = C <∞. But

limh→0

∣∣∣∣y − yhhp

∣∣∣∣ = limh→0

∣∣∣∣hy − hyhhp+1

∣∣∣∣ = C <∞,

i.e. hy = hyh +O(hp+1).

13

Page 15: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

9. Show that if an = O(np) and bn = O(nq) then anbn = O(np+q).

Solution: Given that an = O(np) and bn = O(nq), i.e.

limn→∞

∣∣∣∣annp∣∣∣∣ = C1 <∞, and lim

n→∞

∣∣∣∣ bnnq∣∣∣∣ = C2 <∞,

then

limn→∞

∣∣∣∣anbnnp+q

∣∣∣∣ = limn→∞

∣∣∣∣annp∣∣∣∣ · lim

n→∞

∣∣∣∣ bnnq∣∣∣∣ = C1C2 <∞,

i.e. anbn = O(np+q).

10. (MATLAB) Recall that the infinite (classic) geometric series is

S = 1 + r + r2 + r3 + · · · = 1

1− r, |r| < 1.

In fact we saw in Question 5 that

S = 1 + r + r2 + r3 + . . . rn +O(rn+1),

i.e., the error in using only n + 1 terms in the approximation of Sis O(rn+1). The MATLAB program GEOMETRIC_PRES sums this finite

series for a given r until the absolute error

∣∣∣∣∣S −n∑k=0

rk

∣∣∣∣∣ < tol, where ‘tol’

is a tolerance supplied by the user. Use this program with tol = 1×10−6

and (a) r = 0.001 and (b) r = 0.999. Explain the difference in resultsfor these two cases.

Solution: Running the MATLAB code yields that the number of termsneeded to achieve the required accuracy is 3 for case (a) and 20713for case (b). The series with r = 0.999 clearly converged much moreslowly than the series with r = 0.001 as the error term for the former isO(0.999n+1) while for the latter it isO(0.001n+1), and an order 0.001n+1

term goes to zero much faster than an order 0.999n+1 term as n→∞.

14

Page 16: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

1.6 Computer Arithmetic

1. Perform the indicated computations in each of three ways: (i) Exactly;(ii) Using 3 digit decimal arithmetic, with chopping; (iii) Using 3 digitdecimal arithmetic, with rounding. For both approximations, computethe absolute error and the relative error.

(a)1

6+

1

10(b)

(1

7+

1

10

)+

1

9

Solution:

(a) (i)1

6+

1

10= 0.26

(ii)

fl

(fl

(1

6

)+ fl

(1

10

))= fl(fl(0.16) + fl(0.1))

= fl(0.166 + 0.100)

= fl(0.266)

= 0.266 (chopping)

absolute error = |0.26− 0.266| = 0.0006

relative error =0.0006

0.26= 2.5× 10−3 (2 significant figures)

(iii)

fl

(fl

(1

6

)+ fl

(1

10

))= fl(fl(0.16) + fl(0.1))

= fl(0.167 + 0.100)

= fl(0.267)

= 0.267 (rounding)

absolute error = |0.26− 0.267| = 3.3× 10−4

relative error =0.0003

0.26= 1.25× 10−3 (2 significant figures)

15

Page 17: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

(b) (i)

(1

7+

1

10

)+

1

9= 0.3539682

(ii)

fl

(fl

(1

7

)+ fl

(1

10

))= fl(fl(0.142) + 0.100)

= fl(0.242)

= 0.242

so

fl

(0.142 + fl

(1

9

))= fl(0.142 + 0.111)

= fl(0.353)

= 0.353 (chopping)

absolute error = |0.3539682− 0.353| = 9.682× 10−4

relative error =9.683...× 10−4

0.3539682= 2.735× 10−3 (4 significant figures)

(iii) Rounding obtained via the same method, but as fl

(1

7

)=

0.143, this leads to the answer 0.354. Thus,

absolute error = |0.3539682− 0.354| = 3.175× 10−5

relative error =3.175...× 10−5

0.3539682= 8.969× 10−5 (4 significant figures)

2. For f(x) =ex − 1

x, how many terms in the Taylor expansion are needed

to get single precision accuracy (7 decimal digits) for all x ∈[0,

1

2

]?

How many terms are needed for double precision accuracy (14 decimaldigits) over this same range?

Solution: Recall

ex = 1 + x+x2

2!+ ...+

xn

n!+

xn+1

(n+ 1)!eξ,

16

Page 18: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

for ξ between 0 and x, and x ∈[0,

1

2

]. Rearranging:

ex − 1

x= 1 +

x

2!+x2

3!+ ...+

xn−1

n!+

xn

(n+ 1)!eξ︸ ︷︷ ︸

Rn(x)

.

For single precision (i.e., rounding to t = 7 decimal places) find n suchthat (see your lecture notes for the formula we use)

|Rn(x)| < 5× 10−(t+1) = 5× 10−8,

for all x ∈[0,

1

2

]. Thus

∣∣∣∣ xn

(n+ 1)!eξ∣∣∣∣ =

|x|neξ

(n+ 1)!≤(

12

)ne

12

(n+ 1)!≤ 1.78× 10−8 if n = 8

(and > 5× 10−8 if n = 7). So we need 9 terms or n = 8.

For double precision we need n such that (see your lecture notes forthe formula we use)

|Rn(x)| < 5× 10−(t+1) = 5× 10−15,

where t = 14. Same procedure as before leads to

|Rn(x)| ≤ 2.308× 10−15 if n = 13.

So we need n = 14 terms or n = 13.

3. Using 3−digit decimal arithmetic, find three values a, b, and c, suchthat

(a+ b) + c 6= a+ (b+ c).

Solution: Let a = 1× 10−20, b = 1 and c = −1. Then

(a+ b) + c = (1× 10−20 + 1)− 1,

which with 3−digit decimal arithmetic yields 0. However,

a+ (b+ c) = 1× 10−20 + (1− 1)

with 3−digit decimal arithmetic yields 1× 10−20.

17

Page 19: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

4. Assume we are using 3−digit decimal arithmetic. For ε = 0.0001 anda1 = 5, compute

a2 = a0 +

(1

ε

)a1

for a0 equal to each of 1, 2, and 3. Comment.

Solution:

a0 = 1

fl

(1

0.0001

)= 1× 104

fl(5× 104) = 50000

fl(1 + 5× 104) = fl(50001) = 50000.

a0 = 2

. . . f l(2 + 5× 104) = fl(50002) = 50000

a0 = 3

. . . f l(3 + 5× 104) = fl(50003) = 50000

a2 is the same in all three cases.

5. What is the machine epsilon for a computer that uses binary arith-metic, 24 bits for the fraction, and rounds? What if it chops?

Solution: β = 2 and t = 24: From the lecture notes

δ =1

2β1−t =

1

2× 21−24 = 0.596× 10−7 (rounding)

δ = β1−t = ×21−24 = 0.119× 10−7 (chopping)

6. What is the machine epsilon for a computer that uses binary arithmetic,24 bits for the fraction, and rounds? What if it chops?

18

Page 20: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

7. (MATLAB) Perform the following calculation in MATLAB:

>> 1000.00000000000000005-1000

What does MATLAB give as the answer? What is the exact answer?Explain the discrepancy.

Solution: MATLAB gives 0 as an answer, however the actual answeris 5× 10−17. The discrepancy is due to the fact that MATLAB worksin double precision (rounding numbers to about 16 significant figures)and so MATLAB stores the number 1000.00000000000000005 as 1000.This is a classic problem of loss of significance due to subtracting twonearly equal numbers. To be precise:

fl(fl(1000.00000000000000005)− fl(1000)) = fl(1000− 1000)

= fl(0) = 0.

8. (MATLAB) Perform the following calculation in MATLAB:

>> format long

>> 0.12345678-0.12345677

What does MATLAB give as the answer? What is the exact answer?Explain the discrepancy.

Solution: MATLAB gives 1.000000000861423e − 08 as an answer (i.e.1.000000000861423 × 10−8). The actual answer is of course 1 × 10−8.What is puzzling about this is that both numbers in the calculationonly involve 8 significant figures which one might argue should be storedexactly in MATLAB (as it uses double precision). However, MATLABworks internally in binary arithmetic and even the number 0.1 hasan infinite binary expansion (which is 0.000110011 . . .). Thus loss ofsignificance is again due to finite precision (in the binary system).

19

Page 21: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

9. (MATLAB) Open the MATLAB program EPS_CALC_PRES by typing:

>> open eps_calc_pres

Explain what the program does. If necessary type

>> help while

Also run the program and compare the answer you get with the valueof machine epsilon in MATLAB, obtained by simply entering the com-mands

>> format long

>> eps

Solution: We approximate machine epsilon εM using the definition thatεM is the smallest positive number x such that fl(1 +x) > 1. We startwith the number 1 and keep halving it until the condition is satisfied.The final value is multiplied by 2 as the test 1+x > 1 failed, indicatingthat we cut x in half once to often. When we run the program we getthe output 2.220446049250313e− 16, which is the same answer we getif we type ‘eps’ in MATLAB.

10. (MATLAB) Recall the well-known expression for the number e:

e = limx→∞

(1 +

1

x

)x= 2.718281828459045 . . . .

The program EXP_CALC_PRES uses this result with integer x values toapproximate e. Run this program and explain the pattern in the errors.What is the optimal x value?

Solution: Clearly from the tabulated results the absolute errors decreaseto a minimum value for n (i.e., x) around 108. After that the errorsincrease to a maximum around n ≥ 1016. This is because for n ≥1016 MATLAB calculates 1/n as 0 (due to finite precision), yieldingthe approximation (1 + 1/n)n = 1. Thus the absolute error becomes|e− 1| = 1.718281828459 . . . , which is what we see.

20

Page 22: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Chapter 2

A Survey of Simple Methodsand Tools

2.1 Horner’s Rule

1. Write the following polynomial in nested form: x3 + 3x+ 2.

Solution: 2 + 3x+ x3 = 2 + x(3 + x2).

2. Write the following polynomial in nested form, but this time take advan-tage of the fact that they involve only even powers of x to minimize the

computations: 1− 1

2x2 +

1

24x4.

Solution:

1− 1

2x2 +

1

24x4 = 1 + x2

((−1

2

)+

1

24x2

).

3. Write the following polynomial in nested form: 1− x2 +1

2x4 − 1

6x6.

Solution:

1− x2 +1

2x4 − 1

6x6 = 1 + x2

((−1) +

1

2x2 − 1

6x4

)= 1 + x2

((−1) + x2

((1

2

)−(

1

6

)x2

))21

Page 23: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Note: it takes longer to work out

1 + x

(x

(−1 + x

(x

(1

2− 1

6x2

)))).

4. Consider the polynomial

p(x) = 1 + (x− 1) +1

6(x− 1)(x− 2) +

1

7(x− 1)(x− 2)(x− 4).

This can be written in “nested-like” form by factoring out each binomialterm as far as it will go, thus:

p(x) = 1 + (x− 1)

(1 + (x− 2)

(1

6+

1

7(x− 4)

)).

Write the following polynomial in this kind of nested form:

p(x) = −1+6

7

(x− 1

2

)− 5

21

(x− 1

2

)(x−4)+

1

7

(x− 1

2

)(x−4)(x−2).

Solution:

p(x) = −1 +6

7

(x− 1

2

)− 5

21

(x− 1

2

)(x− 4) +

1

7

(x− 1

2

)(x− 4)(x− 2)

= −1 +

(x− 1

2

)(6

7− 5

21(x− 4) +

1

7(x− 4)(x− 2)

)= −1 +

(x− 1

2

)(6

7+ (x− 4)

((− 5

21

)+

1

7(x− 2)

))

2.2 Difference Approximations

1. Compute, by hand, approximations to f ′(1) for each of the following func-tions, with h = 1/16 using the Forward and Centered Difference Approx-imations: (a) f(x) = arctan(x), (b) f(x) = e−x.

Solution:For (a) f(x) = arctan(x)

22

Page 24: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Forward Difference Approximation

f(x+ h)− f(x)

h=

arctan(1 + 1

16

)− arctan(1)

116

=0.8157− π

4116

= 0.485 ≈ f ′(1)∗∗ (3 decimal places)

Centered Difference Approximation

f(x+ h)− f(x− h)

2h=

arctan(1 + 1

16

)− arctan(1− 1

16)

216

= 0.500 (3 decimal places)...which is better.

Observe that f ′(x) =1

x2 + 1so f ′(1) = 0.5.

For (b) f(x) = e−x

Forward Difference Approximation

f(x+ h)− f(x)

h=

e−(1+ 116) − e−1

116

= −0.356 (3 decimal places)

Centered Difference Approximation

f(x+ h)− f(x− h)

2h=

e−(1+ 116) + e−(1− 1

16)

216

= −0.368 (3 decimal places)...which is better.

Observe that f ′(x) = −e−x so f ′(1) = −e−1 ≈ −0.367 to 3 decimal places.

2. Use the Forward Difference Approximation to approximate f ′(1), the Cen-tered Difference Approximation to approximate f ′(1.1), and the BackwardDifference Approximation to approximate f ′(1.2), using the data in thetable below:

23

Page 25: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

x f(x)

1.00 1.00000000001.10 0.95135076991.20 0.9181687424

Solution: Using h = 0.1Forward Difference Approximation

f(1 + 0.1)− f(1)

0.1=

0.9513...− 1.00

0.1= −0.487 ≈ f ′(1) (3 s.f.)

Centered Difference Approximation

f(1.1 + 0.1)− f(1.1− 0.1)

2(0.1)=

0.91816...− 1.00

0.2= −0.409 ≈ f ′(1.1) (3 s.f.)

Backward Difference Approximation

f(1.2)− f(1.2− 0.1)

0.1=

0.91816...− 0.951350

0.1= −0.332 ≈ f ′(1.2) (3 s.f.)

3. Use the Centered Difference Approximation to the first derivative to provethat this approximation will be exact for any quadratic polynomial.

Solution: Let p(x) = a + bx + cx2 so p′(x) = b + 2cx. Using the centereddifference approximation

p(x+ h)− p(x− h)

2h=

[a+ b(x+ h) + c(x+ h)2]− [a+ b(x− h) + c(x− h)2]

2h

=2bh+ c(x+ h)2 − c(x− h)2

2h

= b+c

2h[(x+ h)2 − (x− h)2]

= b+c

2h[(x2 + 2xh+ h2)− (x2 − 2xh+ h2)]

= b+4cxh

2h= b+ 2cx

= p′(x).

24

Page 26: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

4. Find coefficients A,B, and C so that (a) f ′(x) = Af(x) + Bf(x + h) +Cf(x+ 2h) +O(h2) (hard!). Hint: Use Taylor’s theorem.

Solution:

f(x+ h) = f(x) + hf ′(x) +h2

2!f ′′(x) +

h3

3!f ′′′(c1), (2.1)

for c1 between x and x+ h.

f(x+ 2h) = f(x) + 2hf ′(x) +(2h)2

2!f ′′(x) +

(2h)3

3!f ′′′(c2), (2.2)

for c2 between x and x+ 2h. Now computing 4×(2.1)-(2.2) gives

4f(x+ h)− f(x+ 2h) = 3f(x) + 2hf ′(x) +h3

6[4f ′′′(c1)− 8f ′′′(c2)]

= 3f(x) + 2hf ′(x) +2h3

3[f ′′′(c1)− 2f ′′′(c2)].

So dividing by 2h yields:

2

hf(x+ h)− 1

2hf(x+ 2h) =

3

2hf(x) + f ′(x) +O(h2)

or

f ′(x) = − 3

2hf(x) +

2

hf(x+ h)− 1

2hf(x+ 2h) +O(h2).

Thus A = − 3

2h, B =

2

h, and C = − 1

2h.

5. Use Taylor’s Theorem to show that the approximation

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

12h

is O(h4) (hard!). Again, use Taylor’s theorem.

Solution: Expand to powers of h4 (with remainder term O(h5) as we thendivide by 12h:

f(x+ h) = f(x) + hf ′(x) +h2

2!f ′′(x) +

h3

3!f ′′′(x) +

h4

4!f (iv)(x) +

h5

5!f (v)(c1)

25

Page 27: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

f(x− h) = f(x)− hf ′(x) +h2

2!f ′′(x)− h3

3!f ′′′(x) +

h4

4!f (iv)(x)− h5

5!f (v)(c2)

f(x+ 2h) = f(x) + 2hf ′(x) +(2h)2

2!f ′′(x) +

(2h)3

3!f ′′′(x) +

(2h)4

4!f (iv)(x) +

(2h)5

5!f (v)(c3)

= f(x) + 2hf ′(x) + 2h2f ′′(x) +4h3

3f ′′′(x) +

2h4

3f (iv)(x) +

4h5

15f (v)(c3)

f(x−2h) = f(x)−2hf ′(x)+2h2f ′′(x)−4h3

3f ′′′(x)+

2h4

3f (iv)(x)−4h5

15f (v)(c4)

Thus,

8f(x+ h)− 8f(x− h)− f(x+ 2h) + f(x− 2h) = 16hf ′(x)− 4hf ′(x) + ch5

= 12hf ′(x) + ch5,

where c depends on f (v)(ci) for i = 1, 2, 3, 4. So dividing by 12h yields

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

12h= f ′(x) + ch4,

where c =c

12. Rearranging

f ′(x) =8f(x+ h)− 8f(x− h)− f(x+ 2h) + f(x− 2h)

12h+O(h4).

6. Use the derivative approximation from the previous problem with h = 0.1to evaluate f ′(1.2), using the data in the following table:

x f(x)

1.00 1.00000000001.10 0.95135076991.20 0.91816874241.30 0.89747069631.40 0.8872638175

26

Page 28: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution:

f ′(1.2) =8f(1.2 + 0.1)− 8f(1.2− 0.1)− f(1.2 + 0.2) + f(1.2− 0.2)

12(0.1)

=8f(1.3)− 8f(1.1)− f(1.4) + f(1.0)

1.2

=8(0.89747...)− 8(0.95135...)− (0.88726...) + 1.0

1.2= −0.265 (3 decimal places)

7. Use Taylor expansions for f(x ± h) to derive an O(h2) accurate approx-imation to f ′′(x) using f(x) and f(x ± h). Provide all the details of theerror estimate. Hint: Go out as far as the fourth derivative term, andthen add the two expansions.

Solution: See class notes.

8. (MATLAB) Use the programs FDA_PRES, BDA_PRES and CDA_PRES to in-vestigate how the errors in difference approximations change with a de-crease in the step sizes h. Explain what you see. (Note: the above pro-grams compute approximations to the derivative of a function y = f(x)at some point x0 using the Forward, Backward and Centered DifferenceApproximations respectively.)

Solution: Just run the program and see. They all show pretty much thesame thing so we just show the output for a single example. We lookedat the example of f(x) = ln(x) (entered as log(x) in MATLAB) withx0 = 1. The Forward Difference Approximation was used to approximatethe derivative of this function. N was chosen as 16:

h approx |error|

1/10 0.95310180 0.0468982019567507

1/100 0.99503309 0.0049669146831908

1/1000 0.99950033 0.0004996669165768

1/10000 0.99995000 0.0000499966670269

1/100000 0.99999500 0.0000049999601159

1/1000000 0.99999950 0.0000005000819332

27

Page 29: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

1/10000000 0.99999995 0.0000000494161295

1/100000000 0.99999999 0.0000000110774709

1/1000000000 1.00000008 0.0000000822403710

1/10000000000 1.00000008 0.0000000826903710

1/100000000000 1.00000008 0.0000000827353710

1/1000000000000 1.00008890 0.0000889005818410

1/10000000000000 0.99920072 0.0007992778374091

1/100000000000000 0.99920072 0.0007992778373641

1/1000000000000000 1.11022302 0.1102230246251559

1/10000000000000000 0.00000000 1.0000000000000000

As expected, initially as we decrease h the accuracy of the approximationdecreases until we obtain the least error around h = 10−8, after which theerror begins to rise again due to rounding error.

28

Page 30: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

2.3 Euler’s Method

1. Use Euler’s method with h = 0.25 to compute approximate solutionvalues for the initial value problem

y′ = sin(t+ y), y(0) = 1.

You should eventually get y4 = 1.851566895 (be sure that your calcu-lator is set to radians).

Solution:

yn+1 = yn + h sin(tn + yn)

tn = nh = 0.25n

So {yn+1 = yn = 0.25 sin(0.25n+ yn)y0 = y(0) = 1

n = 0

y1 = y0 + 0.25 sin(0 + y0)

= 1 + 0.25 sin(1)

= 1.2103677462...(≈ y(0.25))

n = 1

y2 = y1 + 0.25 sin(0.25 + y1)

= 1.45884498566...(≈ y(0.5))

n = 2

y3 = y2 + 0.25 sin(0.5 + y2)

= 1.6902572796...(≈ y(0.75))

n = 3

y4 = y3 + 0.25 sin(0.75 + y3)

= 1.85156689533...(≈ y(1))

29

Page 31: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

2. Use Euler’s method with h = 0.25 to compute approximate solutionvalues for

y′ = et−y, y(0) = −1.

What approximate value do you get for y(1) = 0.7353256638?

Solution:

yn+1 = yn + hf(tn, yn)

= yn + 0.25etn−yn

= yn + 0.25e0.25n−yn ,

with y0 = y(0) = −1.

n = 0

y1 = y0 + 0.25e0−y0

= −1 + 0.25e1

= −0.320429542885...(≈ y(0.25))

n = 1

y2 = y1 + 0.25e0.25−y1

= 0.121827147394...(≈ y(0.5))

n = 2

y3 = y2 + 0.25e0.5−y2

= 0.486730952236...(≈ y(0.75))

n = 3

y4 = y3 + 0.25e0.75−y3

= 0.812025139875...(≈ y(1))

3. Repeat the above with h = 0.125. What value do you now get fory8 ≈ y(1)?

30

Page 32: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution:

yn+1 = yn + hf(tn, yn)

= yn + 0.125etn−yn

= yn + 0.125e0.125n−yn ,

with y0 = y(0) = −1.

n = 0

y1 = y0 + 0.125e0−y0

= −0.660214771443...(≈ y(0.125))

n = 1

y2 = y1 + 0.125e0.125−y1

= −0.38610503923...(≈ y(0.25))

n = 2

y3 = y2 + 0.125e0.25−y2

= −0.149966473291...(≈ y(0.375))

n = 3

y4 = y3 + 0.125e0.375−y3

= 0.061333798435...(≈ y(0.5))

n = 4

y5 = y4 + 0.125e0.5−y4

= 0.255163498508...(≈ y(0.625))

n = 5

y6 = y5 + 0.125e0.625−y5

= 0.436100739954...(≈ y(0.75))

31

Page 33: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

n = 6

y7 = y6 + 0.125e0.75−y6

= 0.607194720154...(≈ y(0.875))

n = 7

y8 = y7 + 0.125e0.875−y7

= 0.770581294899...(≈ y(1))

4. (MATLAB) Use the program EULER_PRES to compute approximationsto each of the following initial value1 problems on [0, 1], using M =2, 4, 8, 16 steps.

(i) f(t, y) = −y + sin(t), y(0) = 1,

where y(t) = (3/2) exp(−t) + (sin(t)− cos(t))/2,

(ii) f(t, y) = t− y, y(0) = 2,

where y(t) = 3 exp(−t) + t− 1,

(iii) f(t, y) = exp(t− y), y(0) = −1,

where y(t) = log(exp(t)− 1 + exp(−1)),

(iv) f(t, y) = −y log(y), y(0) = 3,

where y(t) = exp(log(3) exp(−t)).

Where is the maximum error on [0, 1] for each value of h = 1/M? Howdoes the approximation change as h is reduced?

Solution: Just run the program and see. In general we expect themaximum error to be close to t = 1 and the errors to decrease as h→ 0.However, as we saw in our lecture notes Euler’s Method converges quiteslowly. An example of the program output with the graph is given forexample (iii) below with h = 1/4:

t exact approx |error|

0.000 -1.000000 -1.000000 0.000000

0.250 -0.427857 -0.320430 0.107427

0.500 0.016464 0.121827 0.105363

0.750 0.395334 0.486731 0.091397

1.000 0.735326 0.812025 0.076699

1In MATLAB log means natural log.

32

Page 34: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

2.4 Linear Interpolation

1. Use linear interpolation to find an approximation to erf(0.34), wheref(x) = erf(x) is the error function, using the data in the following ta-ble:

x f(x)

0.3 0.328626759459130.4 0.42839235504667

Also, give an upper bound on the error in the approximation.

Solution: Choose x0 = 0.3 and x1 = 0.4 so that [a, b] = [0.3, 0.4].

P1(x) =

(x− x1

x0 − x1

)f(x0) +

(x− x0

x1 − x0

)f(x1).

With x = 0.34,

P1(0.34) = 0.6(0.3286...)+0.4(0.4283...) = 0.3685 (4 significant figures).

33

Page 35: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

For the error in the approximation we need the 2nd derivative of f(x) =erf(x), which is given by

f ′′(x) = − 4x√πe−x

2

.

Without resorting to a graphical approach this is difficult to bound. Acrude upper bound is

|f ′′(x)| ≤ 4(0.4)√πe−(0.3)2 = 0.8250087277,

where we have bounded usingmax (numerator)

min (denominator)and the fact that y1 = x

is increasing on [0.3, 0.4] and y2 = e−x2

is decreasing on [0.3, 0.4]. Thusfrom the error bound formula

|f(x)− P1(x)| ≤ M

8(x1 − x0)

2

=0.250087277

8(0.4− 0.3)2

= 0.001031 (4 significant figures),

where M = max |f ′′(x)| for all x ∈ [x0, x1].

Check: using erf(x) =2√π

∫ x

0

e−t2

dt on my TI-89 graphing calculator I

get

|f(x)− P1(x)| =

∣∣∣∣ 2√π

∫ 0.34

0

e−t2

dt− 0.3685329977

∣∣∣∣= |0.369364529345...− 0.3685329...|= 0.000831...,

consistent with our result above.

2. The gamma function, denoted by Γ(x), occurs in a number of applications,most notably probability theory and the solution of certain differentialequations. It is basically the generalization of the factorial function tonon-integer values, in that Γ(n + 1) = n!. The table below gives valuesof Γ(x) for x between 1 and 2. Use linear interpolation to approximateΓ(1.005) = 0.99713853525101....

34

Page 36: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

x Γ(x)

1.00 1.00000000001.10 0.95135076991.20 0.91816874241.30 0.89747069631.40 0.88726381751.50 0.88622692551.60 0.89351534931.70 0.90863873291.80 0.93138377101.90 0.96176583192.00 1.000000000

Solution: Choose x0 = 1.00 and x1 = 1.10. Set f(x) = Γ(x).

P1(x) =

(x− x1

x0 − x1

)f(x0) +

(x− x0

x1 − x0

)f(x1).

With x = 1.005

P1(1.005) = 0.95(1.00)+0.05(0.9513507699) = 0.9976 (4 significant figures).

3. Construct a linear interpolating polynomial to the function f(x) = x−1

using x0 = 1/2 and x1 = 1 as nodes. What is the upper bound on theerror over the interval [1/2, 1], according to the error estimate?

Solution:

P1(x) =

(x− x1

x0 − x1

)f(x0) +

(x− x0

x1 − x0

)f(x1)

=

(x− 112− 1

)(1

2

)−1

+

(x− 1

2

1− 12

)1−1

= 3− 2x.

For the error bound we need f ′′(x):

f ′(x) = −x−2 and f ′′(x) = 2x−3 =2

x3.

35

Page 37: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

We need an upper bound for f ′′(x) on

[1

2, 1

]. Noting that x3 is increasing

on

[1

2, 1

]=⇒ 2

x3is decreasing on

[1

2, 1

]and positive throughout, thus

|f ′′(x)| = 2|x−3| ≤ 2

∣∣∣∣∣(

1

2

)−3∣∣∣∣∣ = 2

∣∣(2−1)−3∣∣ = 2(8) = 16 = M.

So using the error bound formula

|f(x)− P1(x)| ≤ M

8(x1 − x0)

2 =16

8

(1− 1

2

)2

= 2

(1

4

)=

1

2.

4. Repeat the above for f(x) = x13 , using the interval [1/8, 1].

Solution:

P1(x) =

(x− x1

x0 − x1

)f(x0) +

(x− x0

x1 − x0

)f(x1)

=

(x− 118− 1

)(1

8

) 13

+

(x− 1

8

1− 18

)1

13

=4

7x+

3

7or

1

7(4x+ 3).

Now

f ′(x) =1

3x−

23 and f ′′(x) = −2

9x−

53 = − 2

9x53

.

Where is |f ′′(x)| a maximum on

[1

8, 1

]? Now (positive) powers of x and

their reciprocals are maximized or minimized at one of the end-points ofthe interval. Thus the absolute values of these functions are maximizedat one of the end-points. To find which end-point leads to a maximum wecheck each and see.

f ′′(

1

8

)= −7.1 and f ′′(1) = −0.2

36

Page 38: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

=⇒ |f ′′(x)| ≤ 7.1 = M . Thus from the error bound formula

|f(x)− P1(x)| ≤ M

8(x1 − x0)

2 =7.1

8

(1− 1

8

)2

= 0.6806 (4 significant figures)

Note: alternatively, to find the maximum value of |f ′′| we could haveargued as follows. Because we took absolute values we can ignore thesign of f ′′. As x5/2 is increasing, f ′′ is decreasing, and so the function ismaximized at the left end-point. See the graph below:

5. (MATLAB) Apply the program INTEROL_PRES to Question 3.

Solution: Running the program with a = 1/2, b = 1 with 2 nodes yields:

37

Page 39: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

2.5 Trapezoid Rule

1. Apply the trapezoid rule with h =1

8, to approximate the integral

I =

∫ 1

0

1√1 + x4

dx = 0.92703733865....

How small does h have to be to get an error less that 10−3? 10−6?

Solution:

f(x) =1√

1 + x4, h =

1

8, [a, b] = [0, 1], n =

b− ah

=1− 0

18

= 8.

38

Page 40: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

i xi f(xi)

0 0 11 1

80.99987...

2 14

0.99805...3 3

80.99025...

4 12

0.97014...5 5

80.93145...

6 34

0.87157...7 7

80.79400...

8 1 0.70710...

T8(f) =1

2h(f(x0) + 2f(x1) + ...+ 2f(x7) + f(x8))

=

(1

2

)(1

8

)(1 + 2(0.99987...) + 2(0.99805...) + ...+ 2(0.79400...) + 0.70710...)

= 0.926115180158...

In order to estimate the error we need the 2nd derivative of f(x) =

(1 + x4)−12 .

f ′(x) = −1

2(1 + x4)−

32 · 4x3 = −2x3(1 + x4)−

32 .

Using the product rule we can show that

f ′′(x) =6x2(x4 − 1)

(x4 + 1)52

.

This is a complicated function to get an upper bound on. We woulduse a combination of calculus and curve sketching techniques to deducethe following diagram:

39

Page 41: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Alternatively, you could get an upper bound by maximizing and min-imizing the numerator and denominator in f ′′ respectively. (Note: Iwouldn’t give you one as involved as this in our tests, but the principleis still important.) Now recall from lecture notes that to get an error≤ 10−3 we require

|I(f)− Tn(f)| ≤ b− a12

h2M =h2

12(1.39275) ≤ 10−3, (2.3)

where M = max |f ′′(x)| for all x ∈ [a, b] (as indicated in the diagram).Rearranging, we require

h2 ≤ 0.08616047... or h ≤ 0.0928227...

But h must divide b− a = 1, so as

n =b− ah

or h =b− an

=1

n≤ 0.0928227...

=⇒ n ≥ 10.77

40

Page 42: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

so choose n = 11 =⇒ h =1

n= 0.09. As in (2.3) we seek h such that

h2

12(1.39275) ≤ 10−6 or h2 ≤ 8.616047×10−6 so h ≤ 0.0029353104.

As before

h =1

n≤ 0.0029353104... =⇒ n ≥ 340.679...

so with n = 341, =⇒ h =1

n= 0.00293255132...

2. Use the trapezoid rule with h =π

4, to approximate the integral

I =

∫ π2

0

sin(x) dx = 1.

How small does h have to be to get an error less that 10−3? 10−6?

Solution:

f(x) = sin(x), h =π

4, [a, b] =

[0,π

2

], n =

b− ah

=π2− 0π4

= 2.

i xi f(xi)

0 0 0

1 π4

1√2

2 π2

1

T2(f) =1

2h(f(x0) + 2f(x1) + f(x2))

=

(1

2

)(π4

)(0 +

2√2

+ 1

)=

(√2 + 1

8

)π ≈ 0.948059...

41

Page 43: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

(Note: exact answer is 1.)In order to estimate the error we need the 2nd derivative of f(x) =sin(x).

f ′(x) = cos(x) and f ′′(x) = − sin(x)

Thus |f ′′(x)| = | sin(x)| ≤ 1 = M on[0,π

2

].

From the error bound formula we need

|I(f)− Tn(f)| ≤ b− a12

h2M =π2

12h2 · 1 =

π

24h2 ≤ 10−3 (2.4)

=⇒ h2 ≤ 0.007639437... so h ≤ 0.08740387...

But h must divide b − a =π

2, so noting that h =

b− an

=π2

n≤

0.08740387...=⇒ n ≥ 17.97.

So choose n = 18 =⇒ h =π2

n= 0.087266....

From (2.4) we also seek h such that

π

24h2 ≤ 10−6

=⇒ h2 ≤ 7.639...× 10−6

=⇒ h ≤ 0.002763953...

As before

h =π2

n≤ 0.002763953... =⇒ n ≥ 568.315...

Choosing n = 569 =⇒ h =π2

569= 0.0027606...

3. Apply the trapezoid rule with h =1

8, to approximate the integral

I =

∫ 1

0

x(1− x2) dx =1

4.

How small does h have to be to get an error less than 10−3? 10−6?

42

Page 44: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution:

f(x) = x(1−x2), h =1

8, [a, b] = [0, 1], n =

b− ah

=1− 0

18

= 8.

i xi f(xi)

0 0 01 1

863512

2 14

1564

3 38

165512

4 12

38

5 58

195512

6 34

2164

7 78

105512

8 1 0

T8(f) =1

2h(f(x0) + 2f(x1) + ...+ 2f(x7) + f(x8))

=

(1

2

)(1

8

)(0 + 2

(63

512

)+ 2

(15

64

)+ ...+ 2

(105

512

)+ 0)

=63

256= 0.2461 (4 significant figures)(

Note: exact answer is1

4

). In order to estimate the error we need the

2nd derivative of f(x) = x(1− x2).

f ′(x) = 1− 3x2 =⇒ f ′′(x) = −6x.

So |f ′′(x)| = 6|x| ≤ 6 = M on [0, 1]. From the error bound formula weneed h such that

|I(f)− Tn(f)| ≤ b− a12

h2M =h2

12(6) ≤ 10−3, (2.5)

Rearranging, we require

h2 ≤ 0.002 or h ≤ 0.0447213595...

43

Page 45: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

But h must divide b− a = 1, so as

h =b− an

=1

n≤ 0.0447213595...

=⇒ n ≥ 22.36...

so choose n = 23 =⇒ h =1

23= 0.04347826. For an error less than

10−6 we need h such that

|I(f)− Tn(f)| ≤ b− a12

h2M =h2

12(6) ≤ 10−6

Rearranging, we require

h2 ≤ 2× 10−6 or h ≤ 0.00141421356...

As before

h =1

n≤ 0.00141421356... =⇒ n ≥ 707.106...

so with n = 708, =⇒ h =1

708= 0.00141242937...

4. Apply the trapezoid rule with h = 18, to approximate the integral

I =

∫ 1

0

ln(1 + x) dx = 2 ln(2)− 1.

How small does h have to be to get an error less than 10−3? 10−6?

Solution:

f(x) = ln(1+x), h =1

8, [a, b] = [0, 1], n =

b− ah

=2− 1

18

= 8.

i xi f(xi)

0 0 01 1

8ln(

98

)2 1

4ln(

54

)3 3

8ln(

118

)4 1

2ln(

32

)5 5

8ln(

138

)6 3

4ln(

74

)7 7

8ln(

158

)8 1 ln(2)

44

Page 46: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

T8(f) =1

2h(f(x0) + 2f(x1) + ...+ 2f(x7) + f(x8))

=

(1

2

)(1

8

)(0 + 2 ln

(9

8

)+ 2 ln

(5

4

)+ ...+ 2 ln

(15

8

)+ ln(2)

)= 0.3856 (4 significant figures)

(Note: exact answer is 0.3863 to 4 significant figures). In order toestimate the error we need the 2nd derivative of f(x) = ln(1 + x).

f ′(x) =1

1 + x=⇒ f ′′(x) = − 1

(1 + x)2.

So |f ′′(x)| =1

(1 + x)2≤ 1

(1 + 0)2= 1 = M on [0, 1]. From the error

bound formula we need h such that

|I(f)− Tn(f)| ≤ b− a12

h2M =h2

12(1) ≤ 10−3, (2.6)

Rearranging, we require

h2 ≤ 0.012 or h ≤ 0.10954451150...

But h must divide b− a = 1, so as

h =b− an

=1

n≤ 0.10954451150...

=⇒ n ≥ 9.12...

so choose n = 10 =⇒ h =1

10= 0.1. For an error less than 10−6 we

need h such that

|I(f)− Tn(f)| ≤ b− a12

h2M =h2

12(1) ≤ 10−6

Rearranging, we require

h2 ≤ 1.2× 10−5 or h ≤ 0.003464101615...

As before

h =1

n≤ 0.003464101615... =⇒ n ≥ 288.67...

so with n = 289, =⇒ h =1

289= 0.0034602076...

45

Page 47: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

5. (MATLAB) Apply the program TRAPEZOID_PRES to numerically verifyany of the above results. For example, what is the actual error incurredfor Question 3 when using h = 1/23?

Solution: Running the program yields that the approximate area is0.249483. The exact answer is 1/4, thus the error is about |1/4 −0.249483| = 5.17× 10−4, which is consistent with the theoretical result(|error| ≤ 10−3). The approximation is illustrated below:

2.6 Solution of Tridiagonal Linear Systems

1. Use the tridiagonal algorithm to solve the following system of equations:4 2 0 01 4 1 00 1 4 10 0 2 4

x1

x2

x3

x4

=

π9√3

2√3

2

−π9

46

Page 48: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution:4 2 0 01 4 1 00 1 4 10 0 2 4

∣∣∣∣∣∣∣∣π9√3

2√3

2

−π9

r2 − 14r1 → r2

4 2 0 00 7

21 0

0 1 4 10 0 2 4

∣∣∣∣∣∣∣∣π9√

32− π

36√3

2

−π9

r3 − 27r2 → r3

4 2 0 00 7

21 0

0 0 267

10 0 2 4

∣∣∣∣∣∣∣∣π9√

32− π

365√

314

+ π126

−π9

r4 − 713r3 → r4

4 2 0 00 7

21 0

0 0 267

10 0 0 45

13

∣∣∣∣∣∣∣∣π9√

32− π

36= 0.778759

5√

314

+ π126

= 0.643523

−5√

326− 3π

26= −0.695578

(each to 6 significant figures). Keeping 6 significant figures for theright-hand side values yields the following associated linear system (seelecture notes):

4x1 + 2x2 = π9

72x2 + x3 = 0.778759

267x3 + x4 = 0.643523

4513x4 = −0.695578

Back substitution yields:

x1 = 0.008495 x2 = 0.1575 x3 = 0.2274 x4 = −0.2009

(each to 4 significant figures).

2. Use the tridiagonal algorithm to solve the following system of equations:1 1

20 0

12

13

14

00 1

415

16

0 0 16

17

x1

x2

x3

x4

=

2253301514

47

Page 49: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

.

Solution:1 1

20 0

12

13

14

00 1

415

16

0 0 16

17

∣∣∣∣∣∣∣∣2253301514

r2 − 12r1 → r2

1 1

20 0

0 112

14

00 1

415

16

0 0 16

17

∣∣∣∣∣∣∣∣2153301514

r3 − 3r2 → r3

1 1

20 0

0 112

14

00 0 −11

2016

0 0 16

17

∣∣∣∣∣∣∣∣21−37

301514

r4 + 1033r3 → r4

1 1

20 0

0 112

14

00 0 −11

2016

0 0 0 134693

∣∣∣∣∣∣∣∣21−37

309671386

The associated linear system is:

x1 + 12x2 = 2

112x2 + 1

4x3 = 1

−1120x3 + 1

6x4 = −37

30134693x4 = 967

1386

Back substitution yields:

x1 = 1.004 x2 = 1.993 x3 = 3.336 x4 = 3.608

(each to 4 significant figures).

3. Is the following system diagonally dominant? Use the triadiagonalalgorithm to find the solution.

12

1021

0 014

13

113

00 1

514

121

0 0 15

16

x1

x2

x3

x4

=

61421791565634201310

48

Page 50: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution: Checking for diagonal dominance:

0.5 =1

2>

10

21= 0.4761...X

0.3 =1

3>

1

4+

1

13=

17

52= 0.3269...X

0.25 =1

4>

1

5+

1

21=

26

105= 0.247...X

0.16 =1

6<

1

5...×

Hence the system is not diagonally dominant. Nevertheless, Gaussianelimination may still work:

12

1021

0 014

13

113

00 1

514

121

0 0 15

16

∣∣∣∣∣∣∣∣61421791565634201310

r2 − 12r1 → r2

12

1021

0 00 2

21113

00 1

514

121

0 0 15

16

∣∣∣∣∣∣∣∣61421152735634201310

r3 − 2110r2 → r3

12

1021

0 00 2

21113

00 0 23

260121

0 0 15

16

∣∣∣∣∣∣∣∣6142115273248954601310

r4 − 5223r3 → r4

12

1021

0 00 2

21113

00 0 23

260121

0 0 0 19322

∣∣∣∣∣∣∣∣61421152732489546013014830

The associated linear system is:

12x1 + 10

21x2 = 61

42221x2 + 1

13x3 = 115

27323260x3 + 1

21x4 = 2489

546019322x4 = 1301

4830

Back substitution yields:

x1 = 0.7661 x2 = 2.246 x3 = 2.696 x4 = 4.565

(each to 4 significant figures).

49

Page 51: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

4. (MATLAB) Use MATLAB to verify any of the solutions you have foundby using the ‘backslash’ command. That is, with the system Ax = b tofind x just type >> A\b. See any tutorial to see how to enter vectorsand matrices in MATLAB.

Solution: The MATLAB commands for Question 2 are given below:

>> A=[1,1/2,0,0;1/2,1/3,1/4,0;0,1/4,1/5,1/6;0,0,1/6,1/7]

A =

1 1/2 0 0

1/2 1/3 1/4 0

0 1/4 1/5 1/6

0 0 1/6 1/7

>> b=[2;2;53/30;15/14]

b =

2

2

53/30

15/14

>> A\b

ans =

1.0037

1.9925

3.3358

3.6082

5. (MATLAB) We can use some simple commands in MATLAB to imple-ment the individual row operations in the tridiagonal algorithm. Thecommands applied to a hypothetical augmented matrix B are as follows(ri refers to the ith row of B and k is a nonzero scalar):

50

Page 52: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

To do kri → ri (multiply row i by k) type>> B(i,:) = k*B(i,:)

To do ri − krj → ri (subtract k times row j from row i) type>> B(i,:) = B(i,:) - k*B(j,:)

To do ri ↔ rj (swap rows i and j) type>> B([i,j],:) = B([j,i],:)

Solution: The MATLAB commands for the first step of Question 2 aregiven below (I am assuming you have already constructed the aug-mented matrix called B - see previous solution):

>> format rat % Comment: so calculations done with fractions

>> B(2,:)=B(2,:)-(1/2)*B(1,:)

B =

1 1/2 0 0 2

0 1/12 1/4 0 1

0 1/4 1/5 1/6 53/30

0 0 1/6 1/7 15/14

2.7 Boundary Value Problems

1. Solve, by hand, the following two-point BVP:

−u′′ + u = f(x), x ∈ [0, 1],

u(0) = u(1) = 0,

where f(x) = x, using h = 1/4. Write out the linear system explicitlyprior to a solution. You should get the following 3× 3 system: 2.0625 -1 0

-1 2.0625 -10 -1 2.0625

U1

U2

U3

=

0.0156250.031250.046875

51

Page 53: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution: {−u′′ + u = x, 0 ≤ x ≤ 1u(0) = u(1) = 0

With uk ≈ u(xk) and xk = kh, we replace the above BVP with{−uk−1−2uk+uk+1

h2 + uk = xk,u0 = un = 0

Multiplying the DE through by h2 and simplifying yields:

−uk−1 + (2 + h2)uk − uk+1 = h2xk (2.7)

which with h =1

4, so that n = 1

14

= 4 yields

−uk−1 + 3316uk − uk+1 =

(14

)2 (14k)

=(

14

)3k,

u0 = u4 = 0, for k = 1, 2, 3.

(2.8)

(2.8) leads to 3 linear equations:

k = 1

−u0 +33

16u1 − u2 =

(1

4

)3

=⇒ 33

16u1 − u2 =

1

64(2.9)

k = 2

−u1 +33

16u2 − u3 =

(1

4

)3

(2) =1

32(2.10)

52

Page 54: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

k = 3

−u2 +33

16u3 =

3

64(2.11)

Writing (2.9),(2.10) and (2.11) as a single matrix equation: 3316−1 0

−1 3316−1

0 −1 3316

u1

u2

u3

=

164132364

(Note: 33/16 = 2.0625). Gaussian elimination and back substitutionyield u1

u2

u3

=

266762565

115468

1359

=

0.03490.05630.0500

to 4 decimal places.

2. Repeat the above problem, but this time using h = 1/5. What doesthe new system become?

Solution: −uk−1 + 5125uk − uk+1 =

(15

)2 (15k)

=(

15

)3k,

u0 = u5 = 0, for k = 1, 2, 3, 4.

(2.12)

k = 1

−u0 +51

25u1 − u2 =

(1

5

)3

=⇒ 51

25u1 − u2 =

1

125(2.13)

k = 2

−u1 +51

25u2 − u3 =

(1

5

)3

(2) =2

125(2.14)

k = 3

−u2 +51

25u3 =

(1

5

)3

(3) =3

125(2.15)

53

Page 55: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

k = 4

−u3 +51

25u4 − u5 =

(1

5

)3

(4) =⇒ −u3 +51

25u4 =

4

125(2.16)

Writing (2.13)-(2.16) as a single matrix equation:5125−1 0 0

−1 5125−1 0

0 −1 5125−1

0 0 −1 5125

u1

u2

u3

u4

=

1

1252

1253

1254

125

3. Approximate the solution of the two-point BVP

−u′′ + u′ + u = 1, x ∈ [0, 1],

u(0) = u(1) = 0.

Assuming h < 2 determine if the resulting coefficient matrix is diag-onally dominant? Hint : For the approximation of the 2nd derivativesee your lecture notes 1.

Solution: {−u′′ + u′ + u = 1, x ∈ [0, 1]u(0) = u(1) = 0

with uk ≈ u(xk) and xk = hk, we replace the above BVP with{−(uk−1−2uk+uk+1

h2

)+(uk+1−uk−1

2h

)+ uk = 1,

u0 = un = 0.

Multiplying the DE by h2 yields

−uk−1 + 2uk − uk+1 +h

2(uk+1 − uk−1) + h2uk = h2,

or {−(h2

+ 1)uk−1 + (2 + h2)uk +

(h2− 1)uk+1 = h2

u0 = un = 0, k = 1, 2, ..., n− 1

1Note: the problem of determining diagonal dominance is harder than I would giveyou in an exam, but shows you the sort of calculations one might have to do in practice.However, the initial part is fair game!

54

Page 56: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

As k runs from 1 to n−1, we get the following tridiagonal linear systemin matrix form, after using in the first and last equation that u0 = 0and un = 0, respectively:

2 + h2 h2− 1 0

−(h2

+ 1)

2 + h2 h2− 1

0 −(h2

+ 1)

2 + h2

. . . . . . . . .

−(h2

+ 1)

2 + h2 h2− 1

0 −(h2

+ 1)

2 + h2

u1

u2

u3

u4

u5

u6

=

h2

h2

h2

h2

h2

h2

Is the coefficient matrix diagonally dominant?

First equation: Is |2 + h2| >∣∣∣∣h2 − 1

∣∣∣∣?Recall the triangle inequality, namely

|a+ b| ≤ |a|+ |b|,

so ∣∣∣∣h2 − 1

∣∣∣∣ =

∣∣∣∣h2 + (−1)

∣∣∣∣ ≤ h

2+ 1 < 2 < 2 + h2 = |2 + h2| X

(We used the fact that h < 2 by assumption.)Middle equations: Is |2 + h2| ≥

∣∣− (h2

+ 1)∣∣ +

∣∣h2− 1∣∣? First observe

thath

2− 1 < 0 as h < 2, so

∣∣∣∣h2 − 1

∣∣∣∣ = −(h

2− 1

)= 1 − h

2, after

recalling that

|x| ={

x, x ≥ 0−x, x < 0

So∣∣∣∣−(h2 + 1

)∣∣∣∣+∣∣∣∣h2 − 1

∣∣∣∣ =

∣∣∣∣(h2 + 1

)∣∣∣∣+∣∣∣∣h2 − 1

∣∣∣∣ =h

2+1+1−h

2= 2 < 2+h2 = |2+h2| X

Last equation: Is |2 + h2| >∣∣∣∣−(h2 + 1

)∣∣∣∣?∣∣∣∣−(h2 + 1

)∣∣∣∣ =

∣∣∣∣h2 + 1

∣∣∣∣ =h

2+ 1 < 2 < 2 + h2 = |2 + h2| X

The coefficient matrix is diagonally dominant for all h.

55

Page 57: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

4. (MATLAB) Use the program BVP_SIMPLE_PRES to numerically solvethe BVP

−u′′ + u = f(x), x ∈ [0, 1]

u(0) = u(1) = 0,

where f and the exact solutions u are given by

(a) f(x) = (π2 + 1) sin(πx),u(x) = sin(πx);

(b) f(x) = 4 exp(−x)− 4x exp(−x),u(x) = x(1− x) exp(−x);

(c) f(x) = π(π sin(πx) + 2 cos(πx)) exp(−x),u(x) = exp(−x) sin(πx);

(d) f(x) = 3− 1/x− (x2 − x− 2) ln(x),u(x) = x(1− x) ln(x).

Using h = 1/2n where n = 2, 3, 4, . . . numerically investigate the ac-curacy of the approximations. Recall that with n equal subintervalswe have the approximations uk ≈ u(xk), k = 1, 2, . . . , n − 1 where thenodes are xk = hk, h = 1/n.

As a measure of the accuracy choose the error to be

E(h) := maxk|u(xk)− uk|, k = 1, 2, . . . , n− 1.

Solution: We used the MATLAB program to solve problem (a). Theoutput and graph are displayed below for n = 4, i.e. h = 1/4:

56

Page 58: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

xk Approx Exact |Approx - Exact|

0.000000 0.000000 0.000000 0.000000

0.250000 0.740989 0.707107 0.033882

0.500000 1.047917 1.000000 0.047917

0.750000 0.740989 0.707107 0.033882

1.000000 0.000000 0.000000 0.000000

Clearly the maximum error is (to 6 d.p.) 0.047917. We ran the programagain with h = 1/8, h = 1/16 and h = 1/32 and summarized ourfindings in the table below. We have also shown the ratio of consecutive

h E(h) E(h)/E(h/2)

1/4 0,047917 4.07891/8 0.011745 4.01951/16 0.002922 4.00271/32 0.000730 −−−

errors. From lecture notes we know that with the assumption thatE(h) = Chp (and so E(h)/E(h/2) = hp) the data suggests that hp = 4i.e. p = 2. That is we have shown numerically that E(h) = O(h2) (asexpected).

57

Page 59: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Chapter 3

Root - Finding

3.1 Bisection Method

1. Do three iterations of the bisection method, applied to f(x) = x3 − 2using a = 0 and b = 2.

Solution:f(0) = −2f(2) = 8− 2 = 6

}so by the IMVT a root of f exists in [0, 2]. Set x1 =

0 + 2

2= 1.

f(1) = 13 − 2 = −1f(2) = 8− 2 = 6

}

so by the IMVT a root of f exists in [1, 2]. Set x2 =1 + 2

2= 1.5.

f(1.5) = (1.5)3 − 2 = 1.375f(1) = −1

}

so by the IMVT a root of f exists in [1, 1.5]. Set x3 =1 + 1.5

2= 1.25.

2. (a) How many iterations of the bisection method are needed to findthe root of f(x) = x− e−x2

on [0, 1] to an accuracy of 0.1? (b) Applythe method to find the root to the specified accuracy. (c) If the actual

58

Page 60: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

root is 0.6529186... verify that your root does indeed have the requiredaccuracy.

Solution:

(a) Need k such that

|α− xk| ≤b− a

2k< 0.1 =⇒ 1− 0

2k< 0.1

=⇒ 1 < (0.1)2k

=⇒ ln(1) < ln(0.1) + k ln(2)

=⇒ k > − ln(0.1)

ln(2)= 3.321...

Thus we need 4 iterations (bisections) to achieve the desired accu-racy.

(b)f(0) = 0− e0 = −1f(1) = 0.632...

}so by the IMVT a root of f exists in [0, 1]. Set x1 =

0 + 1

2= 0.5.

f(0.5) = −0.278...f(1) = 0.632...

}

so by the IMVT a root of f exists in [0.5, 1]. Set x2 =0.5 + 1

2=

0.75.f(0.75) = 0.180...f(0.5) = −0.278...

}so by the IMVT a root of f exists in [0.5, 0.75]. Set x3 =

0.75 + 0.5

2=

0.625.f(0.625) = −0.0516...f(0.75) = 0.180...

}so by the IMVT a root of f exists in [0.625, 0.75]. Set x4 =0.625 + 0.75

2= 0.6875.

59

Page 61: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

(c) The actual root is 0.6529186... so the absolute error is:

|0.6529186...− 0.6875| = 0.0345... < 0.1.X

3. (MATLAB) Use the program BISECT_PRES to apply the bisection methodto the following test problems:

(a) f(x) = 2− exp(x) on [0, 1],

(b) f(x) = x3 − 2 on [0, 2],

(c) f(x) = 5− 1/x on [0.1, 0.25],

(d) f(x) = x2 − sin(x) on [0.5, 1].

Can you check the exact roots of the problems with the answers givenby the program? Use an appropriate error bound to check a priori theminimum number of steps needed for the roots to be accurate within10−6.

Solution: We show output for problem (a). The iterations and graphof how xn varies with increasing n is shown below:

x1 = 0.5000000000000000

x2 = 0.7500000000000000

x3 = 0.6250000000000000

etc.

x19 = 0.6931476593017578

x20 = 0.6931467056274414

After 20 iterations the root is approximately 0.6931467056274414 with an

|error|< 1.000000e-06

60

Page 62: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

The root is clearly worked out to be ln(2). With a tolerance of 10−6

we require (see your lecture notes)

b− a2n≤ 10−6.

With [a, b] = [0, 1] we solve for n yielding n ≥ ln(106)/ ln(2) = 19.9316 . . ..Thus the minimum number of steps is 20. Similar results are obtainedfor the other examples. The roots are easily found (exactly on paper)for problems (b) and (c), to be 3

√2 and 1/5 respectively. The root for

problem (d) cannot be found exactly on paper.

3.2 Newton’s Method

1. (a) Write down Newton’s method applied to the function f(x) = x3−2.Simplify the computation as much as possible. (b) What has been ac-complished if we find the root of this function?

Solution:

(a)f(x) = x3 − 2 =⇒ f ′(x) = 3x2.

61

Page 63: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Thus

xn+1 = xn −f(xn)

f ′(xn)

= xn −(x3

n − 2)

3x2n

=2(x3

n + 1)

3x2n

,

where x0 is given.

(b) If we find a root of f we have found the cube root of 2 (do you seewhy?).

2. (a) Write down Newton’s method applied to the function f(x) = a− x−1.Simplify the resulting computation as much as possible. (b) What hasbeen accomplished if we find the root of this function?

Solution:

(a)

f(x) = a− x−1 = a− 1

x=⇒ f ′(x) = x−2 =

1

x2.

Thus

xn+1 = xn −f(xn)

f ′(xn)

= xn −(a− 1

xn)

1x2n

= xn(2− axn),

where x0 is given.

(b) A root of f corresponds to a =1

x, or x =

1

a, a 6= 0.

3. (a) Do three iterations of Newton’s method for f(x) = 3 − ex, usingx0 = 1. Repeat, with x0 = 2, 4, 8, 16, but using as many iterations as

62

Page 64: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

needed in order to see a pattern. (b) Comment on your results.

Solution:

(a)f(x) = 3− ex =⇒ f ′(x) = −ex.

Thus

xn+1 = xn −f(xn)

f ′(xn)

= xn −(

3− exn−exn

)= ((xn − 1)exn + 3) e−xn ,

x0 = 1

x1 =((1− 1)e1 + 3

)e−1

= 3e−1

= 1.103...

x2 = 1.098...

x3 = 1.0986122...

x0 = 2

x1 =((2− 1)e2 + 3

)e−2

= 1.406...

x2 = 1.141...

x3 = 1.0995133...

x0 = 4

x1 =((4− 1)e4 + 3

)e−4

= 3.054...

x2 = 2.196...

x3 = 1.529...

63

Page 65: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

We need to go further to see the pattern:

x4 = 1.179...

x5 = 1.101...

x6 = 1.098617...

x0 = 8

x1 =((8− 1)e8 + 3

)e−8

= 7.00...

x2 = 6.00...

x3 = 5.011...

We need to go further to see the pattern:

x4 = 4.031...

x5 = 3.084...

x6 = 2.22...

x7 = 1.546...

x8 = 1.185...

x9 = 1.102...

x10 = 1.09861...

x0 = 16

x1 =((16− 1)e16 + 3

)e−16

= 15.00...

x2 = 14.00.00......

...

x18 = 1.09861...

(b) The actual root is ln(3) = 1.09861228... (convince yourself of this!).For this particular problem, all chosen starting values converge tothe root, but the further the initial guess is from the root the moreiterations are needed to achieve a given accuracy. However, ingeneral we must be sufficiently close to the root for the iterativeprocedure to converge.

64

Page 66: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

4. Figure 3.1 shows the geometry of a planetary orbit around the sun. Theposition of the sun is given by S, the position of the planet is givenby P . Let x denote the angle defined by P0OA, measured in radians.The dotted line is a circle concentric to the ellipse and having a radiusequal to the major axis of the ellipse. Let T be the total orbital periodof the planet, and let t be the time required for the planet to go fromA to P . Then Kepler’s equation from orbital mechanics, relating x andt, is

x− ε sin(x) =2πt

T.

Here ε is the eccentricity of the elliptical orbit (the extent to which itdeviates from a circle). For an orbit of eccentricity ε = 0.01 (roughlyequivalent to that of the Earth), what is the value of x corresponding to

t =T

4? What is the value of x corresponding to t =

T

8? Use Newton’s

method to solve the required equation.

Figure 3.1: Orbital geometry

65

Page 67: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution: ε = 0.01 and t =T

4leads to

x− 0.01 sin(x) =2π T

4

T=π

2.

Set f(x) = x− 0.01 sin(x)− π2, then

f(0) = −π2< 0

f(π) = π − 0− π2

= π2> 0

}so by the IMVT a root α ∈ [0, π]. Set x0 =

π

2. Newton’s method is:

xn+1 = xn −f(xn)

f ′(xn)

= xn −(xn − 0.01 sin(xn)− π

2

)(1− 0.01 cos(xn))

=xn cos(xn)− sin(xn)− 50π

cos(xn)− 100

Iterating with x0 =π

2:

n = 0 x1 = 1.5807963267...

n = 1 x2 = 1.5807958268...

n = 2 x3 = 1.5807958268...

The method for t =T

8is done in the same way.

5. (MATLAB) Use the program NEWTON_RAPH_PRES to apply Newton’smethod to the following test problems:

(a) f(x) = x2 − 4 on [1, 6.5], x0 = 6, tol = 1e− 06;

(b) f(x) = (x− 2)2 on [1.5, 3.2], x0 = 3, tol = 1e− 03;

(c) f(x) = 6(x− 2)5 on [2, 3], x0 = 3, tol = 1e− 03;

(d) f(x) = (4/3) exp((2− x/2)(1 + ln(x)/x)) on [0, 20], x0 = 2.5,f ′(x) = (4/3) exp((2− x/2)(1 + ln(x)/x))×((2− x/2)(1− ln(x))/x2 − (1 + ln(x)/2)/2);

66

Page 68: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

(e) f(x) = x3 − 2x+ 2 on [−0.5, 1.5], x0 = 0;

(f) f(x) = 1− x2 on [−1, 1], x0 = 0.

The number ‘tol’ refers to the tolerance that the program uses in it’sstopping criterion for Newton’s method (default is 10−6). In each case:(i) explain the behaviour of Newton’s method that you see, and (ii)for those cases where we have convergence, explain how the rate ofconvergence is related to the multiplicity of the roots.

Solution:

(i): We have convergence for problems (a) - (c). In case (d) we havedivergence because the iterates move further and further away awayfrom the root (eventually becoming unbounded). In case (e) we havedivergence because the iterates simple oscillate between 0 and 1. Inproblem (f) the initial guess lands on the graph where the slope ishorizontal (thus the tangent line never intersects with the x-axis). InNewton’s method we attempt to divide by zero, which is impossible.

(ii): In case (a) we have the simple roots ±2. Thus from a Theoremin our lecture notes we know that provided the initial guess is chosensufficiently close to the root the method is a quadratically convergentmethod (i.e., the number of correct decimal places we obtain in theiterates is approximately doubled every step). We see this in the dataoutput from the Matlab program (see below). In case (b) we have adouble root (+2, twice) and so we expect a rate of convergence lessthan quadratic (which is verified if you look at the digits in the iteratesoutput by the Matlab program). In Case (c) we have a root (+2) ofmultiplicity 5, thus we expect an even slower rate of convergence. Weshow the Matlab out put for problem (a) below:

Iteration x0 is 6.0000000000000000

Iteration x1 is 3.3333333333333335

Iteration x2 is 2.2666666666666666

Iteration x3 is 2.0156862745098039

Iteration x4 is 2.0000610360875868

Iteration x5 is 2.0000000009313226

Iteration x6 is 2.0000000000000000

67

Page 69: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Iterations stop because |f(x6)|+|x6-x5|<0.000001

3.3 How to Stop Newton’s Method

1. Approximate the root of f(x) = ex + x using Newton’s Method. Stopthe method using an appropriate Stopping Criterion with a toleranceof 10−4. (see your lecture notes). (Hint: to start, show using the IMVTthat there is a root in [−1, 0].)

Solution: Following the hint,

f(0) = e0 + 0 = 1,

f(−1) = e−1 − 1 = −0.632 . . . ,

thus by the IMVT there is a root in the interval [−1, 0]. Hence it is

68

Page 70: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

sensible to choose our initial guess as x0 = −0.51. Newton’s Method is

xn+1 = xn −f(xn)

f ′(xn)= xn −

(exn + xn)

(exn + 1)

=exn(xn − 1)

exn + 1, (3.1)

after simplifying. We start iterating with (3.1) as follows

x1 =ex0(x0 − 1)

ex0 + 1= −0.566311 . . . ,

x2 =ex1(x1 − 1)

ex1 + 1= −0.567143165034862 . . . ,

x3 =ex2(x2 − 1)

ex2 + 1= −0.567143290409781 . . . , etc,

to fill in the the table of values:

n xn |xn − xn−1| |f(xn)|0 −0.5 — 0.106 . . .1 −0.566311 . . . 0.066 . . . 0.0013 . . .2 −0.567143165034862 . . . 8.321 · · · × 10−4 1.96 · · · × 10−7

3 −0.567143290409781 . . . 1.253 · · · × 10−7 4.551 · · · × 10−15

Clearly after three iterations we have

|f(x3)|+ |x3 − x2| < 10−4

as required, thus we stop iterating.

2. (MATLAB) Use the program NEWTON_RAPH_PRES to check your writtenanswer in Problem 1.

Solution: Just run the Matlab program with the data used in the solu-tion of Problem 1 and a tolerance of 10−4.

1Of course this is just one step of the Bisection Method

69

Page 71: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

3.4 Secant Method

1. Do three steps of the secant method for f(x) = x3 − 2, using x0 = 0,x1 = 1.

Solution: We approximate the cube root of 2 since f(x) = 0 =⇒ x =3√

2. The secant method is:

xn+1 = xn − f(xn)

(xn − xn−1

f(xn)− f(xn−1)

)= xn − (x3

n − 2)

(xn − xn−1

(x3n − 2)− (x3

n−1 − 2)

)= xn − (x3

n − 2)

(xn − xn−1

x3n − x3

n−1

)(3.2)

Using (3.2) to do apply the Bisection Method is not a good idea. Ifxn is very close to xn−1 then both the numerator and denominator inthe fractional part will lead to loss of significance (see the section onComputer Arithmetic). So we will rearrange this expression to avoidthe problem. Now

b3 − a3

b− a= b2 + ab+ a2,

so with b = xn and a = xn−1 we have

x3n − x3

n−1

xn − xn−1

= x2n + xnxn−1 + x2

n−1

soxn − xn−1

x3n − x3

n−1

=1

x2n + xnxn−1 + x2

n−1

.

Substituting this into (3.2) yields:

xn+1 = xn −(x3

n − 2)

x2n + xnxn−1 + x2

n−1

(3.3)

Ideally, we would do some further rearranging to get a single expressionthat avoids the (x3

n − 2) term. This is because once we get very closeto the root x3

n will be very nearly equal to 2, again leading to loss ofsignificance. However, unless we are needing a VERY accurate answerthis shouldn’t make much of a difference. Doing 3 steps of (3.3) (withx0 = 0 and x1 = 1) yields

70

Page 72: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

n = 1

x2 = x1 −(x3

1 − 2)

(x21 + x0x1 + x2

0)

= 1− (13 − 2)

(12 + (0)(1) + 02)= 2

n = 2

x3 = x2 −(x3

2 − 2)

(x22 + x1x2 + x2

1)

= 2− (23 − 2)

(22 + (1)(2) + 12)

=8

7or 1.14286 (6 significant figures)

n = 3

x4 = x3 −(x3

3 − 2)

(x23 + x2x3 + x2

2)

=8

7−

((87

)3 − 2)

((87

)2+(

87

)(2) + 22

)=

75

62or 1.20968 (6 significant figures)

Note: 3√

2 ≈ 1.259921...

2. Repeat the above using x0 = 1, x1 = 0. Comment on how the algo-rithm is performing compared to the last example.

Solution: Using the same method we get

n = 1 x2 = 2

n = 2 x3 =1

2

n = 3 x4 =6

7or 0.857143 (6 significant figures)

71

Page 73: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

The method appears to be converging slower with x0 = 1 and x1 = 0than with x0 = 0 and x1 = 1.

3. (MATLAB) Use the program SECANT_PRES to apply the Secant methodto the following test problems:

(a) f(x) = x2 − 4 on [2, 6], x0 = 6, x1 = 5.5, tol = 1e− 03;

(b) f(x) = (x− 2)2 on [2, 3], x0 = 3, x1 = 2.7, tol = 1e− 06;

(c) f(x) = 6(x− 2)5 on [2, 3], x0 = 3, x1 = 2.95, tol = 1e− 03;

(d) f(x) = x2 − 4 on [−3, 3], x0 = −1, x1 = 1;

(e) f(x) = (4/3) exp((2 − x/2)(1 + ln(x)/x)) on [2, 20], x0 = 2.5,x1 = 3.5.

The number ‘tol’ refers to the tolerance that the program uses in it’sstopping criterion for the Secant method (default is 10−6). In eachcase: (i) explain the behaviour of the Secant method that you see, and(ii) for those cases where we have convergence, explain how the rate ofconvergence is related to the multiplicity of the roots.

Solution:(i): We have convergence for problems (a) - (c). In case (d) we havedivergence because the slope of the first secant line is horizontal, andthus never intersects with the x-axis (we divide by zero in the formula).In case (e) we have divergence because the iterates move further andfurther away from the root (becoming unbounded as x tends to infinity).

(ii): Although we don’t cover this in the lecture notes, the convergencetheorem for the Secant Method is almost the same as for Newton’sMethod. The difference being that the optimal rate of convergence forthe Secant Method is a little less than 2, but bigger than 1 (‘super-linear’). It’s interesting that this optimal rate of convergence is equalto the Golden Mean (namely, (1 +

√5)/2 ≈ 1.62). When you run the

program you should notice that the rate of convergence for problem (a)is faster than for either problem (b) or (c) (as problem (a) has simpleroots, while problems (b) and (c) have roots of multiplicity 2 and 5

72

Page 74: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

respectively). We show (some of) the Matlab output for problem (e)below (program interupted):

Iteration x1 is 2.5000000000000000

Iteration x2 is 3.5000000000000000

Iteration x2 is 4.5156679582822132

Iteration x3 is 5.5514402736656114

Iteration x4 is 6.6339592833128247

Iteration x5 is 7.7514171147633677

Iteration x6 is 8.9020232758648721

Iteration x7 is 10.0794196823085791

Iteration x8 is 11.2797803442435374

Iteration x9 is 12.4992630213283906

Iteration x10 is 13.7349726174479319

Iteration x11 is 14.9844470660306381

Iteration x12 is 16.2457144330682333

Operation terminated by user during secant_pres (line 99)

73

Page 75: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

3.5 Fixed Point Iteration

1. Do three steps of the following fixed point iteration problem:

xn+1 = ln(1 + xn), x0 = 12.

Solution:

n = 0

x1 = ln

(1 +

1

2

)= 0.405465108108...

n = 1

x2 = ln(1 + 0.405465108108...)

= 0.340368285804...

n = 2

x3 = ln(1 + 0.340368285804...)

= 0.292944416354...

2. Let y0 and h = 1/8 be fixed numbers. Do three steps of the followingfixed point iteration

yn+1 = y0 +1

2h(−y0 ln(y0)− yn ln(yn)), y0 = 1/2.

Solution: With y0 =1

2and h =

1

8this becomes

yn+1 =1

2+

1

16

(−1

2ln

(1

2

)− yn ln(yn)

).

Iterating

74

Page 76: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

n = 0

y1 =1

2+

1

16

(−1

2ln

(1

2

)− y0 ln(y0)

)=

1

2+

1

16

(−1

2ln

(1

2

)− 1

2ln

(1

2

))=

1

2+

1

16ln

(1

2

)= 0.543321698785...

n = 1

y2 =1

2+

1

16

(−1

2ln

(1

2

)− y1 ln(y1)

)= 0.542376812253...

n = 2

y3 =1

2+

1

16

(−1

2ln

(1

2

)− y2 ln(y2)

)= 0.54239978931...

3. Consider the fixed point iteration xn+1 = 1 + e−xn . Show that thisiteration converges for any x0 ∈ [1, 2]. How many iterations does thetheory predict it will take to achieve 10−5 accuracy?

Solution: Observe that with g(x) = 1 + e−x

g(1) = 1.367... ∈ [1, 2],

g(2) = 1.135... ∈ [1, 2],

and as g(x) is monotonically decreasing on [1, 2] we know that 1 ≤ g(x) ≤ 2for all x ∈ [1, 2], i.e. g : [1, 2] ⊂ [1, 2]. To show the second condition ofthe fixed point theorem (see your lecture notes) consider

|g′(x)| = |e−x| ≤ |e−1| = 0.367 < 1,

for all x ∈ (1, 2), as e−x is decreasing. Thus by the fixed point theoremwe see that the iteration converges for all x0 ∈ [1, 2]. We apply theerror bound formula

|xn − α| ≤(

Ln

1− L

)(x1 − x0),

75

Page 77: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

where L =1

e= 0.367... and

x0 = 1.5

x1 = 1 + e−x0

= 1 + e−1.5

= 1.22313016015...,

so we seek n such that( (1e

)n1− 1

e

)(1.2231...− 1.5) < 10−5

or e−n < 2.2830965...× 10−5

so − n < ln(2.2830965...× 105)

or n > 10.687..

so take n = 11.

4. For each function listed below, find an interval [a, b] such that g([a, b]) ⊂[a, b]. Draw a graph of y = g(x) and y = x over this interval, and con-firm that a fixed point exists there. Estimate (by eye) the value ofthe fixed point, and use this as a starting value for a fixed point iter-

ation. Does the iteration converge? Explain: (a) g(x) =1

2

(x+

2

x

),

(b) g(x) = 1 + e−x.

Solution:(a) Not straightforward. To clarify things we need to do some curvesketching. Notice first that we can work the fixed points out exactly,as

g(x) =1

2x+

1

x= x =⇒ 1

x=

1

2x or 2 = x2 or x = ±

√2.

Focus on the fixed point at x =√

2. Notice g(√

2) =√

2 tells us that(√

2,√

2) is on the graph of g. Now

g′(x) =1

2− 1

x2= 0 =⇒ x = ±

√2

76

Page 78: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

are critical points. Maxima, minima, or inflection points? Look at thesecond derivative.

g′′(x) =2

x3=⇒ g′′(

√2) =

2

232

> 0

so (√

(2),√

2) is a local minimum. Notice also that

limx→+∞

g(x) = +∞ and limx→−∞

g(x) = −∞.

This information leads to the following graph:

Since g(1) = 1.5 = g(2), thus√

2 ≤ g(x) ≤ 1.5 for all x ∈ [1, 2]

1 ≤ g(x) ≤ 1.5 for all x ∈ [1, 2]

Thus, condition (i) in the key fixed point theorem is satisfied with[a, b] = [1, 2]. To satisfy the second condition we need to show that

|g′(x)| < 1 for all x ∈ (1, 2). Now g′(x) =1

2− 1

x2, so

|g′(1)| = 0.5 and g′(2) = 0.25,

77

Page 79: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

and as g has a local minimum at x =√

2 ≈ 1.414, the slope of g, i.e.g′(x), must be less than 1 throughout (1, 2) X

(b) g(x) = 1 + e−x. Now

g(x) = x =⇒ 1 + e−x = x or e−x = x− 1

illustrated below:

So visually we see that the fixed point is between 1 and 2. Now

g(1) = 1 + e−1 ≈ 1.136...g(2) = 1 + e−2 ≈ 1.135...

}and as g(x) is decreasing,

1 ≤ g(x) ≤ 2 for all x ∈ [1, 2] X

78

Page 80: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

i.e. g : [1, 2] ⊂ [1, 2] and so condition (i) in the fixed point theoremis satisfied. To satisfy condition (ii) in this theorem, consider g′(x) =−e−x so |g′(x)| = e−x, which again is a decreasing function. And as

|g′(1)| = e−1 ≈ 0.367...|g′(2)| = e−2 ≈ 0.135...

}=⇒ |g′(x)| < 1 for all x ∈ (1, 2) X

5. Let h(x) = 1− x2/4. Show that this function has a root at x = α = 2.Using x0 = 1/2, apply fixed point iteration with xn+1 = h(xn) to ap-proximate the fixed point of h. Comment on your results.

Solution: Consider the iterative process

xn+1 = 1− x2n

4, x0 =

1

2.

n = 0 : x1 = 1− x20

4= 0.9375

n = 1 : x2 = 1− x21

4= 0.780273...

n = 2 : x3 = 1− x22

4= 0.847793...

n = 3 : x4 = 1− x23

4= 0.820311...

n = 4 : x5 = 1− x24

4= 0.83177...

......

n = 29 : x30 = 1− x229

4= 0.8284271...

Can we check our answer? The fixed points of h(x), i.e., the roots of

h(x) = 1− x2

4= x, correspond to the roots of

x2+4x−4 = 0 so x =−4±

√32

2(usual quadratic formula).

Just keeping the positive solution yields x = 0.8284271... X

79

Page 81: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

6. (MATLAB) Use the program FIXED_POINT_ITER_PRES to apply FixedPoint iteration to the following iteration functions:

(a) g(x) = sin(x) on [0, 2.5], x0 = 2, tol = 1e− 03;

(b) g(x) = 1 + 2/x on [0, 3.5], x0 = 1, tol = 1e− 03;

(c) g(x) = x2/4 + x/2 on [2, 20], x0 = 2.3.

In each case (i) work out by hand (if possible) the fixed points, (ii)explain what you see, and state whether the iterative process convergesor diverges.

Solution:(i): (a) solving sin(x) = x yields α = 0 (just recall the graph of sin(x) on[0, π]), (b) solving 1+2/x = x yields (after application of the quadraticformula) that α = 2 (α = −1 is not in the specified interval); solvingx2/4 + x/2 = x yields α = 2 (α = 0 is not in the specified interval).

(ii): (a) We see a ‘stair’ shaped diagram. Convergence is quite slow. Atbest fixed point iteration is linearly convergent. (b) We see a ‘cobweb’shaped diagram. The iterates are convergent. (c) We see a ‘stair’shaped diagram. The iterates are diverging from the root. We givesome MATLAB output for problem (a).

Iteration x0 is 2.000000

Iteration x1 is 0.9092974268256817

Iteration x2 is 0.7890723435728884

Iteration x3 is 0.7097000402345258

Iteration x4 is 0.6516062636498291

etc ..

Iteration x15 is 0.4008516296070669

Iteration x16 is 0.3902026038089464

Iteration x17 is 0.3803757974172909

Iterations stop because |g(x16)|-x16|<0.010000

80

Page 82: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

81

Page 83: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Chapter 4

Interpolation andApproximation

4.1 Lagrange Interpolation

1. Find the polynomial of degree 2 that interpolates y = x3 at the nodesx0 = 0, x1 = 1 and x2 = 2. Plot y = x3 and the interpolating polyno-mial over the interval [0, 2].

Solution: To find P2(x) first find the Lagrange basis functions:

L0(x) =(x− x1)(x− x2)

(x0 − x1)(x0 − x2)=

(x− 1)(x− 2)

(0− 1)(0− 2)=

1

2(x− 1)(x− 2),

L1(x) =(x− x0)(x− x2)

(x1 − x0)(x1 − x2)=

(x− 0)(x− 2)

(1− 0)(1− 2)= −x(x− 2),

L2(x) =(x− x0)(x− x1)

(x2 − x0)(x2 − x1)=

(x− 0)(x− 1)

(2− 0)(2− 1)=

1

2x(x− 1).

Thus,

P2(x) = f(x0)L0(x) + f(x1)L1(x) + f(x2)L2(x)

= −x(x− 2) + (8)

(1

2

)x(x− 1)

= x(3x− 2)

The sketch is difficult to draw as both graphs match well over thespecified interval:

82

Page 84: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

2. Construct the quadratic polynomial that interpolates y =√x at the

nodes x0 = 1/4, x1 = 9/16, and x2 = 1.

Solution: To find P2(x) first find the Lagrange basis functions:

L0(x) =(x− x1)(x− x2)

(x0 − x1)(x0 − x2)=

(x− 9

16

)(x− 1)(

14− 9

16

) (14− 1) =

65

15

(x− 9

16

)(x− 1),

L1(x) =(x− x0)(x− x2)

(x1 − x0)(x1 − x2)=

(x− 1

4

)(x− 1)(

916− 1

4

) (916− 1) = −256

35

(x− 1

4

)(x− 1),

L2(x) =(x− x0)(x− x1)

(x2 − x0)(x2 − x1)=

(x− 1

4

) (x− 9

16

)(1− 1

4

) (1− 9

16

) =64

21

(x− 1

4

)(x− 9

16

).

83

Page 85: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Thus,

P2(x) = f(x0)L0(x) + f(x1)L1(x) + f(x2)L2(x)

=

(1

2

)(64

15

)(x− 9

16

)(x− 1) +

(3

4

)(−256

35

)(x− 1

4

)(x− 1)

+(1)

(64

21

)(x− 1

4

)(x− 9

16

)= after much tedious simplification

=

(− 32

105

)x2 +

(22

21

)x+

9

35.

3. Find the polynomial of degree 3 that interpolates y = x3 at the nodesx0 = 0, x1 = 1, x2 = 2, and x3 = 3. (Simplify your interpolatingpolynomial as much as possible.) Hint: This is easy if you think aboutthe implications of the uniqueness of the interpolating polynomial.

Solution: From our lecture notes we know that for distinct nodes, theinterpolating polynomial is unique. Thus the cubic polynomial thatinterpolates the cubic x3 at x0 = 0, x1 = 1, x2 = 2 and x3 = 3 must bex3! We will do the calculation nonetheless.

L0(x) =(x− x1)(x− x2)(x− x3)

(x0 − x1)(x0 − x2)(x0 − x3)=

(x− 1)(x− 2)(x− 3)

(0− 1)(0− 2)(0− 3)

= −1

6(x− 1)(x− 2)(x− 3),

L1(x) =(x− x0)(x− x2)(x− x3)

(x1 − x0)(x1 − x2)(x1 − x3)=

(x− 0)(x− 2)(x− 3)

(1− 0)(1− 2)(1− 3)

=1

2x(x− 2)(x− 3),

L2(x) =(x− x0)(x− x1)(x− x3)

(x2 − x0)(x2 − x1)(x2 − x3)=

(x− 0)(x− 1)(x− 3)

(2− 0)(2− 1)(2− 3)

= −1

2x(x− 1)(x− 3),

L3(x) =(x− x0)(x− x1)(x− x2)

(x3 − x0)(x3 − x1)(x3 − x2)=

(x− 0)(x− 1)(x− 2)

(3− 0)(3− 1)(3− 2)

=1

6x(x− 1)(x− 2).

84

Page 86: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Thus, after setting f(x) = x3

P3(x) = f(x0)L0(x) + f(x1)L1(x) + f(x2)L2(x) + f(x3)L3(x)

= 0 +1

2x(x− 2)(x− 3) + (8)

(−1

2

)x(x− 1)(x− 3)

+ (27)

(1

6

)x(x− 1)(x− 2)

= x3 (after much simplification).

4. What is the error in quadratic interpolation to f(x) =√x, using

equally spaced nodes on the interval [1/4, 1]?

Solution: From lecture notes we have

|f(x)− P2(x)| ≤ h3

9√

3M,

where M = maxx∈(x0,x2)

|f ′′′(x)|. Here f(x) =√x and

h =1− 1

4

2=

3

4· 1

2=

3

8,

so

x0 =1

4, x1 =

1

4+

3

8=

5

8, x2 =

5

8+

3

8= 1.

Also

f(x) = x12 =⇒ f ′(x) =

1

2x−

12

=⇒ f ′′(x) = −1

4x−

32

=⇒ f ′′′(x) =3

8x−

52 ,

which is a decreasing function on

[1

4, 1

]. Thus

|f ′′′(x)| ≤ 3

8

(1

4

)− 52

=

(3

8

)4

52 =

(3

8

)(32) = 12 = M,

85

Page 87: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

for all x ∈[

1

4, 1

]. Thus

|f(x)−P2(x)| ≤(

38

)39√

3(12) =

3√

3

128= 0.0406 (3 significant figures).

5. Repeat the above for f(x) = x−1 on

[1

2, 1

].

Solution: Proceed as in the problem above.

f(x) = x−1 and h =1− 1

2

2=

1

4, so

x0 =1

2, x1 =

3

4, x2 = 1.

Also

f(x) = x−1 =⇒ f ′(x) = −x−2

=⇒ f ′′(x) = 2x−3

=⇒ f ′′′(x) = −6x−4.

So

|f ′′′(x)| = 6

|x|4= 96 = M, for all x ∈

[1

2, 1

].

Thus using the error estimate given in the last problem we have

|f(x)− P2(x)| ≤(

14

)39√

3(96) =

√3

18= 0.0962 (3 significant figures).

6. (MATLAB) Use the program LAGRANGE_PRES to construct and plotthe Lagrange interpolant Pn(x) for the following functions f(x) with nnodes on the specified intervals [a, b]:

(a) f(x) = exp(x) on [−2, 2] with n = 2, 3, 4;

(b) f(x) = x3 on [0, 2] with n = 2, 3, 4;

(c) f(x) = tan(x) on [−1.5, 1.5] with n = 4, 5, 6, 7, 8;

86

Page 88: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

(d) f(x) = 1/(1 + 25x2) on [−1, 1] with n = 4, 8, 16, 32.

We can measure the fit between f(x) and the interpolating polynomialPn(x) using the maximum norm

‖f − Pn‖∞ := maxx∈[a,b]

|f(x)− Pn(x)|,

which is just the maximum absolute error between f and Pn for all x ∈[a, b]. We say that the sequence of Lagrange interpolation polynomialsPn converges to f if ‖f − Pn‖∞ → 0 as n → ∞. For each problemabove decide in which cases the Lagrange interpolation polynomialsare convergent to the given functions. If the Lagrange polynomials aredivergent explain why based on the plots.

Solution:Running the MATLAB program reveals that we have convergence forcases (a) - (c), as measured by the maximum norm (just assess whetherthe maximum absolute error on [a, b] tends to zero as n → ∞). How-ever, in case (d) we have divergence. As we increase the degree ofthe interpolating polynomial the maximum error increases (near theend points of the domain). This is a famous problem illustrating non-convergence called ‘Runge’s phenomena’. So not every function canbe adequately approximated using Lagrange interpolation. The ‘fix’for this problem is to either use piecewise Lagrange interpolation (e.g.,piecewise linear interpolation), or to relax the assumption that thenodes are equally spaced (in some clever way!). We show the plot fromthe MATLAB program for Problem (c) with n = 5 below:

87

Page 89: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

The program also outputs the Lagrange polynomial in traditional al-gebraic form, namely

P (x) = 4.83486x3 − 1.47748x.

Note: the highest power shown here is x3 because the coefficient of x4

was either zero or almost zero.

4.2 Hermite Interpolation

1. Construct the cubic Hermite interpolant to f(x) =√

1 + x using thenodes a = 0 and b = 1. Plot the interpolant and the function together.Where is the approximation error the greatest?

88

Page 90: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Solution: f(x) = (1 + x)12 =⇒ f ′(x) =

1

2(1 + x)−

12 . Then

L1(x) =x− x2

x1 − x2

=x− 1

0− 1= 1− x

L2(x) =x− x1

x2 − x1

=x− 0

1− 0= x

with L′1(x) = −1 and L′2(x) = 1. Now

h1(x) = [1− 2(x− x1)L′1(x1)]L

21(x)

= [1− 2(x− 0)(−1)](1− x)2

= (1 + 2x)(1− x)2 (4.1)

h2(x) = [1− 2(x− x2)L′2(x2)]L

22(x)

= [1− 2(x− 1)(1)]x2

= (3− 2x)x2 (4.2)

h1(x) = (x− x1)L21

= (x− 0)(1− x)2

= x(1− x)2 (4.3)

h2(x) = (x− x2)L22(x)

= (x− 1)x2 (4.4)

Using (4.1)-(4.4) we have

H(x) = f(x1)h1(x) + f ′(x1)h1(x) + f(x2)h2(x) + f ′(x2)h2(x)

= 112 (1 + 2x)(1− x)2 +

1

2x(1− x)2 + 2

12 (3− 2x)x2 +

1

22−

12 (x− 1)x2

= (after simplifying)

= 0.0251x3 − 0.111x2 + 0.5x+ 1 (coefficients to ≤ 3 significant figures)

Based on the graph (see below) we see that the error is greatest at theend of the interval.

89

Page 91: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Figure 4.1: Graph of f(x) and the interpolant

90

Page 92: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

2. Construct the cubic Hermite interpolant to f(x) = sin(x) using thenodes a = 0 and b = π. Plot the error between the interpolant and thefunction. Where is the error greatest?

Solution: f(x) = sin(x) =⇒ f ′(x) = cos(x). Then

L1(x) =x− x2

x1 − x2

=x− π0− π

=π − xπ

L2(x) =x− x1

x2 − x1

=x− 0

π − 0=x

π

with L′1(x) = − 1

πand L′2(x) =

1

π. Now

h1(x) = [1− 2(x− x1)L′1(x1)]L

21(x)

=

[1− 2(x− 0)

(− 1

π

)](π − x)2

π2

=

(1 +

2

πx

)(π − x)2

π2(4.5)

h2(x) = [1− 2(x− x2)L′2(x2)]L

22(x)

=

[1− 2(x− π)

(1

π

)]x2

π2

=

(3− 2

πx

)x2

π2(4.6)

h1(x) = (x− x1)L21

= (x− 0)(π − x)2

π2

= x(π − x)2

π2(4.7)

h2(x) = (x− x2)L22(x)

= (x− π)x2

π2(4.8)

91

Page 93: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Using (4.5)-(4.8) we have

H(x) = f(x1)h1(x) + f ′(x1)h1(x) + f(x2)h2(x) + f ′(x2)h2(x)

= 0 + 1x(π − x)2

π2+ 0

(3− 2

πx

)x2

π2+ (−1)(x− π)

x2

π2

= (after simplifying)

= 0x3 −(

1

π

)x2 + x+ 0

The maximum error occurs near the centre of the interval.

Figure 4.2: Error between f(x) and the interpolant

92

Page 94: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

3. Construct the cubic Hermite interpolatant to f(x) = x13 on the interval[

1

8, 1

]. What is the maximum error as predicted by theory?

Solution: f(x) = x13 =⇒ f ′(x) =

1

3x−

23 . Then

L1(x) =x− x2

x1 − x2

=x− 118− 1

=x− 1

−78

=8

7(1− x)

L2(x) =x− x1

x2 − x1

=x− 1

8

1− 18

=8

7

(x− 1

8

)

with L′1(x) = −8

7and L′2(x) =

8

7. Now

h1(x) = [1− 2(x− x1)L′1(x1)]L

21(x)

=

[1− 2

(x− 1

8

)(−8

7

)](8

7

)2

(1− x)2

=

(16

7x+

5

7

)(64

49

)(1− x)2 (4.9)

h2(x) = [1− 2(x− x2)L′2(x2)]L

22(x)

=

[1− 2(x− 1)

(8

7

)](8

7

)2(x− 1

8

)2

=

(23

7− 16

7x

)(64

49

)(x− 1

8

)2

(4.10)

h1(x) = (x− x1)L21

=

(x− 1

8

)(8

7

)(1− x)2 (4.11)

h2(x) = (x− x2)L22(x)

= (x− 1)

(64

49

)(x− 1

8

)2

(4.12)

93

Page 95: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

Using (4.9)-(4.12) we have

H(x) = f(x1)h1(x) + f ′(x1)h1(x) + f(x2)h2(x) + f ′(x2)h2(x)

=

(1

8

) 13(

16x

7+

5

7

)(64

49

)(1− x)2

+

(1

3

)(1

8

)− 23(x− 1

8

)(8

7

)(1− x)2

+ (113 )

(23

7− 16

7x

)(64

49

)(x− 1

8

)2

+

(1

3

)(1−

23 )(x− 1)

(64

49

)(x− 1

8

)2

= (after simplifying)

= 0.466x3 − 1.26x2 + 1.46x+ 0.336 (coefficients to ≤ 3 significant figures)

From the lecture notes, the interpolation error is

f(x)−H(x) = (x− x1)2(x− x2)

2f(4)(ξx)

4!so

|f(x)−H(x)| ≤

∣∣∣∣∣(x− 1

8

)2

(x− 1)2

∣∣∣∣∣ · M24, (4.13)

where M is an upper bound for |f (4)(x)| on

[1

8, 1

].

f(x) = x13 =⇒ f ′(x) =

1

3x−

23

=⇒ f ′′(x) = −2

9x−

53

=⇒ f ′′′(x) =10

27x−

83

=⇒ f (4)(x) = −80

81x−

113

so |f (4)(x)| = 8081|x|− 11

3 , which is a decreasing function on

[1

8, 1

], thus

|f (4)(x)| ≤ 80

81

(1

8

)− 113

=

(80

81

)(2048) =

20736

10= M.

94

Page 96: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

We also need to maximize

(x− 1

8

)2

(x− 1)2 on

[1

8, 1

]. The graph of

h(x) =

(x− 1

8

)2

(x− 1)2 is:

We use Calculus to find the local maximum point. After some simpli-fying we obtain:

h′(x) =1

32(x− 1)(8x− 1)(16x− 9).

Thus local extrema occur at x = 1, x =1

8and x =

9

16. Thus h(x) ≤

h(

916

)= 2401

65536. So from (4.13)

|x13 −H(x)| ≤ 2401

65536· 20736

10· 1

24= 3.165 (predicted).

Note: if you plot the actual error between H(x) and f(x) the actualmaximum error is closer to 0.016. So the theoretical (predicted) maxi-mum error (≤ 3.165) is quite a bit different from this (but still consis-tent with the prediction).

4. (MATLAB) Use the program HERMITE_PRES to construct and plot theHermite interpolant H(x) for the following functions f(x) with n nodeson the specified intervals [a, b]:

95

Page 97: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

(a) f(x) = exp(x) on [−1, 1] with n = 2, 3

(b) f(x) = 1/x on [1/2, 4] with n = 2, 3, 4

(c) f(x) = x8 + 1 on [−1, 1] with n = 2, 3, 4, 5;

(d) f(x) = 1/(1 + 25x2) on [−1, 1] with n = 2, 3, 4, 5, 6;

We can measure the fit between f(x) and the interpolating polynomialH(x) using the maximum norm

‖f −H‖∞ := maxx∈[a,b]

|f(x)−H(x)|,

which is just the maximum absolute error between f and H for allx ∈ [a, b]. We say that the sequence of Hermite interpolation poly-nomials H converges to f if ‖f −H‖∞ → 0 as n → ∞. For eachproblem above decide in which cases the Hermite interpolation polyno-mials are convergent to the given functions. If the Hermite polynomialsare divergent explain why based on the plots. How do the graphs ofthe Hermite interpolating polynomials differ from the graphs of theLagrange interpolating polynomials?

Solution:Basically, the situation is the same as for the Lagrange case. For cases(a) - (c) we have convergence as n → ∞. Divergence in problem (d)is due to the same ‘Runge’s phenomena’ as in the Lagrange case. Themain difference here is that it is clear from the plots that not only dothe function values between f(x) and H(x) match, but so do the slopes.We show the plot for problem (c) with n = 3 below:

96

Page 98: Homework and Computer Solutions for Math*2130 …mgarvie/Teaching/Homework-2130/Homework-So… · Homework and Computer Solutions for Math*2130 (W17). MARCUS R. GARVIE 1 ... Solution:

The program also outputs the Hermite polynomial in traditional alge-braic form, namely

H(x) = 3x4 − 2x2 + 1.

It is clear that in this case we need greater than 3 nodes to get a goodapproximation. Recall that the degree of the Hermite interpolant isgiven by 2n− 1, thus if if n = 5 (thus the degree of H(x) would be 9)the approximation would be exact (i.e., f(x) = H(x)). This is becausewe are fitting polynomials to polynomials, and if you use a higher degreethan necessary the coefficients of the higher (unnecessary) powers of xwill be zero.

97