Docsity

Numerical Computing - Problem Set 1 with Solution Key | MATH 4800, Assignments of Mathematics

Toggle sidebar
bg1
D. Schwendeman
Numerical Computing Problem Set 1
Due:
Thursday, 9/15/11
1. Text exercise 1.3, page 42.
2. Consider the problem of evaluating the function sin(x) for some value of x∈[0, π]. Let
A= sin(x) and ˜
A= sin(x+h), where his a small perturbation. Estimate the absolute
and relative differences between ˜
Aand A. Estimate the relative condition number of this
problem. (The input to the problem is xand its output is sin(x).) For what value(s) of x
is the problem sensitive?
3. Text exercise 1.6, page 42.
4. Text exercise 1.13, page 43.
5. The roots x1and x2of the quadratic equation x2+ 2bx +c= 0 may be computed using
the usual quadratic formula
x1=−b−pb2−c, x2=−b+pb2−c.
(a) Compute the two roots using the formulas above for the cases (i) b= 1.23456 ×106,
c= 9.87654 ×108and (ii) b=−2.46864 ×10−2,c= 1.35753 ×10−8. Use 6-digit, base
10, floating point arithmetic (as in example 1.11 in the text). Determine the relative error
in the computed roots and discuss the results. Which roots are computed accurately and
which ones are not? Explain the results in terms of round-off error. (Hint: exact values for
the roots, accurate to 6 digits, are x1=−2.46872 ×106and x2=−4.00067 ×102for case
(i) and x1= 2.74957 ×10−7and x2= 4.93725 ×10−2for case (ii).)
(b) Write a Matlab function, called myRoots say, that takes as input the values of band
cin the quadratic equation above and returns x1and x2computed accurately. Base the
calculation of the roots on the formulas for x1and x2above. You may also consider, and
possibly use, the formulas x1+x2=−2band x1x2=cin your code depending on the values
of band c. (You may assume that b2−c > 0.) Run your code for the two cases given in
part (a).
6. Text computing problem 1.5, page 45. (Hint: for parts (b) and (c) you may assume that
the computed values of xand exare given by x(1 + ) and ex(1 + δ), respectively, where ||
and |δ|are bounded by mach.)
Notes:
1. For pencil and paper problems make sure you justify your answers clearly and legibly.
2. For computing problems, include (i) print outs of any Matlab functions or scripts
that you write, (ii) a “diary” of your Matlab session, and (iii) results and/or plots.
Basically, you need to provide documentation of Matlab codes that you write, how
they are used, and what the results are.
pf3
pf4
pf5

Partial preview of the text

Download Numerical Computing - Problem Set 1 with Solution Key | MATH 4800 and more Assignments Mathematics in PDF only on Docsity!

D. Schwendeman

Numerical Computing Problem Set 1

Due: Thursday, 9/15/

  1. Text exercise 1.3, page 42.
  2. Consider the problem of evaluating the function sin(x) for some value of x ∈ [0, π]. Let A = sin(x) and A˜ = sin(x + h), where h is a small perturbation. Estimate the absolute and relative differences between A˜ and A. Estimate the relative condition number of this problem. (The input to the problem is x and its output is sin(x).) For what value(s) of x is the problem sensitive?
  3. Text exercise 1.6, page 42.
  4. Text exercise 1.13, page 43.
  5. The roots x 1 and x 2 of the quadratic equation x^2 + 2bx + c = 0 may be computed using the usual quadratic formula

x 1 = −b − √ b^2 − c, x 2 = −b + √ b^2 − c.

(a) Compute the two roots using the formulas above for the cases (i) b = 1. 23456 × 106 , c = 9. 87654 × 108 and (ii) b = − 2. 46864 × 10 −^2 , c = 1. 35753 × 10 −^8. Use 6-digit, base 10, floating point arithmetic (as in example 1.11 in the text). Determine the relative error in the computed roots and discuss the results. Which roots are computed accurately and which ones are not? Explain the results in terms of round-off error. (Hint: exact values for the roots, accurate to 6 digits, are x 1 = − 2. 46872 × 106 and x 2 = − 4. 00067 × 102 for case (i) and x 1 = 2. 74957 × 10 −^7 and x 2 = 4. 93725 × 10 −^2 for case (ii).) (b) Write a Matlab function, called myRoots say, that takes as input the values of b and c in the quadratic equation above and returns x 1 and x 2 computed accurately. Base the calculation of the roots on the formulas for x 1 and x 2 above. You may also consider, and possibly use, the formulas x 1 +x 2 = − 2 b and x 1 x 2 = c in your code depending on the values of b and c. (You may assume that b^2 − c > 0.) Run your code for the two cases given in part (a).

  1. Text computing problem 1.5, page 45. (Hint: for parts (b) and (c) you may assume that the computed values of x and ex^ are given by x(1 + ) and ex(1 + δ), respectively, where || and |δ| are bounded by mach.)

