Docsity

Assignment 1 Solutions - Numerical Computing | MATH 4800, Assignments of Mathematics

Toggle sidebar
bg1
Numerical Computing S’09 MATH/CSCI 4800-01
Homework-1
1. Find (a) the binary representation of the base-10 number 12.8 and (b) the base-10 representation of
the binary number 10.0101101.(Overbar notation denotes recurrence of digits under the overbar.)
(a) For the integer part, divide repeatedly by 2 and record the remainders.
12/2 = 6 + 0
6/2 = 3 + 0
3/2 = 1 + 1
1/2 = 0 + 1.
Therefore, the binary representation of the integer 12 is
(12)10 = (1100)2
For the fractional part, multiply by 2 and record the integer part.
0.8×2=0.6+1
0.6×2=0.2+1
0.2×2=0.4+0
0.4×2=0.8+0
0.8×2=0.6+1.
We see that the pattern has begun to repeat. Therefore the binary representation of (0.8)10 is
(0.8)10 = (1100 1100 1100 . . .)2
= (.1100)2.
Combining the two results above,
(12.8)10 = (1100.1100)2.
(b) Let x= (10.0101101)2.Then, moving the radix 3 digits to the right,
23x= (10010.1101)2
= 24+ 2 + (.1101)2.
We can rewrite the above relation as
8x= 18 + z, z = (.1101)2.(1)
Now,
z= (.1100)2= (.1101 1101 1101 . . .)2.
Moving the radix 4 points to the right,
24z= (1101.1101 1101 1101 . . .)2,
or,
16z= (1101.1100)2= 23+ 22+ 20+z= 13 + z.
Therefore,
z=13
15.
Substituting into (1) above,
x=1
818 + 13
15=283
120 = (2.3583333 . . .)10.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Assignment 1 Solutions - Numerical Computing | MATH 4800 and more Assignments Mathematics in PDF only on Docsity!

Numerical Computing S’09 MATH/CSCI 4800- Homework-

  1. Find (a) the binary representation of the base-10 number 12.8 and (b) the base-10 representation of the binary number 10. 0101101. (Overbar notation denotes recurrence of digits under the overbar.)

(a) For the integer part, divide repeatedly by 2 and record the remainders.

12 / 2 = 6 + 0 6 / 2 = 3 + 0 3 / 2 = 1 + 1 1 / 2 = 0 + 1.

Therefore, the binary representation of the integer 12 is

(12) 10 = (1100) 2

For the fractional part, multiply by 2 and record the integer part.

  1. 8 × 2 = 0 .6 + 1
  2. 6 × 2 = 0 .2 + 1
  3. 2 × 2 = 0 .4 + 0
  4. 4 × 2 = 0 .8 + 0
  5. 8 × 2 = 0 .6 + 1.

We see that the pattern has begun to repeat. Therefore the binary representation of (0.8) 10 is

(0.8) 10 = (1100 1100 1100.. .) 2 = (.1100) 2.

Combining the two results above,

(12.8) 10 = (1100.1100) 2.

(b) Let x = (10.0101101) 2. Then, moving the radix 3 digits to the right,

23 x = (10010.1101) 2 = 24 + 2 + (.1101) 2.

We can rewrite the above relation as

8 x = 18 + z, z = (.1101) 2. (1)

Now, z = (.1100) 2 = (.1101 1101 1101.. .) 2. Moving the radix 4 points to the right,

24 z = (1101.1101 1101 1101.. .) 2 ,

or, 16 z = (1101.1100) 2 = 2^3 + 2^2 + 2^0 + z = 13 + z. Therefore, z =

Substituting into (1) above,

x =

  1. Express x = 12.8 as a single-precision IEEE float f l(x), using the round-to-nearest rule. (The result of Problem 1 above will be useful.) Compute the relative error d = |x − f l(x)|/|x| exactly as a base- number, and show that d satisfies d ≤ (1/2)mach. In problem 1(a) above we found that x = (12.8) 10 = (1100.1100) 2. (Henceforth we shall not use subscript 2 explicitly to indicate a binary representation, except where needed to resolve any ambiguity.) In normalized form we have

x = 1.[100 1100 1100 1100 1100 1100]1100 1100... × 23.

Since the 24th digit after the radix is 1 and the subsequent digits are not all zeros, rounding up leads to replacing 0 at the 23rd position by 1, and chopping. Therefore,

f l(x) = 1.[100 1100 1100 1100 1100 1101] × 23.

This approximation involves removal of the infinite tail

t =. 1100 × 2 −^23 × 23 =. 1100 × 2 −^20. (1)

We can write the decimal representation of t as follows; the steps are similar to those employed in problem 1(b) above. Let x =. 1100. Then,

24 x = 1100. 1100 16 x = 23 + 2^2 +. 1100 = 12 + x x =

Substitution into (1) yields t = 0. 8 × 2 −^20. (2) The approximation also involves the addition of 2−^23 to the abscissa in the process of rounding up. This addition amounts to a = 2−^23 × 23 = 2−^20. Putting it all together,

f l(x) = x − t + a = x − 0. 8 × 2 −^20 + 2−^20 = x + 0. 2 × 2 −^20

Then, the relative error is

d =

f l(x) − x x

∣ =^

0. 2 × 2 −^20

0. 2 × 8 × 2 −^23

× 2 −^23 =

× 2 −^23 <

mach

where we recall that mach = 2−^23 for IEEE single precision.

  1. (a) Text: Section 0.3, Page 16, Problem 4.

(b) Text: Section 0.3, Page 16, Problem 8. (c) Let x and y be normalized positive floating point numbers on a binary computer with p-bit precision. How small can y − x be if x < 4 < y?

