Applied Numerical Methods Using MATLAB. Won Y. Yang

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

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

Applied Numerical Methods Using MATLAB - Won Y. Yang

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

      to plot the graph of a sinc function

      D=0.5; b1=-2; b2=2; t=b1+[0:100]/100*(b2-b1); subplot(221), plot(t,sinc1(t,D)), axis([b1 b2 -0.4 1.2]) hold on, plot(t,sinc1(t),'r:')

Image described by caption and surrounding text.

       if nargin<2, D=1; end

      This line takes care of the case where the number of input arguments ( nargin) is <2, by assuming that the second input argument is D=1 by default. This programming technique is the key to making the MATLAB functions adaptive to different number/type of input arguments, which is very useful for adding the user‐convenience into the MATLAB functions. To appreciate its role, we remove the second line or deactivate it by putting the comment mark % in the beginning of the line in the M‐file defining ‘ sinc1()’, and type the same statement in the Command window, trying to use ‘ sinc1()’ without the second input argument.

       >plot(t,sinc1(t),'k:') Not enough input arguments. Error in sinc1 (line 4) x=sin(pi*t/D)./(pi*t/D);

      This time we get a serious error message with no graphic result. It is implied that the MATLAB function without appropriate error‐handling capability does not allow the user's default or carelessness.

       t(find(t==0))=eps; or equivalently, for i=1:length(t), if t(i)==0, t(i)=eps; end, end

      This statement changes any zero entry in the t vector into eps (2.2204e − 16). What is the real purpose of this statement? It is actually to remove the possibility of division‐by‐zero in the next statement, which is a mathematical expression having t in the denominator:

       x=sin(pi*t/D)./(pi*t/D);

      Using the modified function ‘ sinc1()’ with the third line activated, you will get the graphs with no hole like Figure 1.7b.

      Lastly, consider of the fourth line in ‘ sinc1()’, which is only one essential statement performing the main job:

       x=sin(pi*t/D)./(pi*t/D);

      What is the dot ( .) before division operator (/) for? Regarding this, authors gave you a piece of advice that you had better put a dot ( .) just before the arithmetic operators *(multiplication), /(division), and ̂(power) in the function definition so that the element‐by‐element (elementwise or termwise) operation can be done any time (Section 1.1.6 (A7)). To appreciate the existence of the dot ( .), we remove it from the M‐file defining ‘ sinc1()’, and run the following statements:

       >clf, plot(t,sinc1(t,D)), sinc1(t,D), sin(pi*t/D)/(pi*t/D) ans = -0.0183

      What do you see in the graphic window on the screen? To our surprise, nothing appears and that with no warning message! What is more surprising, the value of sinc1(t,D) or sin(pi*t/D)/(pi*t/D) shows up as a scalar. It is hoped that this accident will help you realize how important it is for right term‐by‐term operations to put dot ( .) before the arithmetic operators *, /, and ̂. By the way, aren't you curious about how MATLAB deals with a vector division without dot ( .)? If so, try with the following statements:

       >A=[1:10]; B=2*A; A/B, A*B'*(B*B')̂-1, A*pinv(B) ans = 0.5

      To understand this response of MATLAB, you can see Section 1.1.7 or 2.1.2.

      In this section, we looked over several sources of runtime error, hoping that it aroused reader's attention to runtime errors.

       global Gravity_Constant Dielectric_Constant

      %plot_sinc.m clear, clf global D D=0.5; b1=-2; b2=2; t=b1+[0:100]/100*(b2-b1); %passing the parameter(s) through arguments of the function subplot(221), plot(t, sinc1(t,D)), axis([b1 b2 -0.4 1.2]) %passing the parameter(s) through global variables subplot(222), plot(t, sinc2(t)), axis([b1 b2 -0.4 1.2])

function x=<b>sinc1</b><![CDATA[(t,D) if nargin<2, D=1; end t(find(t==0))=eps; x=sin(pi*t/D)./(pi*t/D); function x=<b>sinc2</b><![CDATA[(t) global D t(find(t==0))=eps; x=sin(pi*t/D)./(pi*t/D);

      Then, how convenient it would be, since you do not have to bother about passing the parameters. But as you get proficient in programming and handle many functions/routines that are involved with various sets of parameters, you might find that the global variable is not always convenient, because of the following reasons:

       Once

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