Introduction to Matlab

Mathematics University of Queensland Mathematics

12  A Small Assignment

All numbers on a computer are stored as binary numbers. (These are like decimals, except each digit is in base 2). So fractions like 1/3 cannot be stored exactly, but will have to be rounded to the nearest number that can be stored. The accuracy depends on the number of digits used to store numbers on the computer. Typically each number is stored so the accuaracy is at least 16 decimal digits. The relative error will be less than 2.2×10-16. As the details of binary arithmetic vary among computers, the accuracy expected on a particular machine is summarised by the easily computed quantity machine epsilon. Machine epsilon is defined as
machine epsilon : = min
{2-k : (1+2-k)-1 > 0,   k = 1,2,... },
where here (1+2-k)-1 means the value computed by the machine under investigation. Mathematically, (1+2-k)-1 is always positive, but when k is large the computer will not have enough digits to store (1+2-k) exactly, and eventually it will have to be rounded it down to 1. Then the computed value of (1+2-k)-1 will then be 0. The model assignment question we will do here is

Question:    Use Matlab to calculate machine epsilon on the Mathematics Department's computers. (Hint: Evaluate (1+2-k)-1 for k = 50, 51, ... , 55 .)

In principle, we can just type in the commands to get Matlab to evaluate (1+2-50)-1, (1+2-51)-1, ...  (1+2-55)-1 one after the other. A better way is the remember the component-wise operations and use the following commands.

format short e
k = (50:55)' ; e = 2 .^(-k) ; er = (1+e) -1 ; tab = [ k e er ]

Here k is the vector of integers 50 ... 55, and er contains the results of (1+2-k)-1. The table tab neatly summarises the computations. (Be sure to use the format short e to display the results at the terminal. The fixed point format short f is not suitable for answering this question, as some important small numbers will be displayed as 0.)

The computed results in tab are enough to answer this model assignment question, and we will need to hand up tab with the assignment. This could be done by just copying numbers from the terminal screen, but it is quicker to cut and paste these numbers to a word processor. Then we can neaten the table a little. Finally add some explanation to make it into our answer to the question. (Please ask for a demonstration if you do not know how to cut and paste numbers from the Matlab command window to your favourite editor or word processor.)

12.1  Model Answer

The matlab code below computes the value of (1+2-k)-1 for k=50,51,...,55. The results are shown in the table. The smallest value of 2-k for which (1+2-k)-1 > 0 is 2-52 = 2.22×10-16. Thus machine epsilon is 2.2×10-16

   

    Results for the Machine Epsilon Computations
 
      k       2^(-k)      (1 + 2^(-k))-1
     --     ---------       ---------
     50     8.8818e-16     8.8818e-16
     51     4.4409e-16     4.4409e-16
     52     2.2204e-16     2.2204e-16
     53     1.1102e-16              0
     54     5.5511e-17              0
     55     2.7756e-17              0
 
 
        Matlab Code Used          
 
  k = (50:55)' ; e = 2 .^(-k) ;
  er = (1+e) -1 ; tab = [ k e er ]

   

This question is very short. But it does illustrate some important points which hold for longer and more complicated problems.