ME305: Introduction to System Dynamics Using MATLAB MATLAB stands for MATrix LABoratory and is a powerful tool for general scientific and engineering computations. Combining with user-friendly graphics interface, MATLAB is able to present sophisticated plots and graphics. 1. Invoking and Exiting MATLAB You can start MATLAB either from an X terminal or an ASCII terminal. The major difference of the two is that in an ASCII terminal you cannot see the results of graphics commands. The following instructions show you how to invoke MATLAB (Please note that if you have MATLAB on a PC, then you can skip this section.): Using an ASCII terminal: simply type matlab at the UNIX prompt. Using an X terminal or a work-station: Make sure you are in windows environment, i.e. such as done through openwin command. When you invoke MATLAB and get the message of MATLAB will run, but no windows will open, you need to set the DISPLAY environment so as to properly show the results of graphics command in your program. You can use either one of the following commands: setenv DISPLAY terminalname:0 or matlab -display terminalname:0 where terninalname is the name of the station on which you are working, and is shown on the top of the terminal (e.g. ranki). When you successfully invoke MATLAB, you will get the MATLAB prompt: >>. If you have problem with the path setting, you need to modify your.cshrc file by either appending the path /usr/site/bin to any of set-path line, or simply adding a new line set path = ($path, /usr/site/bin) to the end of the file. To activate the modification without re-logging, you can use the command source.cshrc<cr>. To exit Matlab, simply enter exit or quit at the MATLAB prompt. ME 305: System Dynamics Using MATLAB page 1
2. Fundamentals 2.1 How Does MATLAB Work? MATLAB can either work interactively by interpreting each command entered through keyboard, or use an M-file which stores a long sequence of commands. In the former case, MATLAB works like a calculator which shows you the result right after entering a sequence of operations. In the later case, an M-file functions like a program which collects a sequence of instructions. To create an M-file, you can use VI editor, text editor, or other editors available, and store the file with the extension.m, such as, homework.m. To invoke an M-file, simply enter the name, without the extension at the MATLAB prompt, for example, >>homework<cr> More information on these files will be provided later. 2.2 How to Create a Matrix? The easiest way to create a matrix is to explicitly list all the elements of the matrix in brackets [ ]. In the brackets, each element is separated by a blank or comma; each row ends with a semicolon. To create a matrix, say A, we can enter A = [1 0 0; 0 1 0; 0 0 1]<CR> or as the following: A = [1 0 0<CR> 0 1 0<CR> 0 0 1 ]<CR> In addition, you can use the following built-in functions to create a matrix: eye: to create an identity matrix eye(n) creates a n-by-n identity matrix. eye(m,n) creates an m-by-n matrix with ones on the diagonal and zeros elsewhere. eye(size(a)) creates an identity matrix with the same size as matrix A. ones: to create a matrix with all ones as its elements. one(n) creates a n-by-n ones matrix. one(m,n) creates an m-by-n ones matrix. one(size(a)) creates an ones matrix with the same size as matrix A. zeros: to create a matrix with all zeros as its elements. zero(n) creates a n-by-n zeros matrix. zero(m,n) creates an m-by-n zeros matrix. zero(size(a)) creates an zeros matrix with the same size as matrix A. ME 305: System Dynamics Using MATLAB page 2
For example, you can define matrix A as a 3-by-3 identity matrix by entering A=eye(3). You will obtain a matrix A exactly the same as the one you entered before. You can also use linspace, logspace, rand, randn, meshgrid, and so on, to create a matrix. 2.2 How to Create a Vector? To Create a vector simply enter all the elements of the vector in the brackets [ ].In the brackets, each element is separated by a blank or comma. For example, to create a vector, say B, we can enter B = []<CR> 2.2 Some Basic Operators and Important Symbols Assume that A and B are two vectors, say A = 1 2 and B = 3 4, Y is a square matrix, say Y = 1 2, and p is a scalar. 3 4 Arithmetic Operators: Operator Description Usage Operator Description Usage + addition C=A+B C=p+B - subtraction C=A-B C=p-B * matrix multiplication C=A'*B C=A*B' C=p*A.* array multiplication C=A.*B C=p.*A C=A.*p C=A*p / matrix left division (XA=B X=A/B) C=A/B C=A/p./ array left division C=A./B C=p./B \ matrix right division (AX=B X=A\B) C=A\B C=p\A.\ array right division C=A.\B C=p.\B ^ matrix power C=Y^p.^ array power C=A.^B (Y must be a squarec=p^y matrix) C=A.^p C=p.^B ' matrix transpose C=A'.' array transpose C=A.' The dimensions of vectors and matrices are important in these arithmetic operations. You may try C=A*B, C=A'.*B, C=A.*B' and see what you get. ME 305: System Dynamics Using MATLAB page 3
Relational Operators: perform element-by-element comparisons between two matrices of equal dimensions. The result is a matrix of the same size, with elements of ones and zeros. Operator Description Usage Operator Description Usage > greater than A>B >= greater than or equal A>=B < less than A<B <= less than or equal A<=B == equal A==B ~ = not equal A~=B Logical Operators: perform element-by-element operations between two matrices of equal dimensions. The result is a matrix of the same size, with elements of ones and zeros. Operator Description Usage & AND C=A&B OR C=A B ~ NOT C=~A Important Symbols: Symbol Description Usage (or example) % Denotes a comment % This is a comment.! Execute a command available!ls -al<cr> in the operating system : Creates vectors, matrixi:j = [i,i+1,i+2,...,j] subscripts, and for iterations. i:k:j = [i, i+k, i+2k,..., j] C=X(:,j) -> take j-th column as vector C C=X(i,:) -> take i-th row as vector C (Try X(:,:), X(i:j), X(:, i:j), X(i:j,:), X(:)) 3. Complex Numbers for i = 1:10 You can also use complex numbers in MATLAB. In MATLAB, i, j, and sqrt(-1) are the same. Therefore, you can use any of them to express a complex unit, for example, a=3+4i. Similarly, you can create a complex matrix as the following: A = [1 2]+i*[3, 4] or A = [1+3*i, 2+4*i] Notice that there is no blank spaces surrounding + sign. You can also use the operators presented in precious section in complex vectors and matrices. ME 305: System Dynamics Using MATLAB page 4
Solution to Linear Equations To solve linear systems of equations, you can use either direct methods or iterative methods. 1. Direct Methods 1.1 Using Matrix Division Operators Given the following linear system of equations 3x x + 2x = 12 x + 2x + 3x = 11 2x 2x x = 2 The above system can be written as: AX = B, where A is a 3X3 matrix, and X and B are column vectors. We can solve for X = x x x T using the following steps: Step 1: Create matrix A, A=[3-1 2; ; 2-2 -1] Step 2: Create vector B, B=[12; 11; 2] Step 3: Solve for X, X=A\B' 1.2 LU Decomposition You can use this method only when matrix A is a square matrix. The above procedure in the MATLAB utilizes methods similar to Gaussian elimination. MATLAB also has the functions available for LU decomposition. This is slightly different from the formulation in the text-book. To produce two matrices, L, and U using MATLAB, use the following steps (assuming that matrix A is already entered). [L, U] = lu(a) This will print L and U after the execution of this command. The above command utilizes dual assignment capability of the MATLAB, since the function "lu" creates two outputs. Once you have the LU decomposition of matrix A, you can solve AX = B as follows: y = L\B, X = U\y ME 305: System Dynamics Using MATLAB page 5
Learning MATLAB Online When you are within MATLAB on any computer, 1. Type demo at >> prompt. 2. Click on matrices in demowindow. 3. Double click on Basic Matrix operations ( Slideshow player will start) a) Click on start >> and then click on next slide to see the slide show. There are other options like b) Inverse of Matrices c) Graphs and Matrices d) Sparse matrics e) Matrix Manipulation f) Eigen and Singular value show g) Command line demos 4. Type helpwin (Matlab window will open) To know all the general commands, operators. Ex. Double click on matlab/general to know all the general purpose commands Double click on matlab/ops to know all the operators and special characters etc. ME 305: System Dynamics Using MATLAB page 6