Applied Numerical Methods Using MATLAB. Won Y. Yang

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

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

Applied Numerical Methods Using MATLAB - Won Y. Yang

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

digital value corresponding to an analog data smaller than b(1) (the smallest boundary) should be c(1) (the smallest centroid) and that corresponding to an analog data larger than b(N) (the largest boundary) should be c(N) (the largest centroid) where the boundary and centroid vectors are assumed to be arranged in ascending order and N is the number of the centroids (quantization levels) or quantization intervals.Explain the jobs of lines 8, 9, and 10 of the following ADC function ‘ adc1()’.Explain the jobs of lines 3, 5, and 7 of the following ADC function ‘ adc2()’.Run the script “nm01p10.m” (see below box) to get two graphs as shown in Figure P1.10. Explain about the graphs.Figure P1.10 The characteristic of an analog‐to‐digital converter (ADC).function d=<b>adc1</b><![CDATA[(a,b,c) %Analog-to-Digital Converter %Input a=analog signal, b(1:N+1)=boundary vector % c(1:N)=centroid vector %Output: d=digital samples N=length(c); for n=1:length(a) I=find(a(n)<b(2:N)); if ∼isempty(I), d(n)=c(I(1)); else d(n)=c(N); end endfunction d=<b>adc2</b><![CDATA[(a,b,c) N=length(c); d(find(a<b(2)))=c(1); for i=2:N-1 index=find(b(i)<=a&a<b(i+1)); d(index)=c(i); end d(find(b(N)<=a))=c(N);%nm01p10.m b=[-3 -2 -1 0 1 2 3]; % Boundary vector c=[-2.5 -1.5 -0.5 0.5 1.5 2.5]; % Centroid vector % Plot the input-output relationship xa=[-300:300]/100; % Analog data in the range [-3∼+3] xd1=adc1(xa,b,c); xd2=adc2(xa,b,c); subplot(221) plot(xa,xd1, xa,xd2,'r:') % Output of ADC to a sinusoidal input t=[0:200]/100*pi; xa=3*sin(t); xd1=adc1(xa,b,c); %xd2=adc2(xa,b,c); subplot(222) plot(t,xa, t,xd1,'r')

      11 1.11 Decimal‐to‐Binary/Octal ConversionsReferring to the decimal‐to‐binary conversion process illustrated in Figure 1.6, complete the following function ‘ dec2bin_my()’ so that it can perform the conversion process. Then, using the function, convert two decimal numbers 3 and 14 to their corresponding binary numbers and compare the results with those obtained using the MATLAB built‐in function ‘ dec2bin()’. Can you modify the function so that the binary numbers corresponding to each of x can be placed columnwise, that is, from top to bottom, rather than from left to right when a vector consisting of multiple decimal numbers is given as the first input argument x?Complete the following function ‘ bin2dec_my()’ so that it can perform the binary‐to‐digital conversion process:function y=<b>dec2bin_my</b><![CDATA[(x) % converts given decimal numbers into binary numbers of N bits y=[]; xmax=max(x); N=1; while xmax>1 xmax=floor(xmax/2); N=N+1; end for n=1:length(x) xn=x(n); for i=?:-1:1; xn_1= floor(xn/?); yn(i)= xn-?*xn_1; xn=xn_1; end y= [y yn]; endfunction y=<b>bin2dec_my</b><![CDATA[(x) [M,N]=size(x); % Number of binary numbers and Number of bits for m=1:M y(m,:)=x(m,:)*?.̂[?-1:-1:0]'; endThen, using the function, convert two binary numbers [0 0 1 1] and [1 1 1 0] to their corresponding digital numbers and compare the results with those obtained using the MATLAB built‐in function ‘ bin2dec()’, by typing the following statements at MATLAB prompt:bin2dec_my([0 0 1 1; 1 1 1 0]) bin2dec(['0011'; '1110'])(c) Modify the decimal‐to‐binary converting function ‘ dec2bin_my()’ into another function ‘ dec2oct_my()’ so that it can perform the decimal‐to‐octa conversion process. Then, using the function, convert two decimal numbers 14 and 3 to their corresponding octal numbers and compare the results with those obtained using the MATLAB built‐in function ‘ deci2oct()’.

      12 1.12 Truth Table for a Logical (Boolean) ExpressionNoting that the logical AND, OR, and NOT operators are &, |, and ∼ in MATLAB, complete and run the following script “make_truth_table.m” to construct the truth table for the following logical (Boolean) expression:(P1.12.1) that lists the (output) value of the expression for all possible values of the input variables.%make_truth_table.m % makes the truth table for a logical (Boolean) expression N=3; % Number of Boolean variables Inputs=dec2bin_my([0:2̂N-1]); %All possible values of input variables A=Inputs(:,1); B=Inputs(:,2); C=Inputs(:,3); truth_table=[Inputs (?A&?&C)?(B?∼C)]

      13 1.13 Playing with PolynomialsPolynomial evaluation: polyval() Write a MATLAB statement to compute(P1.13.1) (b) Polynomial addition/subtraction by using compatible vector addition/subtractionWrite a MATLAB statement to add the following two polynomial coefficient vectors:(P1.13.2) (c) Polynomial multiplication: conv()Write a MATLAB statement to get the following product of polynomials:(P1.13.3) (d) Polynomial division – deconv()Write a MATLAB statement to get the quotient and remainder of the following polynomial division:(P1.13.4) (e) Routines for differentiation/integration of a polynomialWhat you see in the below box is the function ‘ poly_der(p)’, which gets a polynomial coefficient vector p (in the descending order) and outputs the coefficient vector pd of its derivative polynomial. Likewise, compose a function ‘ poly_int(p)’, which outputs the coefficient vector of the integral polynomial for a given polynomial coefficient vector.(cf) MATLAB has the built‐in functions ‘ polyder()’/‘ polyint()’ that can be used to find the derivative/integral of a polynomial.function pd=<b>poly_der</b><![CDATA[(p) %p: Vector of polynomial coefficients in descending order N=length(p); if N<=1, pd=0; % constant else for i=1:N-1, pd(i)=p(i)*(N-i); end end function pi=<b>poly_int</b><![CDATA[(p) % p: Vector of polynomial coefficients in descending order N=length(p); pi(N+1)=0; for i=1:N, pi(i)=p(?)/(N-?+1); end(f) Roots of a polynomial equation: roots()Write a MATLAB statement to get the roots of the following polynomial equation:(P1.13.5) You can check if the result is right, by using the MATLAB command ‘ poly()’, which generates a polynomial having a given set of roots.(g) Partial fraction expansion (PFE) of a rational polynomial function – residue()/ residuez()(i) The MATLAB function ‘ [r,p,k]=residue(B,A)’ finds the PFE for a ratio of given polynomials B(s)/A(s) as(P1.13.6a) which is good for taking the inverse Laplace transform. Use the function to find the PFE for(P1.13.7a) (ii) The MATLAB function ‘ [r,p,k]=residuez(B,A)’ finds the PFE for a ratio of given polynomials B(z)/A(z) as(P1.13.6b) which is good for taking the inverse z‐transform. Use the function to find the PFE for(P1.13.7b) (h) Piecewise polynomial – mkpp()/ ppval()Suppose we have an M × N matrix P, the rows of which denote M (piecewise) polynomials of degree (N − 1) for different (nonoverlapping) intervals with (M + 1) boundary points bb=[b(1) .. b(M+1)], where the polynomial coefficients in each row are supposed to be generated with the interval starting from x = 0. Then we can use the MATLAB command ‘ pp=mkpp(bb,P)’ to construct a structure of piecewise polynomials, which can be evaluated by using ‘ ppval(pp)’.Figure P1.13 shows a set of piecewise polynomials {p1(x+3), p2(x+1), p3(x −2)} for the intervals [ −3, −1], [ −1, 2], and [2, 4], respectively, where(P1.13.8) (i) Complete and run the upper part of the above MATLAB script “nm01p13h.m” to use ‘ mkpp()’/‘ ppval()’ for making this graph.(ii) Complete and run the lower part of the above MATLAB script “nm01p13h.m” to use ‘ polyval()’ for making this graph.(iii) (cf) You can type ‘ help mkpp’ to see a couple of examples showing the usage of ‘ mkpp()’.%nm01p13h.m % to plot the graph of a piecewise polynomial bb=[-3 -? 2 4]; % Boundary point vector % Matrix having three polynomial coefficient vectors % in its rows P=[1 0 0;-1 2 -?;1 0 -2]; pp=mkpp(??,P) xx=bb(1)+[0:1000]/1000*(bb(end)-bb(1)); % Whole range plot(xx,ppval(??,xx)), hold on% Alternative without using ppval() % to plot the polynomial curves one by one for i=1:size(P,1) xx=[0:100]/100*(bb(i+1)-bb(i)); plot(xx+??(i),polyval(?(i,:),xx),'r:') endFigure P1.13 The graph of piece‐wise polynomial functions.

      14 1.14 Routine for Matrix MultiplicationAssuming that MATLAB cannot perform direct multiplication on vectors/matrices, supplement the following incomplete function l.l ‘ multiply_matrix(A,B)’ so that it can multiply two matrices given as its input arguments only if their dimensions are compatible, but display an error message if their dimensions are not compatible. Try it to get the product of two arbitrary 3 × 3 matrices generated by the command ‘ rand(3)’ and compare the result with that obtained by using the direct multiplicative operator *. Note that the matrix multiplication can be described as(P1.14.1) function C=<b>multiply_matrix</b><![CDATA[(A,B) [M,K]=size(A); [K1,N]=size(B); if K1∼=K error('The # of columns of A is not equal to the # of rows of B') else for m=1:? for n=1:? C(m,n)=A(m,1)*B(1,n); for k=2:? C(m,n)=C(m,n)+A(m,k)*B(k,n); end end end end

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