ТОП просматриваемых книг сайта:
Applied Numerical Methods Using MATLAB. Won Y. Yang
Читать онлайн.Название Applied Numerical Methods Using MATLAB
Год выпуска 0
isbn 9781119626824
Автор произведения Won Y. Yang
Жанр Математика
Издательство John Wiley & Sons Limited
1.1.9.3 while
Loop
A while
loop will be iterated as long as its predefined condition is satisfied and a break
statement is not encountered inside the loop.
1 (Ex. 7) A while Loop%nm119_7.m: example of while loop r=1; while r<10 r= input('\nType radius (or nonpositive number to stop):'); if r<=0, break, end %isempty(r)|r<=0, break, end v= 4/3*pi*r̂3; fprintf('The volume of a sphere with radius %3.1f =%8.2f\n',r,v); end
2 (Ex. 8) while Loops to find the minimum/maximum positive numbers represented in MATLAB%nm119_8.m: example of while loops x=1; k1=0; % To repeat division-by-2 to find the minimum positive number while x/2>0 x=x/2; k1=k1+1; end k1, x_min=x; fprintf('x_min is %20.18e\n',x_min) % To repeat multiplation-by-2 to find the maximum positive number x=1; k2=0; while 2*x<inf x=x*2; k2=k2+1; end k2, x_max0=x; tmp=1; k3=0; while x_max0*(2-tmp/2)<inf tmp=tmp/2; k3=k3+1; end k3, x_max=x_max0*(2-tmp); fprintf('x_max is %20.18e\n',x_max) format long e, x_min,-x_min,x_max,-x_max format hex, x_min,-x_min,x_max,-x_max format shortThe following script “nm119_8.m” contains three while loops. In the first one, x=1 continues to be divided by 2 till just before reaching zero and it will hopefully end up with the smallest positive number that can be represented in MATLAB. In the second one, x=1 continues to be multiplied by 2 till just before reaching inf (the infinity defined in MATLAB) and seemingly it will get the largest positive number ( x_max0) that can be represented in MATLAB. But while this number reaches or may exceed inf if multiplied by 2 once more, it still is not the largest number in MATLAB (slightly less than inf) that we want to find. How about multiplying x_max0 by (2−1/2n)? In the third while loop, the temporary variable tmp starting with the initial value of 1 continues to be divided by 2 till just before x_max0*(2‐tmp) reaches inf and apparently it will end up with the largest positive number ( x_max) that can be represented in MATLAB.
1.2 Computer Errors vs. Human Mistakes
Digital systems like calculators and computers hardly make a mistake, since they follow the programmed order faithfully. Nonetheless, we often encounter some numerical errors in the computing results made by digital systems, mostly coming from representing the numbers in finite bits, which is an intrinsic limitation of digital world. If you let the computer compute something without considering what is called the finite‐word‐length effect, you might come across a weird answer. In that case, it is not the computer, but yourself as the user or the programmer, who is to blame for the wrong result. In this context, we should always be careful not to let the computer produce a farfetched output. In this section, we will see how the computer represents and stores the numbers. Then we think about the cause and the propagation effect of computational error in order not to be deceived by unintentional mistake of the computer and hopefully, to be able to take some measures against them.
1.2.1 IEEE 64‐bit Floating‐Point Number Representation
MATLAB uses the IEEE 64‐bit floating‐point number system to represent all numbers. It has a word structure consisting of the sign bit, the exponent field, and the mantissa field as follows:
63 | 62 | 52 | 51 | 0 | ||||
S | Exponent | Mantissa |
Each of these fields expresses S, E, and M of a number f in the way described as follows:
Sign bit
Exponent field (b62b61b60 ⋯ b52): adopting the excess 1023 code
Mantissa field (b51b50 ⋯ b1b0):In the un‐normalized range where the numbers are so small that they can be represented only with the value of hidden bit 0, the number represented by the mantissa is(1.2.1) You might think that the value of the hidden bit is added to the exponent, instead of to the mantissa.
In the normalized range, the number represented by the mantissa together with the value of hidden bit bh = 1 is
(1.2.2)
The set of numbers S, E, and M, each represented by the sign bit S, the exponent field Exp and the mantissa field M, represents a number as a whole
(1.2.3)
We classify the range of numbers depending on the value (E) of the exponent and denote it as
(1.2.4)
In each range, the least unit, i.e. the value of least significant bit (LSB) or the difference between two consecutive numbers represented by the mantissa of 52 bits is
(1.2.5)
Let us take a closer look at the bit‐wise representation of numbers belonging to each range:
(0) 0 (zero)
63 | 62 | 52 | 51 | 0 | ||||
S | 000 ⋯ 0000 | 0000 0000 ⋯ 0000 0000 |
(1) Un‐normalized range (with the value of hidden bit bh = 0)(1.2.6.1a) (1.2.6.1b)
(2) The smallest normalized range (with the value of hidden bit bh = 1)(1.2.6.2a) (1.2.6.2b)
(3) Basic normalized range (with the value of hidden bit bh = 1)(1.2.6.3a) (1.2.6.3b)
(4) The largest normalized range (with the value of hidden bit bh = 1)(1.2.6.4a) (1.2.6.4b)
(5) ± ∞(inf) with Exp = 211 − 1 = 2047, E = Exp − 1023 = 1024 (meaningless)
From what has been mentioned earlier, we know that the minimum and maximum positive numbers are, respectively,
(1.2.7a)
(1.2.7b)
where the three MATLAB constants, i.e. eps
, realmin
, and realmax
, represent 2−52, 2−1022, and (2 − 2−52) × 21023, respectively. This can be checked by running the script “nm109.m” in Section 1.I..
Now, in order to gain some idea about the arithmetic computational mechanism, let us see how the addition of two numbers, 3 and 14, represented in the IEEE 64‐bit floating number system, is performed.
In the process of adding the two numbers illustrated in Figure 1.6, an alignment is made so that the two exponents in their 64‐bit representations equal each other; and it will kick out the part smaller by more than 52 bits, causing some numerical error. For example,