Applied Numerical Methods Using MATLAB. Won Y. Yang
Чтение книги онлайн.
Читать онлайн книгу Applied Numerical Methods Using MATLAB - Won Y. Yang страница 21
1.1.9.2 for index=i_0:increment:i_last‐end
Loop
A for
loop makes a block of statements executed repeatedly for a specified number of times, with its loop index increasing from i
_ 0
to a number not greater than i
_ last
by a specified step ( increment
) or by 1 if not specified. The loop iteration normally ends when the loop index reaches i
_ last
, but it can be stopped by a break
statement inside the for
loop. The for
loop with a positive/negative increment will never be iterated if the last value ( i
_ last
) of the index is smaller/greater than the starting value ( i_0
).
1 (Ex. 6) A for Loop%nm119_6.m: example of for loop point= [76 85 91 65 87]; for n=1:length(point) if point(n)>=80, pf(n,:)= 'pass'; elseif point(n)>=0, pf(n,:)= 'fail'; else %if point(n)<0 pf(n,:)= '????'; fprintf('\n\a Something wrong with the data??\n'); break; end end pf
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)