Page 823 - Mechatronics with Experiments
P. 823

®
                                                      ®
                                                MATLAB , SIMULINK , STATEFLOW, AND AUTO-CODE GENERATION  809
                                                                 ®
                                  The main data structure in MATLAB is a “matrix.” A scalar is considered a 1x1
                                                                         ®
                             matrix. A vector is considered a row matrix. MATLAB does not require the data type of
                             a variable or matrix be declared. By default, all numerical data is treated as double.
                                  The percent sign, %, is used to indicate the beginning of comments. The semi column
                             “ ; ” at the end of a line suppresses the echo of the data to the screen. Three dots are used
                             to indicate that the command line continues in the next line.
                                  Variable names must start with a character, and are case sensitive, so x and X are
                             different variable names.
                                  Help on any topic can be obtained by

                             >>   help
                             >>   help topic

                             The display accuracy of the numerical data can be controlled by

                             >> format    short   ;
                             >> format    long     ;
                             >> format    short e ;    % to display in scientific notation
                             >> format    long   e  ;
                             >> format    bank      ;  % Display two decimals only, i.e. 5.40
                             >> format    rat       ;  % Display in fractional form,      i.e. 10/3
                             Matrix data is defined as follows,

                             >>    A = [   1      2     3
                                           4      5     6
                                           7      8     9  ]         ;
                             The same matrix, A, can also be defined with the following syntax,

                             >>   A = [ 1     2    3 ;   4    5    6 ;    7     8    9 ]   ;

                             Instead of spaces between matrix elements, comma “,” can be used,
                             >>   A = [ 1,2,3 ; 4,5,6      ; 7,8,9]   ;

                             A specific element of a matrix, (k,l) - row k, column l, is referenced as,

                             >>   x = A(k,l) ;
                             >>   A(k,l) = 5.0     ;
                             >>

                             A row, or a column or a sub-matrix of a matrix can be referenced (for reading and writing)
                             >>   row_k    = A(k, : )     ;
                             >>   col_l    = A(:, l )     ;
                             >>   B   = A(1:k, 10:20)     ;    %   B is a sub-matrix of A.
                                                                                        √
                             Note that MATLAB ®  uses variable names “i” and “j” for representing  −1 imaginary
                             number for complex variable definitions. In order to make sure we do not change the
                             definition of built in variables “i” and “j”, we should not use “i” and “j” in our programs as
                             data variables, such as index number to vectors and matrices. When a “clear” statement is
                             executed, the original definitions of the variables are restored.

                             >> clear
                             >> i
   818   819   820   821   822   823   824   825   826   827   828