Notes:

  1. For pencil and paper problems make sure you justify your answers clearly and legibly.
  2. For computing problems, include (i) print outs of any Matlab functions or scripts that you write, (ii) a “diary” of your Matlab session, and (iii) results and/or plots. Basically, you need to provide documentation of Matlab codes that you write, how they are used, and what the results are.

Problem 1: Exercise 1.3, Page 42

From page 5, absolute error = approximate value − true value, relative error = absolute errortrue value.

From the exercise, a = approximate value, t = true value, r = relative error.

Then the first equation above becomes absolute error = a − t, and the second equation becomes r = a−t t ⇔ rt = a − t ⇔ rt + t = a ⇔ a = t(1 + r).

Problem 2

Use the first two formulas from the last problem; to estimate A¯ = sin(x + h) for small h, use linear approximations by differentials to get sin(x + h) − sin(x) ≈ (sin(x) + h cos(x)) − sin(x) = h cos(x). This is our absolute error estimate. Plugging this into the formula for relative error, we get a relative error of hsin(^ cos(xx) )= h cot(x). The condition number is given on page 15 as C ≈

∣ xf f (′(xx))

∣ ∣^ =

∣∣ xsin( cos(xx))^ ∣∣∣ = |x cot(x)|. The problem is sensitive when the condition number is large. As x → 0 +,

we expect C ≈ 1, since sin(x) ≈ x and cos(x) ≈ 1 for small x. As x → π−, we expect C to become large (and the problem to become sensitive), as the numerator is ≈ π while the denominator heads to zero. Graphing the function^1 confirms this prediction.

Problem 3: Exercise 1.6, Page 42

Use the diagram on page 12 as a reference.

Part (a) Our true function is f (x) = sin(x); our approximating function is fˆ (x) = x. The forward error is simply the absolute difference between these: F = |f (x) − fˆ (x)|. The backward error is the absolute difference between our input and the input required to produce the estimation fˆ (x): B = |x − xˆ|, where ˆx is defined by the equation ˆx = f −^1 ( fˆ (x)). (^1) See [http://www.wolframalpha.com/input/?i=abs%28x*Cot%28x%29%29%2C+x+from+0+to+pi], for example.

the explicit formulas for x 1 and x 2 , without using any of the formulas from the next part of the problem.) To explain this, it is useful to keep in mind Example 1.15 from the text. Note that the nonzero errors occur when we add two numbers with opposite sign: cancellation of the significant digits leaves the accumulation of the previous steps’ round-off error as the result.

Part (b) One way to do this is to use the posted code, testMyRoots.m. An example of what myRoots.m might look like follows.

1 function x=myRoots(b,c) 2 3 discriminant=bˆ2-c; 4 if discriminant≤ 0 5 disp('Error: discriminant must be positive') 6 x=[0 0]; 7 return 8 end 9 10 if b> 0 11 x1=-b-sqrt(discriminant); 12 x2=c/x1; 13 else 14 x2=-b+sqrt(discriminant); 15 x1=c/x2; 16 end 17 18 x=[x1 x2];

Problem 6

Part (a) This is just a straightforward limit calculation using l’Hˆopital’s rule, as indicated in the text.

xlim→ 0 f^ (x) = lim x→ 0 e

x (^) − 1 x = lim^ x→^0

ex 1 =^ e

Part (b) The following example code produces outputs that begin at about 1.05, decrease to slightly below 1, and finally increase to about 1.11.

1 f = zeros(1,15); 2 3 for k=1: 4 x = 10ˆ(-k); f(k) = (exp(x)-1)/x; 5 end

This does not agree with theoretical expectations, which say that the behavior should be more like f (x) → 1 +^ as x → 0 +. To see the source of this disagreement, use the hint given by Professor Schwendeman, as well as some judicious choice of Taylor expansions.

ex^ − 1 x ≈^

ex(1 + δ) − 1 x(1 + ε) ≈^

(1 + x)(1 + δ) − 1 x(1 + ε) ≈^

(1 + δ)(1 + x)(1 − ε) − (1 − ε) x

≈ 1 +^ x^ +^ δ^ − x^ ε −^ (1^ −^ ε)= x^ +x δ= 1 + (^) xδ

First, use the hint. Then, use ex^ ≈ 1 + x. Then, use (^) 1+^1 ε ≈ 1 − ε. Finally, since x, δ, and ε are all small, ignore terms higher than linear terms and simplify algebraically. To interpret the final result, note that δ ≤ 10 −^16 and x ≥ 10 −^15 , so 1 + δx could be as much as about 1.1 (and note that the approximation improves as x grows smaller, which is why we see such good agreement for smaller x).

Part (c) The following code produces outputs that begin at about 1.05 and steadily decrease toward 1, becoming about one decimal place closer to 1 with each successive output. The program code is here.

1 g = zeros(1,15);