Applied Numerical Methods Using MATLAB. Won Y. Yang
Чтение книги онлайн.
Читать онлайн книгу Applied Numerical Methods Using MATLAB - Won Y. Yang страница 26
to plot the graph of a sinc function
With this purpose, type the following statements in the Command window:
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:')
The two plotting commands ‘ sinc1(t,D)
’ and ‘ sinc1(t)
’ yield the solid and dotted line graphs, respectively, as shown in Figure 1.7a where it should be noted that ‘ sinc1()
’ does not bother us and works fine even without the second input argument D
. We owe the second line in the function ‘ sinc1()
’ for the nice error‐handling service:
Figure 1.7 Graphs of sinc functions. Using ‘ sinc1()
’ without division‐by‐zero handling (a) and with division‐by‐zero handling (b).
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.
However, you may feel annoyed by the hole at t = 0 in the graphs shown in Figure 1.7a. What caused such an ‘accident of hole’? It happened (with no warning message about ‘division‐by‐zero’) because not only the denominator but also the numerator of Eq. (1.3.6) becomes for t = 0 to make the value of Eq. (1.3.6) NaN
(not a number : undetermined
) while the true value of the sinc function at t = 0 is sinc(0) = 1 as obtained by applying the L'Hopital's rule [W-7]. To avoid the ‘accident of hole’ due to the NaN problem, activate the third line (in ‘ sinc1()
’) by deleting the comment mark %
in the beginning of the line where the third line is another error‐handling statement:
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.
1.3.5 Parameter Sharing via GLOBAL Variables
When we discuss the runtime error that may be caused by user's default in passing some parameter as input argument to the corresponding function, you might feel that the parameter passing job is troublesome. OK, it is understandable as a beginner in MATLAB. How about declaring the parameters as global so that they can be accessed/shared from anywhere in the MATLAB world as far as the declaration is valid? If you want to, you can declare any variable(s) by inserting the following statement in both the main program and all the functions using the variables.
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