Numerical Analysis 2

Spring 2005

Some Homework Solutions

1b

Limit[(2  n^2 + 6  n - 1)/(4  n^2 + 2  n + 1), 
      n -> Infinity]

Limit[((2  (n^2) + 6  n - 1)/(4  (n^2) + 2  n + 1)), 
      n -> ∞]

Limit[(1/2) - (2  n^2 + 6  n - 1)/(4  n^2 + 2  n + 1), 
      n -> Infinity]

4a

Plot[x^2 - 3  x + 1, {x, -1, 2}]

D[x^2 - 3  x + 1, x]

D[x^2 - 3  x + 1, {x, 2}]

x^2 - 3  x + 1 /. x -> {-1, 1.5, 2}

5a

f[x_] := x^4 - 4  x^2

f[0]

Plot[f[x], {x, -2, 2}]

f[-2]

f[2]

D[f[x], x]

Solve[D[f[x], x] == 0, x]

6b

g[x_] := (x^2)/(x + 1)

(g[1] - g[0])/(1 - 0)

Solve[D[g[x], x] == 1/2, x]

8a

∫x (E^x) \[DifferentialD]x /. {x -> 
          0}

(∫_0^2) 
        x (E^x) \[DifferentialD]x

11a

(∑_(n = 0)^∞) (1/(2^n))

11d

(∑_(k = 1)^∞) (1/(4  (k^2) - 1))

How does the system arrive at the value 1/2.  The nth partial sum of the series is:

(∑_(k = 1)^n) (1/(4  (k^2) - 1))

The value of  (∑_(k = 1)^∞) (1/(4  (k^2) - 1)) is the limit of the nth partial sum, or

Limit[n/(2  n + 1), n -> ∞]

12a

Series[(Sqrt[x]), {x, 1, 4}]

Binary Numbers

All numbers are expressed in binary format in the computer.  This leads to errors in cases where a decimal number does not have an exact binary expression.

Adding 0.1 100000 Times

10000 - Apply[Plus, Table[0.1, {100000}]]

10000 - (∑_(k = 1)^100000) 0.1

There is an error as shown above because 0.1 does not have an exact binary representation.

Adding 0.125    80000 Times

10000 - Apply[Plus, Table[0.125, {80000}]]

This has no error because 0.125 has an exact binary representation.  First, consider a procedure for converting a binary number to decimal format.

Conversion of a Base 2 Integer to a Base 10 integer

binarytodecimalinteger[
        num_] := 
Module[{ids, 
          mults}, 
ids = 
          IntegerDigits[num]; 
If[
          Not[(Union[ids] == {0, 1} || Union[ids] == {0} || 
                Union[ids] == {1})], Print["Not a Binary"]; 
          Break[]]; 
mults = 
          Table[2^(k - 1), {k, Length[ids], 
              1, -1}]; 
Apply[Plus, 
          ids mults]
]

binarytodecimalinteger[111]

binarytodecimalinteger[100011011]

binarytodecimalinteger[1001100]

binarytodecimalinteger[1111111]

These conversions can be made by using the following Mathematica syntax.

2^^100011011

2^^1001100

2^^1111111

No problems in that--now a function that takes a base 10 integer and converts it to a base 2 integer--the output is a list of the digits in the binary form.

base10tobinary[
        num_] := 
Module[{bindigits, 
          newnum}, 
bindigits = {}; 
newnum = num; 
While[newnum > 0, 
          PrependTo[bindigits, Mod[newnum, 2]]; 
          newnum = 
            Quotient[newnum, 
              2]]; 
bindigits
]

base10tobinary[127]

base10tobinary[76]

This can be done in Mathematica using the following syntax.

BaseForm[127, 2]

BaseForm[76, 2]

Again, no problems--conversion causes no errors.  Next consider first the conversion of a base 2 fraction to a decimal fraction, and following that, the conversion of a decimal fraction to a base to fraction--here, you will find some errors--errors of the type that account for the error shown in the above example.

binarytodecimalfraction[
        num_] := 