(c) Recall that the digits closest to the machine number z on either side of z are x = z(1 − mach) and y = z(1 + mach), as long as z is in the interior of a block corresponding to a particular value of the exponent. Here, however, z = 4 is at the boundary of the 2^1 and 2^2 blocks. Thus the number immediately to the right of 4 is

y = [1. 00... 01] × 22

where the fractional part of the mantissa has all zeros except in the p − 1 place. Thus we can write y = [1 + 1/ 2 p−^1 ] × 22 = 4 + 4/ 2 p−^1. Similarly the number immediately to the left of 4, lying in the 2^1 block, is

x = [1. 11... 11] × 21

where the fractional part of the mantissa now has all ones. Thus we can write

x =

[

2 p−^1

]

× 21 =

1 − 1 / 2 p 1 − 1 / 2 × 21 = 4 − 4 / 2 p.

Then,

y − x = 4 + 4/ 2 p−^1 − [4 − 4 / 2 p] = 4/ 2 p−^1 + 4/ 2 p^ = 8/ 2 p^ + 4/ 2 p^ = 6/ 2 p−^1 = 6mach.

The result could have been obtained more simply by realizing that the separation between numbers is 2^1 × mach in the 2^1 block and 2^2 × mach in the 2^2 block.

  1. (a) Text: Section 0.4, Page 19, Problem 2. Hint: You will need a calculator, Matlab for example. Use the ideas presented in Example 0.6 on pages 18-19 of the Text. Taylor expansion of the square root in the quadratic formula can also be used. (b) Text: Section 0.5, Page 23, Problem 8.

(a) We have ax^2 + bx + c = 0 with a = 1, b = 3 and c = − 8 −^14. The conventional way to express the roots is x 1 =

−b +

b^2 − 4 ac 2 a

and x 2 =

−b −

b^2 − 4 ac 2 a

Since b^2 is large compared to 4ac,

b^2 − 4 ac ≈ b and the formula for x 1 is subject to catas- trophic cancellation. We can compute x 2 as above and for x 2 , use the alternate form

x 1 =

c ax 2

Double precision calculation on Matlab gives

s =

b^2 − 4 ac = 3.000 000 000 000 151.

Then, using (1) above,

x 2 =

−b − s 2 a

to 3 digits, and

x 1 = −b + s 2 a

15. 1 × 10 −^14

= 7. 55 × 10 −^14.

The second occurrence of 5 in the answer above for x 1 is suspect because −b + s has only 3 significant digits, with the third digit influenced by rounding. Using the above value of x 2 in (2) we obtain the more accurate solution

x 1 =

− 8 −^14

= 7. 579122514774211 × 10 −^14 = 7. 579 × 10 −^14 to 3 digits.

(b) We use Taylor’s theorem with remainder (Theorem 0.8, page 21 in the Text):

f (x) = f (a) + (x − a)f ′(a) +

(x − a)^2 2!

f ′′(a) + · · · +

(x − a)n n!

f (n)(a) +

(x − a)n+ (n + 1)!

f ′(n+1)(c),

where the last term is the remainder after n terms and c lies between x and a. Here, f (x) = cos x and n = 5. Then the above formula becomes

cos x = 1 −

x^2 2!

x^4 4!

+ R 5.

Since f (vi)(x) = − cos x, the remainder is

R 5 = −

x^6 6! cos c.

For a = 0 and x ∈ [−π/ 4 , π/4], the upper bound on R 5 corresponds to x = π/4 and cos c = 1. Then |R 5 | ≤

(π/4)^6 6!

= 3. 26 × 10 −^4.

  1. Text: Section 0.4, Page 20, Computer Problem 2(a).

It is easily seen that for f (x) =

tan x − x x^3 , lim x→ 0 f (x) = 1/ 3.

The attached Matlab output is a printout of x = 10−p^ and f (x) for p = 1, 2 , · · · 10. It shows that as p increases, agreement between computed f (x) and the limiting value increases until p = 4, and then decreases. At p = 8 the computed value of f (x) = 0, indicating that all significant digits are lost.

  1. Consider the polynomial f (x) = (x − 1)^6. It can also be written in the expanded form as g(x) = x^6 − 6 x^5 + 15x^4 − 20 x^3 + 15x^2 − 6 x + 1. Write a Matlab Script to graph f and g on the same plot on the interval [1 − h, 1 + h], h = 0. 005. In your program evaluate the expanded form g by Horner’s method, outlined in Section 0.1 of the Text. You may use the Matlab script PolyTest.m provided on the course home page as a starting point for your own script. Make sure that the script is suitably commented, that the two functions are plotted in different colors, that the axes are appropriately labeled, and that the plot is titled. Attach the script and the plot to your report. You will find that the two plots are not identical. Discuss the results, giving reasons for the observed differences. The Matlab script and the figure are attached. The figure shows that the plot of f has the expected shape but that of g is contaminated by roundoff errors, which imbue it with a jagged shape and a large number of unexpected zeros.

PROBLEM 6. Matlab code.

% Script File: PolyTest

% Computes y=(x-1)^6 near x=1.

% Plots y and the function zero on the interval [1-h,1+h].

n=101; % number of plotting points

h=0.005; % width of plotting interval

x=linspace(1-h,1+h,n); % x coordinates of plotting points

y=(x-1).^6;

z=1+x.(-6+x.(15+x.(-20+x.(15+x.*(-6+x)))));

plot(x,y,'r',x,zeros(n,1),'g',x,z,'b');

axis([1-h 1+h -max(abs(y)) max(abs(y))]);

xlabel('x');

title('Plots of f(x) (red), g(x) (blue) and zero (green)');

x 10 −

x

Plots of f(x) (red), g(x) (blue) and zero (green)