Number Approximation - Exercises

1.Numbers

Exercise 1. Define one-to-one correspondences between the following sets of numbers:

  1. ๐”ผ={n|nโˆˆโ„•,nmod2=0.}, ๐•†={n|nโˆˆโ„•,nmod2=1.}

    Solution. f:๐”ผโ†’๐•†, f(n)=n+1 is one-to-one.

  2. โ„•, โ„ค

    Solution. f:โ„•โ†’โ„ค, with f defined by {0โ†’0,1โ†’-1,2โ†’1,3โ†’-2,4โ†’2,โ€ฆ} is one possibility. Introducing [x] as the integer part of xโˆˆโ„š, i.e. [x]=n with nโฉฝx<n+1, f can be expressed as

    f(n)=(-1)nmod2([n/2]+nmod2)

    In Julia xรทy is integer division, so for nโˆˆโ„• [n/2]=nรท2, and % is the modulo operator

    [4÷5 4÷4 4÷3 4÷2; 4%5 4%4 4%3 4%2]

    [ 0 1 1 2 4 0 1 0 ] (1)

    function f(n)
      q = n÷2; r = n%2; s = 1-2*r;
      s*(q+r)
    end

    f

    N=10; [collect(0:N)'; f.(0:N)']

    [ 0 1 2 3 4 5 6 7 8 9 10 0 -1 1 -2 2 -3 3 -4 4 -5 5 ] (2)

  3. โ„ค, โ„š

    Solution. Construct a table and introduce diagonal traversal to obtain the positive rationals p/q.

    Figure 1. One-to-one mapping showing that |โ„š|=|โ„•|

From above deduce |โ„•|=|๐”ผ|=|๐•†|=|โ„ค|=|โ„š|=โ„ต0.

Exercise 2. Provide an example to show |โ„|>|โ„•|.

Exercise 3. Let โ„•q={n|n.โˆˆโ„•,n<2q}. Answer the following questions analytically. Also provide a Julia implementation.

  1. Define a one-to-one correspondence f:โ„•2qโ†’โ„•qร—โ„•q

  2. Let f(m1)=(n1,p1), f(m2)=(n2,p2). Assume m1+m2โˆˆโ„•2q. Express f(m1+m2) in terms of n1,n2,p1,p2 .

  3. Assume m1โ‹…m2โˆˆโ„•2q. Express f(m1โ‹…m2) in terms of n1,n2,p1,p2 .

Exercise 4. Construct a one-to-one representation of the positions of atoms within an hexagonal lattice, f:โ„ค2โ†’โ„2. Implement f and f-1 as Julia functions. Use f to construct a graphical representation of a two-dimensional hexagonal lattice.

Exercise 5. Construct a one-to-one representation of the positions of atoms within an hexagonal lattice, f:โ„ค3โ†’โ„3. Implement f and f-1 as Julia functions. Use f to construct a graphical representation of a three-dimensional hexagonal lattice.

2.Approximation

Exercise 6. Write Julia code to compute machine epsilon ฯต for Float32 and Float64.

Solution

function MachEps(type)
  one=type(1.0); half=type(0.5); eps=one;
  while (one+half*eps != one)
    eps=half*eps;
  end
  return eps;
end;
[MachEps(Float32) eps(Float32) MachEps(Float64) eps(Float64)]

[ 1.1920928955078125e-7 1.1920928955078125e-7 2.220446049250313e-16 2.220446049250313e-16 ] (3)

Exercise 7. Carry out a numerical experiment to verify the Axiom of floating point arithmetic within Float32, by computing ฯ€+r in Float32 and comparing to the result in Float64. Construct a scatter plot of (r,ฮต) with ฮต the error in computing ฯ€+r in Float32.

Solution

The floating point axiom states fl(x)โŠ›fl(y)=(xโˆ—y)(1+ฮต), with |ฮต|โฉฝฯต, leading to

ฮต=fl(x)โŠ›fl(y)xโˆ—y-1,

or in this case

ฮต=fl(ฯ€)โŠ•fl(r)ฯ€+r-1.

The operations in โ„ are computed in Float64 in the following, with r randomly chosen.

function ErrPlot(n)
  pi32=Float32(pi); pi64=Float64(pi); half=Float64(0.5); one=Float64(1.0);
  rscale = 1.0e3*half; e32 = eps(Float32);
  r64=Float64.( rscale*(rand(n) .- half) );
  r32=Float32.(r64);
  result32 = pi32 .+ r32; result64 = pi64 .+ r64;
  ε = result32 ./ result64 .- one;
  rmin=minimum(r64); rmax=maximum(r64);
  clf(); plot(r32,ε,".",[rmin rmax],[e32 e32],"dg",[rmin rmax],[-e32 -e32],"dg");
  xlabel("r"); ylabel("ε"); title("Float32 addition error");
end;
ErrPlot(1000); savefig(homedir()*"/courses/MATH661/images/E01Fig02.eps")

Figure 2. Numerical experiment on verification of floating point axiom. While for most numbers within this random sample the axiom is verified, there are a few cases when rโ‰…0 the error is larger than ฯต.

Exercise 8. Consider the approximations of e

