中国高校课件下载中心 》 教学资源 》 大学文库

西安建筑科技大学:《高等数学计算方法》课程教学资源(教材讲义)Appendix_An Introduction to MATLAB

文档信息
资源类别:文库
文档格式:PDF
文档页数:7
文件大小:96.83KB
团购合买:点击进入团购
内容简介
西安建筑科技大学:《高等数学计算方法》课程教学资源(教材讲义)Appendix_An Introduction to MATLAB
刷新页面文档预览

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   !          & .  ￾ %*       !               !      !         !           *   ￾ +      - .  <

6o, j)aate t 3a, er oee [2ciB=[1234 6 ie the t3a, er oee 0=B 13 24 3*(B*C)3 3(BC* 1308029568 2956866840 Array Operations th 1 B r acbaae ie the, ) n 0=, ctio, e that ca, or eate o, the i, (ivi al elen e, te o=a n at 3i2c t hie s ae(en o, et 3ate( ea3lie3 s he, the coei, e o=the e, t3iee o=as Z n at312 s ae ta5e, c t he n at 3i2 or eatio, e o=a((itio, Oe)bt3actio, Oa,( ecala3 n )Itir lica tio, alea(y or eate elen e, ts ieeOb)t the n at 3i2 or e3atio, e o=n )Itir licatio, 0 (ivieio, Oa,( ros e3(o, otc t heee thee or e3atio, e ca, be n a( to or eate elen e, ts iee by r 3ece(i, a then s ith a r e3io(Y cocoa, (.c ct ie in rota,t to),(e3eta,( hos a, s he, to )ee theee or e3atio, ec 1 ay or e3atio, e a3 c3)cial to the efficie, t co, et 3 ctio, a,( e2ec)tio, 0=7 1t 41Br 3oa3an e a,( a ar hi [2ciA=[1234 A 2 r 300 cee the n at 312 r 300ct AA 710 eq) a3ee each e, t2y o=1 ans 916 COS(A /2) (ivi(ee each e, t3y o=l by 20the, ta5ee the coei, e o=each e, t 3y 0.87760.5403 0.0707-0.4161 Graph 7 lt 41 B ca, r 30(ce ts o. a,( thee. (in e, eio, al r lote o=c)3vee a,( e)3 -aceec Or tio, e a,( a((itio, al =eat )3ee o=a ar hice i, 7 1t 41 B ca, be =0),( the o.li )n e,t t he r lot con n a,( ie )ee( to ae, eate aar he o=ts o ( in e, eio, al=),ctio,ec t he =ollos i, a e2an r le s ill cate the r lot o=the a ar he y= coe(a-a,( y coe Tr- ove3 the i, te3val 0, T]c 0:0.1

￾ $ >  ￾   /  6/ * 0* $76- $       7   6 7- <"#   % &$% &$% $$% ￾     6             ￾                         ￾                  0  1    ￾                  !                       ￾              !        4 ? 5  -                  !         Æ          ￾       /  ./ * 0* .-       ..     .-     !      & $  .      !   ! ;         !  %$   #$   ￾                   6            ￾             !      ￾                  ￾                 7  "#   7  ￾"#      @A B /  55* 8

s(x) z=Co5(x).2; plot(xy,x,z,o”) The first line specifies the domain with a step size of 0. 1. The next two lines define the two functions Note that the first three lines all end a semicolon The semicolon is necessary to suppress the echoing of the matrix x, y, and z on the command screen. The fourt h line contains the plot command that produces the graph. The first two terms in the plot command, x, and y, plot the function y= cos(a). The third and fourth terms, x and z, produce the plot of y= cos(r). The last term,'o, results in o's being plotted at each point (k, Ek)where zk= cos(ak) The graphics command fplot is a useful alternative to the plot command The form of the command is fplot('name'la, b], n). This command creates a plot of the function name. m by sampling n points in the int erval [a, b]. The default number for n is 25 Ex. >fplot tanh, [-2,2 plot y= tanh(a) over[-2, 2 The plot and plot 3 commands are used to graph parametric curves in two and three-dimensional space, respectively. These commands are particularly useful in the visualization of the solut ions of different ial equations in two and three Ex. The plot of the ellipse c(t)=(2cos(t),3sin(t), w here0≤t≤2丌,is roduced with the follow t=0:0.2:2*p >>plot (2*cos(t ), 3*sin(t)) Ex. The plot of the curve c(t)=(2cos(x),t2,1/t), w here0.1≤t≤4丌, produced with the following commands t=0.1:0.1:4*pi plot(2*cos(t), t., 1 /t) Three-dimensional surface plot s are obt ained by specifying a rectangular sub- set of the domain of a function with the meshgrid command and then using the mesh or surf commands to obtain a graph. These graphs are helpful in visualizing the solutions of partial differential equations Ex.>x=pi:0.1; >>y=X x,yI=meshgrid(x,y) >>z=sin(cos(x+y)); Lool d conditionals Relational Operator Equal t Not less than Greater than

 * 2 - * ￾ 2-- ￾ &   &       =  A0 ￾    &     3    &          ￾      !            =       ￾                 ￾ &                   7  "# ￾        2       7  ￾"# ￾    ) )    )        "￾ ￾#   ￾ 7  ￾"￾# ￾     '￾         ￾    ￾        '￾ "-(-/0# ￾             !          @  B ￾      ;1 /  '￾ -￾ -/#  0   7 "#  @￾; ;B ￾ ￾  ￾                       ! ￾        !    =        :           /  ￾     "# 7 ";  "# < "##   A    ;            C ￾5 5 * ￾  ￾  ￾ /  ￾      "# 7 ";  "# ￾  0￾#   A0    8            C ￾55 * ￾  ￾￾-￾ ￾           ! !                                 ￾       =        :    C /  #5** * /0( 8 * 2   * ( 2       '  6    /  3      D    7    1

Greater than or equal to Logical operators Not(Complement) & And(True if both operands are true Or (True if eit her or both operands are true Boolean values 0 False The for, if, and while statements in MatlaB operate a manner analogous to their counterparts in other programming languages. These st atements have for(loop-variable= loop-expression) xecutable-statements f(logical-expression executa ble-statements end while(while-expression executable-statements while(while-expression executable-statements The following example shows how to use nested loops to generate a mat The follow ing file was saved as a M-file named nest. m. Typing nest in the MATLAB Command Window produces the matrix A Note, when viewed from the upper-left cornner, that the entries of the matrix A are the entries in Ex. for i=1: 5 A〔i,1)=1:A(1,) nd for i=2. 5 2:5 A(j)=A(j1)+A(-1 end The break command is used to exit from a loop Ex. for k=1: 100 qr if(k>10)&(x-foor(x)==0)

7 D       6   ￾ 3 "$  # 9  "￾        #  6 "￾          #   2    E ￾ ' '         ￾                     ￾            4 ' "    7     #        8 ' "    #        8   "   #        8   "   #        8 ￾                    ￾    &     &    ￾!    ￾ $  %       3                          .)  /  ' 5 . *. * 8 '  5 ' : 5 . :. :# . #:* 8 8 . ￾ ;           /  ' ;5 !￾ ;* ' ;F #"  ; 8 8 G

k - he dewi comma d ca be used to display text or a matrix Ex. nf A kf a bes k<f Ⅴfkcr dowi dv v'xvrju kf kt A Programs A efficie t way to col struct programs is to use user-defi ed ful ctio s. -hese fu ction s are saved as M-files. -hese programs allow the user to specify the il put al d output parameters. -hey are easily called as subroutil es i other programs. -he follow il g example allows ol e to visualize the effects of modil g out Pascal's trial gle w ith a prime umber. -ype the follow il g ful ctio i the MatLAB Editor /Debugger al d thel save it as al M-file amed pasc.m Ex. fum kom Pf i gw dmmu %Imiut)mew Tbs num Bsp of Foa w )m ew Tbs i pems num Bsp %Ouiut )i ewPgw glw Tegnqls PcirAf A PdAjuf A fop kf x: m fopjf x: m Pckijuf psmdPckij)Aumut psmdPck)Aljumu; Now i the MA-LAB Comma d wil dow el ter Pf i gw d5r u to see the first five rows of Pascal's tria gle mod r- Or try Pf i gw dA\Sr it( ote the semi colo))al d the type wi hPu(gel erates a sparse matrix for large values of n onclusion At this poil t the reader should be able to create al d modify programs bas ol the algorithms i this textbook. Additio al i] formatio Imal ds formatio regardil g the use of MA-LAB ol your particular platform cal be foul d i the ol - e help facility or i the docume tation accompa yil g the

; ￾ 8      !     /  * ;*   ; ; * 8 / - - 0 ;; * 8     Æ !           &   ￾       & ￾        !          ￾!   !           ￾         =  :      .)         ￾!           / 5,       &   /  '+￾ +￾ #   ￾  +( '  = # (  ￾  ( +( =?+￾+￾ #  <- ￾ ' :5 < :*< :* 8 ' ; 5 ' : 5 < ;:( < ;:#( ( < ;#:(* 8 8 3    ￾ $  %    <     &  &   .)  (8  6 ! <  C "      #   !  < "            #                    !                               ￾  !                !       !      H

已到末页,全文结束
刷新页面下载完整文档
VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
相关文档