# MATLAB Basics Padmanabhan Seshaiyer

First Steps You should start MATLAB by simply typing matlab in your Unix system. If all goes well you will see a MATLAB prompt >> inviting you to initiate a al ulation. In what follows! any line beginning with "" indi ates typed input to MATLAB. You are expe ted to type what follows by not the "" prompt itself. MATLAB supplies that automati ally. Arithmetic with MATLAB MATLAB understands the basi arithmeti operations# \$! %! &! '. (owers are indi ated with )! thus typing >> 5*4 + 3^2 and pressing enter! results in ans = 29 The laws of pre eden e are built in but if in doubt! you should put parentheses appropriately. *or example! >> 7+3*(2/(8-2)) ans = 8 The sort of elementary fun tions familiar on hand al ulators are also available. *or example! >> sq t(3^2+4*4) ans = 5 >> e!p(l"#(3\$2)) ans = 3\$2

If you now type . *or example the volume of a sphere an be al ulated via the following se+uen e# >> adius = 3% >> &"lume=(4/3)*pi* adius^3 &"lume = ''3\$(973 . At any point typing the ommand! >> /)" tells about all the variables that are in the wor0spa e of the urrent MATLAB session.Using Variables You an assign numeri al values to variables for use in subse+uent al ulations.).ote that the first line did not seem to produ e a result in s reen.ined . >> &=0'23251% >> /='3235% &a iable . Vectors A ve tor an be input in several ways. This is useful if you want to avoid luttering up the s reen with intermediate results. In identally! a +ui 0 way of repeating a previous MATLAB instru tion is to press the 1up%arrow2 0ey until you re over the ommand you need. You may also use the sideways arrows to modify any of the previous ommands. *or example if you have followed the above example with! >> !=4*pi* adius*) you should get the result! *** +nde. It really does obey the instru tion and re ords the value of the variable in the memory. /a h variable must somehow be assigned before you ma0e use of it in further al ulations.un-ti"n " >> )=3% >> !=4*pi* adius*) you should have more su ess. *or example! >> u=0' 3 51 . it suppresses any visual onfirmation.\$ This is self%explanatory. -hen MATLAB en ounters an instru tion followed by a semi% olon .

*or example! >> u5 ans = ' 3 5 The differen e between the ve tors u and u2 an be understood by typing the ommands >> si4e(u) >> si4e(u5) whi h yields the row si4e followed by the olumn si4e for ea h ve tor. To get a orresponding column ve tor! we invo0e the transpose of a ve tor. 3ne an now multiply ve tors appropriately to either yield an innerproduct >> 4=u*u5 or a matrix! >> 4=u5*u Multipli ation of ve tors only happens if the inner matrix dimensions agree. To save a ertain part of the wor0 in the MATLAB session! one an type! >> dia y /" 8' >> 9\$ >> 9\$ .)6 an be understood to be the orresponding element%by%element operations. *or example! >> u*u would yield! *** 6 7nne " usin# ==> * mat i! dimensi"ns must a# ee\$ 5uppose we want a set of values 4 given by 4 = u2 then we want >> 4 = u\$*u% where the . is inserted before the & symbol whi h for es an element%by%element operation.'v and u. 3ne an perform ve tor operations su h as! >> 4=u+&-/% .ote that the ve tors des ribed so far are row ve tors.All three ommands above yield the same ve tor. 5imilarly u.

? C is a 6 by 9 olumn matrix ""D 7 8=! E!%:. < = :.>> dia y ". Matrices To define a matrix you start with its name. /xamples# "" B 7 8: %B < ? B is a 9 by 6 row matrix "" C 7 8:.. %: 9 >? In either ase you have defined the same 6 by 6 matrix named A and you an use it at any time in a al ulation or to define another matrix by using its name @AA. 3ne of the most useful ommands in MATLAB is the )elp ommand. *or example you an type! >> )elp sin >> )elp sq t and so on. <! =! :. %B.. 5+uare bra 0ets are used to begin and end ea h matrix. %:! 9! >? or this# "" A 7 89 : %.. <. All the wor0 between the diary ommands will be saved into a file alled work1. You an separate elements with a spa e or a omma! and end ea h row with a semi% olon li0e this# "" A 7 89! :! %. 6!%B! =? D is a : by 6 matrix You an edit individual elements of a matrix as follows# "" A@9!9A 7 %= hanges the element in row9 olumn9 of matrix A to %= .

