Вы находитесь на странице: 1из 7

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

Using Variables You an assign numeri al values to variables for use in subse+uent al ulations. *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 ,ote that the first line did not seem to produ e a result in s reen. -hen MATLAB en ounters an instru tion followed by a semi% olon . it suppresses any visual onfirmation. It really does obey the instru tion and re ords the value of the variable in the memory. This is useful if you want to avoid luttering up the s reen with intermediate results. /a h variable must somehow be assigned before you ma0e use of it in further al ulations. *or example if you have followed the above example with! >> !=4*pi* adius*) you should get the result! *** +nde,ined ,un-ti"n " >> )=3% >> !=4*pi* adius*) you should have more su ess. 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. At any point typing the ommand! >> /)" tells about all the variables that are in the wor0spa e of the urrent MATLAB session. Vectors A ve tor an be input in several ways. *or example! >> u=0' 3 51 ; >> &=0'23251% >> /='3235% &a iable .).$ This is self%explanatory. If you now type

All three ommands above yield the same ve tor. 3ne an perform ve tor operations su h as! >> 4=u+&-/% ,ote that the ve tors des ribed so far are row ve tors. To get a orresponding column ve tor! we invo0e the transpose of a ve tor. *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. 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. *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. 5imilarly u.'v and u.)6 an be understood to be the orresponding element%by%element operations. To save a ertain part of the wor0 in the MATLAB session! one an type! >> dia y /" 8' >> 9$ >> 9$

>> dia y ",, All the wor0 between the diary ommands will be saved into a file alled work1. 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. Matrices To define a matrix you start with its name. 5+uare bra 0ets are used to begin and end ea h matrix. 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! :! %;. <! =! :. %:! 9! >? or this# "" A 7 89 : %;. < = :. %: 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. /xamples# "" B 7 8: %B < ? B is a 9 by 6 row matrix

"" C 7 8:. %B. <. ?

C is a 6 by 9 olumn matrix

""D 7 8=! E!%:. 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 %=

"" D@:!6A 7 >

hanges the element in row: olumn6 of matrix D to >

To perform operations on matri es you use the symbols @ +, , ! A from the number 0eypad or above the numbers a ross the top of the 0eyboard. /xamples# "" A $ A "" B & C "" C&B % A adds the matrix A to itself. multiplies B and C A is subtra ted from the produ t of C and B

The symbol " @above the number ;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. 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. "" A 7 8a b . e f g. p + r?. "" b 7 8u. v. w?. "" M 7 8A b?. "" G 7 rref@MA. "" H 7 G@B! 9#6A.

Iere are some basi matlab ommands that you will need# #$it or exit either of these loses the program and ends your matlab session.

sa%e filename this ommand will save all variables @and 3,LY the variables % ,3T the whole sessionA that you have defined in the urrent session. This is helpful when you enter large matri es that you want to wor0 with at a later date. load filename this ommand loads the variables that you saved in a previous session.

lpr this is the ommand used to print your diary file. /HIT TI/ MATLAB (G3FGAM. At the osf9 prompt! type# lpr &st''( filename @note# st::> is the printer in 5T I room ::>. 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.@again and againA

clear lears all variables from your urrent session. 3nly use this ommand if you want to lose everything. * this is used for omments. Matlab ignores any line that begins with J

Matlab has many built in matrix fun tions and operators. 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. 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. 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

&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) ,ote that the axes are automati ally hosen to suit the range of variables used. 3ne an add more features to the plot using the following ommands# >> title(:; ap) ", 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) ,ote that the )"ld "n ommand tells MATLAB to retain the most re ent graph so that a new graph an be plotted. ,ote that axes are adKusted and the se ond urve is plotted with a red ir le. More options on the hoi e for the olor and symbol an be found by typing! >> )elp pl"t

Вам также может понравиться