A ppendix An Introduction to matlaB This appendix int roduces the reader to programming with the software pack- age MATLAB. It is assumed that the reader has had previous experience with a high-level programming language and is familiar wit h the techniques of writing loops, branching using logical relation, calling subroutines, and editing These techniques are directly applicable in the windows ty pe environment of MATLAB MATLAB is a mat hemat ical software package based on matrices. The package consists of an extensive library of numerical routines, easily accessed two-and three-dimensional graphics, and a high-level programming format. The ability to quickly implement and modify programs makes MATLAB an appro- priate format for exploring and executing the algorit hms in this textbook The reader should work through the following tut orial introduction to MATLAB(MATLAB commands are in typ writer type). The examples illus- trate typical input and output from the MaTLAB Command window. To find additional information about commands, options, and examples, the reader is urged to make use of the on-line help facility and the Reference and User's guides that accompany the software Arit hmetic Operations addition Subtraction Multiplication Division pi, e, i Const ants Ex.>(2+3*pi)/2 ans 5.7124 Built-in Functions Below is a short list of some of the funct ions availa ble in matlab. The follow. ing example illustrates how functions and arit hmetic operations are combined Descriptions of ot her available functions may be found by using the on-line help facility abs(=)cos(=)exp(=) log(=) log10(-)cosh( in()tan()sqrt()floor (+) acos(-) tanh(-) Ex. >3*cos(sqrt(4.7) ans= 1.6869
! ! ! ! ! ! ! " # ! $ % & ! ' () ! * + , - . $ / , ! ! ! "# "# "# "# "# "# "# "# !"# ""# "# "# / ! #$%$& 0
The default format shows approximately five signifi cant decimal figures, Enter- ng the command format long will display approximately 15 signifi cant decimal figures Ex. Format long 3*cos(sqrt(4.7)) ans= 1.68686892236893 Assignment Statements Variable names are assigned to expressions by using an equal sign Ex.>a=3-floor(exp(2.9)) -15 A semicolon placed at the end of an expression suppresses the computer echo (out put Ex. >b=sin(a) Note: b was not displayed 2*b^2 ans 0.8457 Defining functions in MatLAB the user can define a function by construct ing an M-file(a file ending in. m) in the M-file Edit or/Debugger. Once defined, a user-defined function is called in the same manner as built-in functions Ex. Place the function fun(x)=1+a-r /4 in the M-file fun. m. In th Editor/ Debugger one would enter the follow ing function y=fun(x) y=1+xX.2/4 We will explain the use of".' shortly. Different letters could be used for the varia bles and a different name could be used for the function but the same format would have to be followed. once this funct ion has been saved as an M-file named fun .m it can be called in the matlab command w indow in the same manner as any funct ion >>>cos(fun (3)) ans 0.1782 A useful and efficient way to evaluat e funct ions is to use the feval command This command requires that the funct ion be called as a st ring Ex. >feval('fun, 4) Matrices
! & & & / '( ! ! 01 & & / '( ! #$%$%$%& $%& 2 ! ) #" & # " # / * 3 4 ! - % & ! & " & # & / 5, 6 & & / . "# 7 0 * 8 & / 5, 4 '+ '+ #- * % 9-9 ! ,: : 6 & '+( $ % ! '+ #% Æ ! ', / ', -'+- ;
All variables in MATLAB are treated as matrices or arrays. Matrices can en Ex.>A=[123456789 4 258 369 Semicolons are used to separate the rows of a matrix. Note that, the ent ries of the mat rix must be separated by a single space. Alternatively, a matrix can be entered row by row Ex.>A=[123 456 789 456 Matrices can be generated using built-in functions Ex. >Z=zeros(3, 5): creates a 3 5 mat >>X=ones(3, 5) creates a 3×5 matrix of ones =0:05:2 creates the displayed 1 x5 matrix 00.50001.00001.50002.0000 creates a 1x5 matrix by taking the cosine of each entry of Y ans= 1.00000.87760.54030.0707-0.4161 The components of matrices can be manipulated in several ways Ex.>A=(2,3) select a submat rix of A ans 6 >A(1 3 [13 another way to select a submatrix of A ans 13 79 >A(2, 2)=tan(7, 8): assign a new value to an entry of A Additional commands for matrices can be found by using the on-line help facility or consulting the documentat ion accompanying the software Matrix Operations Addition Subtraction Multiplication ower
! !4 / ./ * $* % &0 . $ % & + 3 ! ! ! / ./ $ % & 0 . $ % & / 12 * < 1 = 3 * < 1 455 ! 0 1 4 4 0 1 ! ! 4 %$ #$ ! / . $ . / 0/ 0 ! & . %* ! ! ! ! * + - . <