"" A 7 8a b . "" b 7 8u. e f g. "" H 7 G@B! 9#6A. To solve the system of e+uations for @x! y! 4A using Faussian /limination# ax\$by\$ 47u ex\$fy\$g47v px\$+y\$r47w we perform the following steps in matlab. . multiplies B and C A is subtra ted from the produ t of C and B The symbol " @above the number ."" D@:!6A 7 > hanges the element in row: olumn6 of matrix D to > To perform operations on matri es you use the symbols @ +. . v. ! A from the number 0eypad or above the numbers a ross the top of the 0eyboard.A is used to raise a matrix to an exponent as follows# "" A)6 "" C&D ubes the matrix A @you might also use A&A&A for the same al ulationA an error message is displayed Matlab will give an error message when the al ulation annot be done be ause of a dimension mismat h. /xamples# "" A \$ A "" B & C "" C&B % A adds the matrix A to itself. w?. p + r?. "" G 7 rref@MA. "" M 7 8A b?.

load filename this ommand loads the variables that you saved in a previous session. * this is used for omments. /HIT TI/ MATLAB (G3FGAM. I have listed some here that you may find useful# "" si+e@AA "" in%@AA "" det@AA "" rref@AA "" A. gives the dimension of the matrix A al ulates the inverse of the matrix A ! if it exists. Matlab ignores any line that begins with J Matlab has many built in matrix fun tions and operators. this is the : by : identity builds the 4ero matrix of any dimension fills a matrix of any si4e with ones. displays elements of the matrix A as fra tions displays all numbers with 9= digits instead of the usual B digits "" e)e @:!:A "" +eros @6!6A "" ones @6!:A "" rats@AA "" format long . This is helpful when you enter large matri es that you want to wor0 with at a later date. 3nly use this ommand if you want to lose everything. At the osf9 prompt! type# lpr &st''( filename @note# st::> is the printer in 5T I room ::>.3T the whole sessionA that you have defined in the urrent session. At other lo ations you will use another printer name after the %& A who wh) displays variables you have defined in your urrent session matlab answers the +uestion.LY the variables % .@again and againA clear lears all variables from your urrent session. al ulates the determinant of the matrix A al ulates the row redu ed e helon form of the matrix A forms the transpose of the matrix A. sa%e filename this ommand will save all variables @and 3.Iere are some basi matlab ommands that you will need# #\$it or exit either of these loses the program and ends your matlab session. lpr this is the ommand used to print your diary file.

ote that the )"ld "n ommand tells MATLAB to retain the most re ent graph so that a new graph an be plotted. ap) ".&lotting Basics 5uppose we wish to plot the graph y=!2 in the interval 8%9! 9?! then Kust type! >> ! = -'3(\$'3' >> y=!\$*! >> pl"t(!2y) . y=!^25) >> !label(:!5) >> ylabel(:y5) 5uppose now we want to plot the graphs y'=!2 and y:7:x on the same figure! we need! >> y'=!\$*!% >> y2=2*!% >> pl"t(!2y') >> )"ld "n >> pl"t(!2y225 "5) . 3ne an add more features to the plot using the following ommands# >> title(:. . More options on the hoi e for the olor and symbol an be found by typing! >> )elp pl"t .ote that axes are adKusted and the se ond urve is plotted with a red ir le.ote that the axes are automati ally hosen to suit the range of variables used.

Sign up to vote on this title