Applied Numerical Methods Using MATLAB. Won Y. Yang

Чтение книги онлайн.

Читать онлайн книгу Applied Numerical Methods Using MATLAB - Won Y. Yang страница 30

Applied Numerical Methods Using MATLAB - Won Y. Yang

Скачать книгу

make a routine ‘ norm_vector(v,p)’ which computes the norm of a given vector as(P1.15.1) for any positive integer p, finds the maximum absolute value of the elements for p=inf, and computes the norm as if p=2, even if the second input argument p is not given. If you have no idea, permutate the statements in the below box and save it in a file named “norm_vector.m”. Additionally, try it to get the norm with p=1, 2,∞ ( inf) for an arbitray vector generated by the command ‘ rand(2,1)’. Compare the result with that obtained by using the ‘ norm()’ command.function nv=<b>norm_vector</b><![CDATA[(v,p) if nargin<2, p=2; end nv= sum(abs(v).̂p)̂(1/p); nv= max(abs(v)); if p>0&p∼=inf elseif p==inf end

      16 1.16 Backslash (\) OperatorLet us play with the backslash ( \) operator.Use the backslash ( \) command, the minimum‐norm solution (2.1.7), and the ‘ pinv()’ command to solve the following equations, find the residual error ||Aix ‐bi||'s and the rank of the coefficient matrix Ai, and fill in Table P1.16 with the results.(P1.16.1) (P1.16.2) (P1.16.3) (b) Use the backslash( \) command, the least‐squares (LSs) solution (2.1.10), and the ‘ pinv()’ command to solve the following equations and find the residual error ||Aix ‐bi||'s and the rank of the coefficient matrix Ai, and fill in Table P1.16 with the results.(P1.16.4) (P1.16.5) (P1.16.6) (cf) If some or all of the rows of the coefficient matrix A in a system of linear equations can be expressed as a linear combination of other row(s), the corresponding equations are dependent, which can be revealed by the rank deficiency, i.e. rank(A) < min(M,N) where M and N are the row and column dimensions, respectively. If some equations are dependent, they may have either inconsistency (no exact solution) or redundancy (infinitely many solutions), which can be distinguished by checking if augmenting the RHS vector b to the coefficient matrix A increases the rank or not, that is, rank([Ab]) > rank(A) or not [M-2].(c) Based on the results obtained in (a) and (b) and listed in Table P1.16, answer the following questions:Based on the results obtained in (a‐(i)), which one yielded the nonminimum‐norm solution among the three methods, i.e. the backslash ( \) operator, the minimum‐norm solution (2.1.7), and the ‘ pinv()’ command? Note that the minimum‐norm solution means the solution whose norm (||x||) is the minimum over the many solutions.Based on the results obtained in (a), which one is most reliable as a means of finding the minimum‐norm solution among the three methods?Based on the results obtained in (b), choose two reliable methods as a means of finding the LS solution among the three methods, i.e. the backslash ( \) operator, the LS solution (2.1.10), and the ‘ pinv()’ command. Note that the LS solution means the solution for which the residual error (||Ax−b||) is the minimum over the many solutions.Table P1.16 Results of operations with backslash( \) operator and ‘ pinv()’ command.backslash ( \)Minimum‐norm or least‐squares (LS) pinv()Remarkx||Aix − bi||x||Aix − bi||x||Aix − bi||rank(Ai) redundant/inconsistentA1x − b11.5000 0 1.50006.4047 × 10−15A2x − b20.3143 0.6286 0.94291.7889A3x − b3∞ ∞ ∞∞A4x − b42.5000 0.00001.2247A5x − b5A6x − b6

      17 1.17 Operations on VectorsFind the mathematical expression for the computation to be done by the following MATLAB statements: >n=0:100; S=sum(2.̂-n)Write a MATLAB statement which performs the following computation:Write a MATLAB statement that uses the commands ‘ prod()’ and ‘ sum()’ to compute the product of the sums of each row of a 3 × 3 random matrix.How does the following function ‘ repetition(x,M,N)’ convert a scalar or a matrix given as the first input argument x to make a new sequence y? How does it compare with the MATLAB built‐in function ‘ repmat()’?function y=<b>repetition</b><![CDATA[(x,M,N) y1=x; for n=2:N, y1 = [y1 x]; end y=y1; for m=2:M, y = [y; y1]; endComplete the following function ‘ zero_insertion(x,M,m)’ so that it can insert m zeros just after every Mth element of a given row vector sequence x to make a new sequence. Write a MATLAB statement to use the function for inserting two zeros just after every third element of x = [1 3 7 2 4 9] to gety = [1 3 7 0 0 2 4 9 0 0]function y=<b>zero_insertion</b><![CDATA[(x,M,m) Nx=length(x); N=floor(Nx/M); y=[reshape(x(1:M*N),?,N); ????s(m,N)]; y=[y(:)' x(M*N+1:Nx)];How does the following function ‘ zeroing(x,M,m)’ convert a given row vector sequence x to make a new sequence y?function y=<b>zeroing</b><![CDATA[(x,M,m) m=mod(m,M); Nx=length(x); N=floor(Nx/M); y=x; y(M*[1:N]-m)=0; Complete the following function ‘ sampling(x,M,m)’ so that it can sample every ( kM − m)th element of a given row vector sequence x to make a new sequence y. Write a MATLAB statement to use the function for sampling every (3k − 2)th element of x = [1 3 7 2 4 9] to getfunction y=<b>sampling</b><![CDATA[(x,M,m) m=mod(m,M); Nx=numel(x); N=floor(Nx/M); y=x([1:N]*?-?); if Nx-N*M>=M-m, y=[y x(N*M+M-m)]; end Complete the following function ‘ rotate_r(x,M)’ so that it can rotate a given row vector sequence x right by M samples to make a new sequence y. Write a MATLAB statement to use the function for rotating x = [1 2 3 4 5] to getfunction xl=<b>rotate_r</b><![CDATA[(x,M) N=size(x,2); M=mod(M,N); xl=[x(:,end-?+1:end) x(:,1:end-?)];

      18 1.18 Distribution of a Random Variable – HistogramComplete the following function ‘ randu(N,a,b)’ that uses the MATLAB function ‘ rand()’ to generate an N‐dimensional random vector having the uniform distribution over [ a, b] and draws the histogram (with 20 bins) for the distribution of the elements of the generated vector as Figure 1.5. Then, find the average height of the histogram that you can get by running the following statement: >randu(1000,-2,2)function x=<b>randu</b><![CDATA[(N,a,b) % generates an N-dimensional random vector with U(a,b) x=a+(?-?)*rand(1,N); if nargout==0, hist(x(:),20); end

      19 1.19 Number RepresentationIn Section 1.2.1, we looked over how a number is represented in 64 bits. For example, the IEEE 64‐bit floating‐point number system represents the number 3(21 ≤ 3 < 22) belonging to the range R1 = [21,22) with E = 1 as010000000000100000000000...........00000000000000000000400800........00000where the exponent and the mantissa areThis can be confirmed by typing the following statement into MATLAB command window: >fprintf('3=%bx\n',3) or >format hex, 3, format short which will print out onto the screen 4008000000000000This is exactly the hexadecimal representation of the number 3 as we expected. Find the IEEE 64‐bit floating‐point number representation of the number 14 and use the command ‘fprintf()’ to check if the result is right.(cf) Since the INTEL system stores numbers in little‐endian (byte order) format with more significant bytes in the memory of higher address number, you might see 0000000000000840in the old versions of MATLAB, which is reversely ordered in the unit of byte (8 bits = 2 hexadecimal digits) so that the number is represented with the most/least significant byte on the right/left side.

      20 1.20 Resolution of Number Representation and Quantization ErrorIn Section 1.2.1, we have seen that adding 2−22 to 230 makes some difference, while adding 2−23 to 230 makes no difference due to the bit shift by over 52 bits for alignment before addition. How about subtracting 2−23 from 230? In contrast with the addition of 2−23 to 230, it makes difference as you can see by running the following MATLAB statement: >x=2̂30; x+2̂-23==x, x-2̂-23==x which will give you the logical answer 1 (true) and 0 (false). Justify this result based on the difference of resolution of two ranges [230,231) and [229,230) to which the true values of computational results (230 + 2−23) and (230 − 2−23) belong, respectively. Note from Eq. (1.2.5) that the resolutions, i.e. the maximum quantization errors are ΔE = 2E−52 = 2−52+30 = 2−22 and 2−52+29 = 2−23, respectively. For details, refer to Figure P1.20, which illustrates the process of addition/subtraction with 4 mantissa bits, 1 hidden bit, and 1 guard bit.Figure P1.20 Process of addition/subtraction with four mantissa bits.

      21 1.21

Скачать книгу