comtrol engineering lab1

Upload: johnyrock05

Post on 08-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 comtrol engineering lab1

    1/11

    MTS-362

    Control Engineering Lab-1

    Introduction to MATLAB its functions and applications

    •Matlab is a tool for doing numerical computations with matrices and vectors. It can

    also display information graphically. The best way to learn what Matlab can do is to work 

    through some examples at the computer.

    • Matlab program and script files always have filenames ending with ".m"; the

     programming language is exceptionally straightforward since almost every data object isassumed to be an array. raphical output is available to supplement numerical results.

    1! Entering t"e #atrices and si#ple #atri$ #anipulation

    11 Matrices

    To enter the matrix!

      # $

    and store it in a variable a% do this&'' a ( ) ! ; # $ *

    To redisplay the matrix% just type its name&'' a

    +nce you know how to enter and display matrices% it is easy to compute with them. ,irst wewill s-uare the matrix a &

    '' a a

    /asn0t that easy1 2ow we0ll try something a little harder. ,irst we define a matrix b&

    '' b ( ) ! ; 3 ! *

    Then we compute the product ab&'' ab

    ,inally% we compute the product in the other order&

    '' ba

     2otice that the two products are different& matrix multiplication is non commutative.

    4ontrol 5ngineering 6ab !7!!

  • 8/19/2019 comtrol engineering lab1

    2/11

    To perform element8wise multiplication rather than matrix multiplication% use the .

    operator&

     p ( a.a

    The matrix operators for multiplication% division% and power each have a correspondingarray operator that operates element8wise. ,or example% raise each element of a to the

    third power&

    a.9#

    +f course% we can also add matrices&

    '' a : b

     2ow let0s store the result of this addition so that we can use it later&

    '' s ( a : bMatrices can sometimes be inverted&

    '' invs<

    To check that this is correct% we compute the product of s and its inverse&

    '' s invs<

    The result is the unit% or identity matrix. /e can also write the computation as

    '' s7s

    /e can also write

    '' s=s

    which is the same as

    '' invs< s

    To see that these operations% left and right division% are really different% we do the following&

    '' a7b

      '' a=b

     2ot all matrices can be inverted% or used as the denominator in matrix division&

    '' c ( ) ! !; ! ! *

      '' invc

  • 8/19/2019 comtrol engineering lab1

    3/11

    > matrix can be inverted if and only if its determinant is non?ero&

    '' deta<

      '' detc<

    > transpose of a matrix'' a@

    Concatenation

    Concatenation is the process of joining arrays to make larger ones. In fact% you made your 

    first array by concatenating its individual elements. The pair of s-uare brackets )* is theconcatenation operator.

    > ( )a%a*

    > (  ! # ! #

      $ A B $ A B

      C D !3 C D !3

    12 Building Matrices

    Matlab has many types of matrices which are built into the system. > C by C matrix withrandom entries is produced by typing

    randC<

    Eou can generate random matrices of other si?es and get help on the rand command within

    matlab&

    rand%A<

    help rand

    >nother special matrix% called a Filbert matrix% is a standard example in numerical linear

    algebra.

    hilbA<

    help hilb

    > A by A magic s-uare is given by the next command&

    magicA<

    help magic

    > magic s-uare is a s-uare matrix which has e-ual sums along all its rows and columns.

    /e0ll use matrix multiplication to check this property a bit later.

    4ontrol 5ngineering 6ab #7!!

  • 8/19/2019 comtrol engineering lab1

    4/11

    Gome of the standard matrices from linear algebra are easily produced&

    eyeB<

    ?eros$%C<

    onesA<

    Eou can also build matrices of your own with any entries that you may want

    13 %etting &or'space infor#ation

    >t any time you want to know the active variables you can use who&

    who

    To get the complete information we will write

    /hos

    '' whos  2ame Gi?e Hytes 4lass

      > #x# C double array

      a !x! D double array

      x #x! $ double array

    To clear the screen(

    4lc

    To clear the memory(

    4lear 

    4lear variables<

    4olon 2otation(

    Matlab offers some powerful methods for creating arrays and for taking them apart.

    x(8&!

    lengthx<

    x( 8&.A&!

    4ontrol 5ngineering 6ab $7!!

  • 8/19/2019 comtrol engineering lab1

    5/11

    x( 8&.&!

    a(magicA<

    a%#<

     2ow we will use the colon notation to select a column of a.

    a%&<

    a&%#<

    a

    a&$%&<

    a&%#&A<

    a&$%#&A<

    a!&&A%&<

    Eou can put a vector into a row or column position within a.

    a&%)! A*<

    a) A*%) $ A*<

    1) Co#ple$ *u#bers and Matrices(

    (# :$i or ?(# :$j

    There are at least two convenient ways to enter complex matrices.

    >()! ;# $* : i)A B;C D*

    >nd

    >()!:Ai :Bi;#:Ci $:Di*

    1+ Miscellaneous co##ands(

    4ontrol 5ngineering 6ab A7!!

  • 8/19/2019 comtrol engineering lab1

    6/11

    The M>T6>H display only shows A digits in the default mode. The fact is that M>T6>H

    always keeps and computes in a double precision !B decimal places and rounds the display to

    $ digits. The command

    format long

    will switch to display all !B digits and

    format short

    will return to the shorter display. It is also possible to toggle back and forth in the scientific

    notation display with the commands

    format short e

    and

    format long e

    It is not always necessary for M>T6>H to display the results of a command to the screen. If you do not want the matrix > displayed% put a semicolon after it% >;. /hen M>T6>H is

    ready to proceed% the prompt '' will appear. Try this on a matrix right now.

    Gometimes you will have spent much time creating matrices in the course of your M>T6>H

    session and you would like to use these same matrices in your next session. Eou can savethese values in a file by typing

    save filename

    This creates a file

    filename.mat

    16 S,LI*% SIM.LTA*E,.S LI*EA/ E0.ATI,*S

      +ne of the most common applications of matrix algebra occursin the solution of linear 

    simultaneous e-uations. 4onsider a set of n e-uations for which the unknowns are x!%

    x% ....... xn.

      a!!x! : a!x : a!#x# : ............... a!nxn ( b!

      a!x! : ax : a#x# : ............... anxn ( b

      a#!x! : a#x : a##x# : ............... a#nxn ( b#  . . . . .

    . . . . .

    4ontrol 5ngineering 6ab B7!!

  • 8/19/2019 comtrol engineering lab1

    7/11

      . . . . .

      . . . . .

      an!x! : anx : an#x# : ............... annxn ( bn 

    The matrix format for these e-uations is

      )a*)x* ( )b*

    where

      a!! a! a!# .... a!n x! b!

      a! a a# .....an x b

      )a* ( a#! a# a## .... a#n )x* ( x# )b* ( b#  . . . . . .

      . . . . . .

      . . . . . .

      . . . . . .  an! an an# .... ann xn bn

     2ote that only when the e-uations are linear in the unknown xi0s is the matrix formulation

     possible.

    The classical matrix solution to this e-uation is based on the definition of the inverse of a

    matrix. The inverse of a matrix is that matrix which when multiplied by the original matrix%

    results in the identity matrix. Then

      a8! a ( I

    where a8! denotes the 0inverse of matrix a0 and I denotes the identity matrix of the same order as matrix 0a0. If 0a0 is a known coefficient matrix and if 0b0 is a column vector of known terms%

    the problem is one of finding the n vales of x!% x% .... xn that satisfy these simultaneous

    e-uations. Jre8multiplying both sides of the e-uation by the inverse of 0a0 gives

      )a8!*)a*)x* ( )a8!*)b*

    or 

      )x* ( )a8!*)b*

    Thus if we find the inverse of the coefficient matrix% the product of the inverse times the

    matrix of non8homogeneous terms% b% yields the unknown matrix% x. To observe the simple property of the inverse% define the $x$ matrix% 4

      4 ( )! 8$ # ; # ! 8 !; ! ! 8!; 8! # !*

    calculate the inverse using the 0matlab0 command% inv

  • 8/19/2019 comtrol engineering lab1

    8/11

    and note that

    4Kinverse 4 ( )! 3 3 3;3 ! 3 3;3 3 ! 3;3 3 3 !*

    where the right hand side is the $x$ identity matrix.

      /e leave to a study of linear algebra% the method and steps re-uired to calculate the

    inverse of a matrix. Guffice it to say here that for systems of e-uations numbering in the

    hundreds or thousands% which often occur in complex engineering problems% the calculationof the matrix inverse is a time consuming operation even for modern computers.

    >s an example of solving a system of e-uations using the matrix inverse% consider the

    following system of three e-uations.

      x! 8 $x : #x# ( 8C

      #x! : x 8 x# ( !$

      x! : x : x# ( A

    Lsing 0matlab0% define the two matrices for )a* and )b*.

      a ( ) ! 8$ #; # ! 8; ! !*;

      b ( ) 8C; !$; A*;

    ,ind the solution vector% x% by

      x ( inva

  • 8/19/2019 comtrol engineering lab1

    9/11

      !#x 8 !!x# ( #A

      #$7!#

  • 8/19/2019 comtrol engineering lab1

    10/11

    Night ivision. The matrix format in which right division is employed is less common but no

    less successful in solving the problem of simultaneous e-uations. Night ivision is invokedwhen the e-uations are written in the form

      )x*)>* ( )H*

     2ote that in this form )x* and )H* are row vectors rather than column vectors as above. This

    form represent a !xn matrix x< pre8 multiplying a nxn matrix >* ()H*if 

      x ( )x! x x#* H ( ) 8C !$ A*

    and

    ! #

      )>* ( 8$ ! !  # 8 !

     2ote that )>* is e-ual to the transpose of )a*.

      > ( a0

    Faving so defined > and H% the solution for x can be obtained by

    right division.

      x ( H7>

    results in

      x ( ) #

      !

      8 *

    4ontrol 5ngineering 6ab !37!!

  • 8/19/2019 comtrol engineering lab1

    11/11

    1 E$ercise(

    !. ,ind the solution to

      r : s : t : w ( $

      r 8 s : w (

      #r : s 8 t 8 w (

      r 8 s 8 #t : w ( 8#

    using the matrix inverse and left and right division.

    . ,ind the solution to

      x! : x 8 $x# : Bx$ : #xA 8 xB ( !B

      8x! : x : #x# : Ax$ 8 xA ( 8C

      x! 8 x 8 Ax# : #x$ : xA : xB ( !

      $x! : #x 8 x# : x$ : xB ( 8!

      #x! : x 8 x# : $x$ : #xA : BxB ( 8!!

      Ax! : x 8 x# : #x$ : xA : xB ( A

    using the matrix inverse and left and right division.

    4ontrol 5ngineering 6ab !!7!!