Sn=1+12!+โ‹ฏ+1n!,Tn=1n!+1(n-1)!+โ‹ฏ+1.

  1. Write Julia functions to compute Sn, Tn.

    Solution

    function S(n)
      fact=1.0; sum=1.0;
      for k=2:n
        fact = k*fact;
        sum = sum + 1/fact;    
      end
      return sum;
    end;
    function T(n)
      fact=1.0;
      for k=n:-1:2
        fact=k*fact;
      end
      sum=0.0;
      for k=n:-1:1    
        sum = sum + 1/fact;
        fact = fact/k;
      end
      return sum;
    end;

  2. Determine if Sn=Tn for all nโˆˆโ„•.

    Solution

    In โ„, indeed Sn=Tn by commutativity (proof by induction). In ๐”ฝ there must exist some N such that for n>N, Snโ‰ Tn as a consequence of the existence of machine epsilon. Verify by computation (note organization of computations to use Julia broadcasting and presentation of results in a single table)

    r=1:8; s=S.(r); t=T.(r); chk = s.==t; [r s t chk]

    [ 1.0 1.0 1.0 1.0 2.0 1.5 1.5 1.0 3.0 1.6666666666666667 1.6666666666666665 0.0 4.0 1.7083333333333335 1.7083333333333333 0.0 5.0 1.7166666666666668 1.7166666666666668 1.0 6.0 1.7180555555555557 1.7180555555555554 0.0 7.0 1.7182539682539684 1.7182539682539684 1.0 8.0 1.71827876984127 1.7182787698412698 0.0 ] (4)

  3. Determine if |Sn-Tn|<ฯต (ฯต is machine epsilon). Is the floating point axiom verified?

  4. Determine if |Sn-Tn|<(n-1)ฯต. Is the floating point axiom verified?

Exercise 9. Consider the approximations of ฯ€/2 given by Wallis's product

Sn=(21)โ‹…(23โ‹…43)โ‹…(45โ‹…65โ‹…67โ‹…87)โ€ฆpn,

Tn=pnโ‹…โ€ฆโ‹…(45โ‹…65โ‹…67โ‹…87)โ‹…(23โ‹…43)โ‹…(21)

  1. Find the general term pn.

  2. Determine if Sn=Tn for all nโˆˆโ„•.

  3. Determine if |Sn-Tn|<ฯต (ฯต is machine epsilon). Is the floating point axiom verified?

  4. Determine if |Sn-Tn|<(n-1)ฯต. Is the floating point axiom verified?

Exercise 10. Consider the approximations of e/2 given by Pippenger's product

Sn=(21)1/2โ‹…(23โ‹…43)1/4โ‹…(45โ‹…65โ‹…67โ‹…87)1/8โ€ฆpn,

Tn=pnโ‹…โ€ฆโ‹…(45โ‹…65โ‹…67โ‹…87)1/8โ‹…(23โ‹…43)1/4โ‹…(21)1/2

  1. Find the general term pn.

  2. Determine if Sn=Tn for all nโˆˆโ„•.

  3. Determine if |Sn-Tn|<ฯต (ฯต is machine epsilon). Is the floating point axiom verified?

  4. Determine if |Sn-Tn|<(n-1)ฯต. Is the floating point axiom verified?

3.Successive approximations

Exercise 11. Assume errors in successive numerical approximation of aโˆˆโ„, finite, are given by en=an-an-1,with{an}nโˆˆโ„•, an=n1/3.

  1. Construct a scatter plot of (n,en). Does the plot indicate convergence of the numerical approximation?

  2. Compute limnโ†’โˆžen.

  3. Suppose |en|<ฮต. What is an upper bound for |an-a|?

Exercise 12. Assume errors in successive numerical approximation of aโˆˆโ„, finite, are given by en=an-an-1,with{an}nโˆˆโ„•, an=n-1/2.

  1. Construct a scatter plot of (n,en). Does the plot indicate convergence of the numerical approximation?

  2. Compute limnโ†’โˆžen.

  3. Suppose |en|<ฮต. What is an upper bound for |an-a|?

Exercise 13. Consider a sequence of successive approximations of the derivative f'(x0)

dn=f(x0+1/n)-f(x0)1/n,nโˆˆโ„•.

  1. Is {dn}nโˆˆโ„• a convergent sequence?

  2. Is {dn}nโˆˆโ„• a Cauchy sequence?

  3. Construct a scatter plot of (n,dn) for f(x)=sinx, x0=ฯ€/4. Does the plot indicate convergence of {dn}nโˆˆโ„•?

  4. Construct a scatter plot of (n,en), en=dn-dn-1, for f(x)=sinx, x0=ฯ€/4. Does the plot indicate convergence of {dn}nโˆˆโ„•?

Exercise 14. Consider errors in successive approximations {an}nโˆˆโ„• given by en=an-an-1=en-1+en-2, i.e., errors at each step accumulate errors in previous two steps, with e1=e0=1. Is this a convergent approximation? Present both an analytical solution, and a numerical experiment.

Exercise 15. Consider errors in successive approximations {an}nโˆˆโ„• given by en=an-an-1=56en-1+16en-2, i.e., errors at each step are a weighted average of those in previous two steps, with e1=e0=1. Is this a convergent approximation? Present both an analytical solution, and a numerical experiment.

Exercise 16. Consider errors in successive approximations {an}nโˆˆโ„• given by en=an-an-1=12en-1+14en-2, i.e., errors at each step are less than a weighted average of those in previous two steps, with e1=e0=1. Is this a convergent approximation? Present both an analytical solution, and a numerical experiment.