MATLAB Premier Asst. Prof. Dr. Melik DÖLEN Middle East Technical University Department of Mechanical Engineering 0/0/04 ME 04
Outline! Introduction! Basic Features of MATLAB! Prompt Level and Basic Aritmetic Operations! Scalars, Vectors, and Matrices! Matrix Operations and MATLAB Functions! Graphics and Data Visualization! Programming in MATLAB! MATLAB in Control Engineering! Operations on Transfer Functions! Simulation of LTI Systems 0/0/04 ME 04
Introduction! MATrix LABoratory MATLAB is an interactive software for numerical / symbolic computations and graphics.! Originally designed for matrix computations, solving linear equation sets, data visualization, etc.! The capabilities of MATLAB can be extended through programs written in its own programming language called M-script.! Over the years, the capabilities of MATLAB in every aspect have widened rapidly.! It has now become an industry-standard computing environment and an outstanding tool for engineering education. 0/0/04 ME 04
MATLAB Prompt To get started, type one of these: helpwin, helpdesk, or demo. For product information, type tour or visit www.mathworks.com.»» (45 + 7)/ 7. Arithmetic operations: +, -, *, /, ^ ans (ANSwer) is a temporary variable storing the result of an instant calculation.» 00^.5*ans.006e+004» sqrt(ans) 00.09 Previous answer can be recalled. Scientific notation:.006 0 4 MATLAB offers a large number of predefined functions like sqrt, sin, cos, tan, exp, log, atan, abs, tanh, and more... 0/0/04 ME 04 4
MATLAB Objects» a =. a =.00 Result of various operations can be assigned to the variables you want to define. Variable names are case sensitive.» b = -9.75; Note that semicolon (;) supresses the display.» x = [; ; ] x = Column vector ( ) is defined: x =» y = [ ] y is a row vector ( ): y = y = [ ] 0/0/04 ME 04 5
MATLAB Objects (Cont d)» A = [ ; 4 5 6; 7 8 9] A = 4 5 6 7 8 9» whos Name Size Bytes Class A x 7 double array a x 8 double array ans x 8 double array b x 8 double array x x 4 double array y x 4 double array A is a matrix ( ): A = 4 5 6 7 8 9 whos displays the names of all variables defined in the MATLAB workspace (memory). Grand total is 8 elements using 44 bytes 0/0/04 ME 04 6
Special Variables» i» -5 + a*i 0 +.0000i -5.0000 +.00i» cos(pi) cos(π) -» a = inf;» /a 0 By default, i and j are defined as imaginary unit ( -). MATLAB allows storage of complex numbers and relevant operations on them. a = (see IEEE 754 standard!) 0/0/04 ME 04 7
Important Notes» sin(x) 0.845 0.909 0.4» help ans Most aritmetic operators and certain functions work for matrices and vectors as well. An extensive online help is available. ANS Most recent answer. ANS is the variable created automatically when expressions are not assigned to anything else. ANSwer. 0/0/04 ME 04 8
Array Manipulations» A(,) Returns the element at nd row, rd column. 6» A(:,) Returns all the elements at nd column. 5 8» A(,:) 7 8 9» A(:,:) 5 6 Returns all the elements at rd row. Returns submatrix. Column A = 4 5 6 7 8 9 Row 0/0/04 ME 04 9
Array Manipulations (Cont d)» B = [A; zeros(,); y] B = 4 5 6 7 8 9 0 0 0 Forms a new matrix B using the variables previously defined. Here, the command zeros(m,n) creates an (m by n) matrix consisting of 0 elements. B = [A] x [0] x [y] x B = 4 5 6 7 8 9 [ 0 0 0 ] [ ] Matrix A Zero vector Vector y 0/0/04 ME 04 0
Array Manipulations (Cont d)» B = [x ones(,) A] B = 4 5 6 7 8 9 Redefines matrix B using the variables previously defined. Here, the command ones(m,n) creates an (m by n) matrix consisting of s. B = [x] x [] x [A] x B = 4 5 6 7 8 9 Vector x Unity vector Matrix A 0/0/04 ME 04
Matrix Operations» A = [ ; 4 5 6; 7 8 9];» B = [ 0; 4 0 6; 0 8 9]» C = [ ; 4; 5 6];» C 5 4 6 Let us define these matrices. Matrix inversion using ( ) operator. The command transpose(c) serves for the same purpose.» det(a) DETerminant of matrix A. 0» inv(b) 0.4000 0.500-0.000 0.000-0.0750 0.0500-0.667 0.0667 0.0667 INVerse of matrix B. 0/0/04 ME 04
Matrix Operations (Cont d)» A+B 4 8 5 7 6 8» A+C??? Error using ==> + Matrix dimensions must agree.» A*C 8 49 64 76 00» C*A??? Error using ==> * Inner matrix dimensions must agree. MATLAB enables you to perform arithmetic operations (+,-,*) on matrices. Matrices must be conformable to carry out a certain operation. Matrix multiplication. Invalid operation due to a mismatch in matrix inner dimensions. 0/0/04 ME 04
Matrix Operations (Cont d)» A- - - 0 4 5 6» *A 6 9 5 8 4 7» A.*B 4 0 6 0 6 0 64 8 Scalars ( ) are exceptions in matrix operations. Multiplies every element of the matrix A by. Multiplies the corresponding elements of matrix A and B. The operator (.*) is called array multiplication. 0/0/04 ME 04 4
Solving Equation Sets Consider this equation set: x x 5x + x x 8x 4x + x + 0x = 8 = = Alternatively, we have 5 8 4 x x 0 x 8 = A x = Therefore, the solution becomes (A A) x = A b x = A b b To implement this in MATLAB, one defines the following:» A = [ -4; - ; 5-8 0];» b = [8; ; ];» x = A\b x = 5.5000.5000 0.500 The operator (\) is called left matrix divide and it essentially solves the linear system using Gauss-Seidel method. Note that x = inv(a)*b yields the same result. 0/0/04 ME 04 5
Advanced Matrix Functions» eig(a) Returns all the eigenvalues of matrix A. 5.0000 4.0000 -.0000» [V,D] = eig(a) V = D = Eigenvectors -0.707 0.8058-0.408-0.0000 0.060-0.865 0.707-0.5889-0.408 Eigenvalues 5.0000 0 0 0 4.0000 0 0 0 -.0000! lu (command) computes LU factorization of a matrix.! chol computes the cholesky factorization of a symmetric positive definite matrix.! qr computes the QR factorization of a matrix.! svd obtains singular value decomposition of a matrix.! norm computes various matrix or vector norms.! cond, condest, rcond estimates various condition numbers. 0/0/04 ME 04 6
Vectors» t = :5 t = 4 5» t = 0:.: Creates a row vector whose components increase arithmetically. Components can change by non-unit steps. t = Columns through 7 0 0.000 0.000 0.000 0.4000 0.5000 0.6000 Columns 8 through 0.7000 0.8000 0.9000.0000» t = linspace(0,,) Generates a vector with equally spaced elements that lie between 0 and. t = Columns through 7 0 0.000 0.000 0.000 0.4000 0.5000 0.6000 Columns 8 through 0.7000 0.8000 0.9000.0000 0/0/04 ME 04 7
D Graphics! MATLAB offers a wide variety of built-in data visualization functions.! Function plot is used to generate D graphs.! To obtain a graph of y=f(x), one needs to create the domain x and then form the range y with the corresponding function values.» x = linspace(0,*pi,00);» y = sin(x);» plot(x,y);» xlabel( x [rad] );» ylabel( y );» grid on; 0/0/04 ME 04 8
D Graphics (Cont d) As the second example, let us plot the following function: x y = f (x) = + x The corresponding MATLAB commands are as follows:» x = linspace(-5,5,00);» y = x./(+x.*x);» plot(x,y);» xlabel( x );» ylabel( y );» grid on; 0/0/04 ME 04 9
Programming! Entering all the commands at the MATLAB prompt sequentially is a tedious and slow process.! One can type in all the commands in a text file having an extension of m. The file containing these commands is called M-script.! When the name of this m file is keyed in just like a command at the prompt, MATLAB executes all the commands sequentially.! Similar to a high-level language, MATLAB provides a number of standard programming constructs such as loops and conditionals. 0/0/04 ME 04 0
Conditionals IF Statement! The use of conditionals in if <boolean expression > MATLAB statements elseif <boolean expression > MATLAB statements else MATLAB statements end Example if x == 0 y = 0; elseif x == y = ; else y = *(x.5); end MATLAB programs are very similar to those of common highlevel languages.! The logical operators in MATLAB are <, >, <=, >=, == (logical equal), ~= (not equal).! Boolean expressions take the values (true) or 0 (false). 0/0/04 ME 04
For Loop FOR Statement for variable = expression MATLAB statements end Example for k = :4 disp(k); end 4 Example for k = [ 7 ] disp(k); end 7! for loop repeats the statements as the loop index takes on the values in a given vector.! Like an if construct, the loop must be terminated by end statement.! disp command simply displays its argument without showing the variable name. 0/0/04 ME 04
While Loop WHILE Statement while <boolean expression> MATLAB statements end Example x = ; while +x > x = x/; end disp(*x) Output.04e-06! while loop repeats the statements as long as the given boolean expression is true.! Another important command is break which terminates the current for or while loop.! The sample program is used to test the floating point accuracy ( machine epsilon ) of a particular computer system. 0/0/04 ME 04
MATLAB Functions! While programming, it is often times necessary to define one s own subroutines (procedures/functions).! Unlike high-level languages, these functions cannot be included in the main program.! Each function has to be an individual m file.! This m file that begins with a line of the following form: function [out,out,...] = cmd_name(in,in,...)! When a function is invoked, MATLAB creates a temporary workspace. The statements inside the function have no access to the variables used in the main workspace unless they are passed as inputs.! When execution ends, all local variables are erased. 0/0/04 ME 04 4
MATLAB Functions (Cont d) function [y,z] = fcn(x) z = + x.*x; y = x./z; plot(x,y) M-file named fcn.m.» x = linspace(-5,5,00);» fcn(x) Calls M-file named fcn.m and passes the vector x.» y = fcn(x);» [y,z] = fcn(x); Different output arguments were passed from the MATLAB function fcn. 0/0/04 ME 04 5
Useful MATLAB Functions Poles of a System D(s) = s 5 + s 4 + 7s + 5s» den = [ 7 5 ];» roots(den) -.0777 +.90i -.0777 -.90i -0.774-0.05 + 0.544i -0.05-0.544i + s + Characteristic Polynomial Let the poles of a system be p = -; p = -; p = -; p,4 = -5 ± 5i;» poles = [- - - 5+5*i 5-5*i];» poly(poles) 6 46 60 00 The resulting characteristic polynomial is D(s) = s 5 + 6s 4 + s + 46s + 60s + 00 0/0/04 ME 04 6
Useful Commands (Cont d) Partial Fractions Expansion Consider the following TF: G(s) = s s + 5s + 6s + s + 6 + s + 6» num = [ 5 6];» den = [ 6 6];» [r,p,k] = residue(num,den) r = -6.0000-4.0000.0000 Residues The resulting fractions are G(s) = N i= ri s p + k 6 4 G (s) = + + + s + s + s + i p = -.0000 -.0000 -.0000 k = Poles Remainder 0/0/04 ME 04 7
Useful Commands (Cont d) Polynomial Multiplication P(s) = s + s + s + Q(s) = s + s + 5 D(s) = P(s)Q(s) =? Zero/Pole Locations» num = [ 5 6];» den = [ 6 6];» pzmap(num,den)» P = [ ];» Q = [ 5];» D = conv(p,q) D = 0 4 6 5 856 8 956!"#$%&$'"()*+ The resulting polynomial becomes *(-./0 9 %956 D(s) = s 5 + s 4 + 0s + 4s + 6s + 5 %8 %856 %456 %4 %756 %7 %856 %8 %956 9,$*#(-./0 0/0/04 ME 04 8
Block Diagram Reduction Transfer Functions in Series X(s) G (s) Y(s) X(s) = G(s) = G As an illustration, let G (s) (s)g (s) Y(s)» num = [ 5]; den = [ 5];» num = []; den = [ ];» [num,den] = series(num,den,num,den) num = 0 0 5 den = 4 8 5 The resulting TF becomes s + 5 G(s) = ; G(s) = s + s + 5 s + G(s) = s + 4s s + 5 + 8s + 5 NUMerator DENominator 0/0/04 ME 04 9
BD Reduction (Cont d) Transfer Functions in Parallel X(s) Y(s) X(s) G (s) G (s) = G(s) = G (s) + G As an illustration, let + + (s) Y(s)» num = [ 5]; den = [ 5];» num = []; den = [ ];» [num,den]=parallel(num,den,num,den) num = 0 9 0 den = 4 8 5 The resulting TF becomes s + 5 G(s) = ; G(s) = s + s + 5 s + G(s) = s s + 4s + 9s + 0 + 8s + 5 0/0/04 ME 04 0
BD Reduction (Cont d) Closed Loop Transfer Functions X(s) + _ Y(s) X(s) G (s) G (s) G (s) = G(s) = + G (s)g As an illustration, let (s) Y(s)» num = [ 5]; den = [ 5];» num = []; den = [ ];» [num,den]=feedback(num,den,num,den) num = 0 6 5 den = 4 9 0 The resulting TF becomes s + 5 G(s) = ; G(s) = s + s + 5 s + G(s) = s s + 4s + 6s + 5 + 9s + 0 0/0/04 ME 04
Unit Impulse Response As an illustration, let G(s) = s s + 4 + s + 4 MATLAB commands are» num = [ 4]; den = [ 4];» impulse(num,den,0) Duration of simulation (0 sec.) 0/0/04 ME 04
Unit Step Response As an illustration, let G(s) = s s + 4 + s + 4 MATLAB commands are» num = [ 4]; den = [ 4];» step(num,den,0) Duration of simulation (0 sec.) 0/0/04 ME 04