Applied Numerical Methods Using MATLAB. Won Y. Yang

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

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

Applied Numerical Methods Using MATLAB - Won Y. Yang

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

S=S+p; end S*exp(-lam) time_not_nested=toc

      The above two scripts are made for computing Eq. (1.3.3). Noting that this sum of Poisson probability distribution [W-4] is close to 1 for such a large K, we can run them to find that one works fine, while the other gives a quite wrong result. Could you tell which one is better?

      It is time‐efficient to use vector operations rather than loop iterations to perform a repetitive job for an array of data. The following script “nm132_1.m” compares a loop iteration and a vector operation (for computing the sum of 105 numbers) in terms of the execution speed. Could you tell which one is faster?

      %nm132_1.m: Vector operation vs. Loop iteration N=1e5; th=[0:N-1]/50000*pi; tic s1=sin(th(1)); for i=2:N, s1= s1+sin(th(i)); end % Loop iteration time_loop=toc, s1 tic s2=sum(sin(th)); % Vector operation time_vector=toc, s2

      (1.3.4)equation

      %nm132_2.m: Vector operation vs. Loop iteration N=1000; x=rand(1,N); kk=[-100:100]; W=kk*pi/100; % Frequency range % for for loop tic for k =1:length(W) X_for1(k)=0; %zeros(size(W)); for n=1:N, X_for1(k) = X_for1(k) +x(n)*exp(-j*W(k)*(n-1)); end end time_loop_loop=toc % for vector loop tic X_for2 =0 ; %zeros(size(W)); for n=1:N X_for2 = X_for2 +x(n)*exp(-j*W*(n-1)); end time_vector_loop=toc % Vector operation tic nn=[1:N].'; X_vec = x*exp(-j*(nn-1)*W); time_vector_vector=toc discrepancy1= norm(X_for1-X_vec) discrepancy2= norm(X_for2-X_vec)

      The above script “nm132_2.m” compares a vector operation vs. a loop iteration for computing the DtFT in terms of the execution speed. Could you tell which one is faster?

      In this section, we compare an iterative routine and a recursive routine performing the same job. Consider the following two functions ‘ fctrl1(n)/fctrl2(n)’, whose common objectives is to get the factorial of a given nonnegative integer k.

      (1.3. 5)equation

function m=<b>fctrl1</b><![CDATA[(n) m=1; for k=2:n, m=m*k; end function m=<b>fctrl2</b><![CDATA[(n) if n<=1, m=1; else m=n*fctrl2(n-1); end

      

      A good program guides the program users who do not know much about the program and at least give them a warning message without runtime error for their minor mistake. If you do not know what runtime error is, you can experience one by taking the following steps:

      function m=<b>fctrl</b><![CDATA[(n) if n<0, error('The factorial of negative number ??'); else m=1; for k=2:n, m=m*k; end end

      1 Make and save the following routine ‘ fctrl()’ in an M‐file named “fctrl.m” in a directory listed in the MATLAB search path.

      2 Type fctrl(‐1) into the MATLAB Command window. Then you will see>fctrl1(-1) ans = 1

      This seems to imply that (−1)! = 1, which is not true. It is caused by the mistake of the user who tries to find (−1)! without knowing that it is not defined. This kind of runtime error seems to be minor because it does not halt the process. But it needs special attention for it may not be easy to detect. If you are a good programmer, you will insert some error handling statements in the function ‘ fctrl()’ as above. Then, when someone happens to execute fctrl(‐1) in the Command window or through an M‐file, the execution stops and he will see the error message in the Command window as

       Error using fctrl (line 2) The factorial of negative number ??

      Most common runtime errors are caused by an ‘out of domain’ index of array and the violation of matrix dimension compatibility, as illustrated in Section 1.1.7. For example, consider the ‘ Gauss(A,B)’ routine in Section 2.2.2, whose job is to solve a system of linear equations Ax=B for x. To appreciate the role of the fifth line handling the dimension compatibility error in the routine, remove the line (by putting the comment mark % before the line in the M‐file defining ‘ Gauss()’) and type the following statements in the Command window:

       >A=rand(3,3); B=rand(2,1); x=Gauss(A,B) Index exceeds matrix dimensions. Error in Gauss (line 10) AB=[A(1:NA,1:NA) B(1:NA,1:NB)]; % Augmented matrix

      Then MATLAB gives you an error message together with the suspicious statement line and the routine name. But it is hard to figure out what caused the runtime error and you may get nervous lest the routine should have some bug. Now, restore the fifth line in the routine and type the same statements in the Command window:

       >x=Gauss(A,B) Error using Gauss (line 8) A and B must have compatible dimension

      This error message (provided by the programmer of the routine) helps you to realize that the source of the runtime error is the incompatible matrices/vectors A and B given as the input arguments to the ‘ Gauss()’ routine. Very like this, a good program has a scenario for possible user mistakes and fires the ‘ error’ routine for each abnormal condition to show the user the corresponding error message.

      Many users often give more/fewer input arguments than supposed to be given to the MATLAB functions/routines and sometimes give wrong types/formats of data. To experience this type of error, let us try using the MATLAB function ‘ sinc1(t,D)’, that is defined in an M‐file named “sinc1.m” as

      function x=<b>sinc1</b><![CDATA[(t,D)

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