Module[{ids, mults, 
          setoff}, 
ids = 
          RealDigits[num]; 
Print[
          ids]; 
setoff = 
          ids[[2]]; 
Print[
          setoff]; 
If[(Union[ids[[1]]] == {0, 1} || 
              Union[ids[[1]]] == {0} || Union[ids[[1]]] == {1}), , 
          Print["Not a Binary"]; Break[]]; 
mults = 
          Table[(1/2)^k, {k, 1 - setoff, 
              Length[ids[[1]]] - setoff}]; 
N[
          Apply[Plus, ids[[1]] mults]]
]

binarytodecimalfraction[11.01]

binarytodecimalfraction[0.110011]

Or doing the same two computations using Mathematica syntax.

2^^0.1

2^^11.01

2^^0.110011

In making the conversion from a decimal fraction to a binary fraction, errors creep in.  The function created to do this is shown next--note that there are two inputs, the decimal to be approximated and the number of binary digits to use in the approximation.  The works by multiplying the fraction by 2 each time, the integer part will give the binary digit at each step and the fractional part of the number is then multiplied by 2, and the step repeated as many times as the number of binary digits to the right of the decimal point requested in the function input.  First a step by step example, and then the definition of the function.  To find the first 4 binary digits for the decimal fraction 0.625:

2 *0.625

IntegerPart[2 *0.625]

FractionalPart[2 *0.625]

IntegerPart[2*0.25]

FractionalPart[2*0.25]

IntegerPart[2*0.5]

FractionalPart[2*0.5]

The IntegerPart of 2*0 is 0, so the last digit produced is 0.  The digits have to be reversed so the binary form of 0.625 is .1010  This result is shown following the definition of the function--note that the function only gives a list of the binary digits to the right of the decimal point.

base10fractobinaryfrac[num_, 
        nbindigitsrightofdecimal_] := 
Module[{bindigits, 
          newnum}, 
bindigits = {}; 
newnum = 2  num; 
Do[
          PrependTo[bindigits, IntegerPart[newnum]]; 
          newnum = 
            2 FractionalPart[
                newnum], {nbindigitsrightofdecimal}]; 
Reverse[bindigits]
]

base10fractobinaryfrac[0.625, 24]

base10fractobinaryfrac[1.1, 120]

These two conversions can also be made in Mathematica as follows.

BaseForm[0.625, 2]

BaseForm[0.1, 2]

BaseForm[1.1, 2]

These functions could be combined to find a binary approximation to any real number in decimal form.

Machine Numbers

Strings of bits (0's and 1's) are stored to represent approximations to real integers or fractions.  The format is

x≈±q × (2^n) where q, the mantissa is a finite binary expression with 0.5≤q<1 and n, the exponent is an integer

The following shows what happens in a system in which the mantissa has 4 binary digits and the exponent ranges from -3 to 4.  First a list of the binaries with leading digit 1 are expressed as a list.

ll = {1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111}

These binaries are converted to decimal fractions by the following function.

nn = 
      N[Map[(#[[1]]*(1/2) + #[[2]]*(1/4) + #[[3]]*(1/
                      8) + #[[4]]*(1/16)) &, 
          IntegerDigits[ll]]]

Next, a table of the possible numbers representable with the given restrictions is shown.

TableForm[Transpose[Table[N[nn (2^k)], {k, -3, 4}]], 
      TableHeadings -> {ll, Range[-3, 4]}]

The decimal equivalents from the above table are rearranged and plotted by the next two statements.  Notice that the gaps between numbers become larger as you move away from 0.

prs = 
      Transpose[{nn10s = Flatten[Table[N[nn 2^k], {k, -3, 3}]], 
          Table[0, {Length[nn10s]}]}]

ListPlot[
        Transpose[{nn10s = Flatten[Table[N[nn 2^k], {k, -3, 3}]], 
            Table[0, {Length[nn10s]}]}], 
        PlotStyle -> {PointSize[0.02]}];

The following link allows you to add numbers in a decimal system in which there are limitations.

Link to Floating Point Applet

Link to Tutorial on Machine Number Format