Page 21 - Applied Numerical Methods Using MATLAB
P. 21
10 MATLAB USAGE AND COMPUTATIONAL ERRORS
commands:
>>[x,y,butkey] = ginput %get the x,y coordinatesofthe mouse button
or ascii code of the key pressed till pressing the ENTER key
>>[x,y,butkey] = ginput(n) %repeat the same job for up to n points clicked
1.1.5 3-D Graphic Output
MATLAB has several 3-D graphic plotting commands such as plot3(), mesh(),
and contour(). plot3() plots a 2-D valued-function of a scalar-valued vari-
able; mesh()/contour() plots a scalar valued-function of a 2-D variable in a
mesh/contour-like style, respectively.
Readers are recommended to use the help command for detailed usage of each
command. Try running the MATLAB program ‘nm115.m’ to see what figures
will appear (Figs.1.5 and 1.6).
%nm115: to plot 3D graphs
t = 0:pi/50:6*pi;
expt = exp(-0.1*t);
xt = expt.*cos(t); yt = expt.*sin(t);
%dividing the screen into2x2 sections
subplot(221), plot3(xt, yt, t), grid on %helix
subplot(222), plot3(xt, yt, t), grid on, view([0 0 1])
subplot(223), plot3(t, xt, yt), grid on, view([1 -3 1])
subplot(224), plot3(t, yt, xt), grid on, view([0 -3 0])
pause, clf
x = -2:.1:2; y = -2:.1:2;
[X,Y] = meshgrid(x,y); Z = X.^2 + Y.^2;
subplot(221), mesh(X,Y,Z), grid on %[azimuth,elevation] = [-37.5,30]
subplot(222), mesh(X,Y,Z), view([0,20]), grid on
pause, view([30,30])
subplot(223), contour(X,Y,Z)
subplot(224), contour(X,Y,Z,[.5,2,4.5])
1.1.6 Mathematical Functions
Mathematical functions and special reserved constants/variables defined in MAT-
LAB are listed in Table 1.3.
MATLAB also allows us to define our own function and store it in a file
named after the function name so that it can be used as if it were a built-in
function. For instance, we can define a scalar-valued function:
2
f 1 (x) = 1/(1 + 8x )
and a vector-valued function
2 2
f 1 (x 1 ,x 2 ) x + 4x − 5
1
2
f 49 (x) = = 2
f 2 (x 1 ,x 2 ) 2x − 2x 1 − 3x 2 − 2.5
1