Вы находитесь на странице: 1из 10


Km. 54, National Highway, Makiling, Calamba City, Laguna




MATLAB Basic Functions and Commands for Control Design Applications

Name: Course & Year: BSECE/ 5th Yr

Ronel V. Vidal, PECE Instructor


1. To familiarize MATLAB environment, its capabilities and basic commands 2. To learn how to do Matrix/Vector manipulation and operations 3. To explain the operations commonly used in Feedback and Control Systems and their procedure using MATLAB


MATLAB is a high level technical computing environment for high-performance numeric computation and analysis. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Applications such as modeling, data analysis, simulation, exploration, and visualization can be done with this tool. For these reasons, MATLAB has been commonly employed in Feedback Control Systems design. The name MATLAB stands for matrix laboratory. It uses the functionality and versatility of matrices in order to perform complex computations.

MATLAB Familiarization To start MATLAB, the MATLAB desktop appears, containing tools (graphical user interfaces) for managing files, variables, and applications associated with MATLAB. The first time MATLAB starts, the desktop appears as shown in Fig. 1, although the Launch Pad may contain different entries. The following important entities in the MATLAB user interface are explained in detail.
Command Window: This is where to input commands like entering values in variables, or running scripts (m-files). M-files are scripts that simply execute a series of MATLAB statements, or they can be functions that also accept arguments and produce output. The prompt >> is an indicator where to input values, basic expressions and scripts. Command History: Lines entered in the Command Window are logged in the Command History window. In the Command History, previously used functions can be viewed, and copied and selected lines can be executed. The green text %-- 11:18 AM 4/04/04 --% indicates the Timestamp in which the command was executed, while the succeeding texts indicate the commands performed on that particular time. Launch Pad: MATLAB's Launch Pad provides easy access to tools, demos, and documentation. Simulink and Control System Toolbox are the most common tools to be used for this course. Workspace: The MATLAB workspace consists of the set of variables (named arrays) built up during a MATLAB session and stored in memory. You add variables to the workspace by using functions, running M-files, and loading saved workspaces. To view the workspace and information about each variable, use the Workspace browser, or use the functions who and whos. Fig. 2 shows the Workspace browser.

MATLAB Fundamentals: Format, Variables, Expressions and Commands Once you are familiar with the MATLAB environment, it is time to enter basic expressions. In the command window, all commands are straightforward; just type the expressions such as entering values in a variable or running an m-file. Expressions and Variables Expressions typed without a variable name are evaluated by MATLAB and the result is stored and displayed by a variable called ans. The result of an expression can be assigned to a variable name. Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB uses only the first 31 characters of a variable name and MATLAB is case sensitive. MATLAB is so straightforward that you may enter also mathematical expressions and formulas as you enter the variables. Notice that MATLAB display the result with 5 significant digits (default format: format short). The commands format short e, format long, and format long e display 5 digits floating point, 15 digits fixed point, and 15 digits floating point, respectively. The first example shows a no typed variable expressions, answer is automatically stored in ans. The first expression involves direct value in a variable, while the second one shows a direct mathematical expression using the function cos. Further explanations on Math functions are explained in Elementary Mathematics Functions. >> 13 ans = 13 >> cos(3.1416/3) ans = 0.49999787927255 The second example shows an expression with values stored in variables a and b. >> a= 234.56778 a= 2.3457e+002 >> b=3.1416*(cos(3.1416/6))+2 b= 4.7207e+000
In displaying answers and expressions, the % indicates that any typed expression is converted into a comment while typing a semicolon, ;, after a mathematical expression, omits the MATLAB response or does not display the result. An example is shown in basic display manipulation. The first expression does not display the value of a1 but it is still in the workspace while the second expression is converted into a comment. >> a1=3.5445/64; >> % a1=3.5445/64 Using the command fprintf you can directly manipulate the format of the output of an expression or function. This command displays the result with a desired format on the screen or to specified filename. The %8.4f, indicates that the output value is a float number that has 4 decimal values and has 8 characters in length, if length of characters is less than 8 (7 in the example) the 8th would be a space. All expressions inside the single quote sign are the ones to be displayed. The \n indicates that the next output to be displayed (if there s any) would be on the next line. The expression to be evaluated is typed after the comma , sign. >> fprintf('Area of a circle is %8.4f Square meters\n', 3.1416*3^2) Area of a circle is 28.2744 Square meters

MATLAB has several predefined variables and are listed below. Try the following variables in the Command Window to check their functionality. Special Variables and Commands ans : most recent answer eps : floating point relative accuracy i, j : imaginary unit inf : infinity NaN : not a number pi : There are several commands that perform specific functions. These commands are listed below. It is recommended to try them to see how it works. Special Commands clc : clears the screen clear variable : clears the content and the variable itself exit : exits MATLAB help command : asks help on a particular command who : displays variables in workspace who variable : displays the number of elements in a variable Character String A sequence of characters in single quotes is called a character string or text variable. Characters can be augmented or combined by using a vector [first character,second character] An example is shown below. >> c='Manila' c= Manila >> cs=[c,',Philippines'] cs = Manila,Philippines

Matrix Fundamentals MATLAB treats the single value element, like in the first and second example earlier on Expressions and Variables as a single element matrix. From the word MATLAB which means Matrix Laboratory, it explains why values are treated as matrices. Matrices are entered into MATLAB by listing the elements of the matrix and enclosing them within a pair of brackets, [ ]. Elements of a single row are separated by commas or blanks, and rows are separated by semicolons or carriage return. A single row matrix is entered in MATLAB in two ways, either using spaces or using commas.
>> A= [4 3 2 31 5] A= 4 3 2 31 5 >> A= [4,3,2,31,5] A= 4 3 2 31 5

A single column matrix is entered in MATLAB by using semicolons or carriage returns (in this example, using semicolons). >> B = [3; 4; 5] B= 3 4 5 Combining the single column and single row matrix instructions can create an m x n matrix. To create a matrix, it is entered in MATLAB by using spaces or commas with semicolons or carriage returns, as shown below. >> C = [1 2;3 4] C= 12 34 >> The entire row or column can be addressed using colon, ;. For example, to get the first row of matrix C, follow the example below. The number 1 denotes first and its location tells whether it is a column or row. If the number is placed on the first position, then it denotes that the output is an entire row and if it is placed in the second position, it denotes an entire column. In the example, the number 1 is placed on the first position; therefore its output is the first row. Try to interchange the colons and numbers to see the corresponding change on the output. >> frow = C(1,:) frow = 12 Matrix / Vector Basic Operations Arithmetic operations on arrays are done element-by-element. This means that addition and subtraction are the same for arrays and matrices, but that multiplicative and division operations are different. Basic operations such as addition and subtraction on matrices with the same dimensions can be done. If they are conformable, two matrices can be multiplied and divided. For element-by-element multiplication and division or array operations, MATLAB uses a dot, or decimal point, as part of the notation for multiplication and division. Element by element operations are listed in Table 3. Given two matrices C and D, multiplication can be done by typing >>C*D, element by element multiplication can be one by typing >>C.*D, >>C\D is equivalent to C-1D, and >>C/D is equivalent to CD-1. The inverse of a matrix, denoted by C-1 can be done by using the command >>inv(C). An example is shown below. >> C = [1 2; 3 4] C= 12 34 >> D = [5 6; 7 8] D= 56 78 >> C*D

ans = 19 43 >> C\D ans = -3.0000 4.0000 >> inv(C)*D ans = -3.0000 4.0000 >> C.*D ans = 5 21

22 50

-4.0000 5.0000

-4.0000 5.0000

12 32

Table 1 Element by Element Math Operations Addition +

.* ./ .\ .^ .

Subtraction Element-by-element multiplication Element-by-element division Element-by-element left division Element-by-element power Unconjugated array transpose

An n vector is a row vector or a column array of n numbers. In MATLAB, elements enclosed by brackets and separated by semicolon generate a column vector. The transpose of a row vector is a column vector, and vice versa. This can be done in MATLAB using the symbol (apostrophe). An example is shown below, the transpose of matrix D is E. >> E=D' E= 57 68 Vectors can be generated by just specifying the first, last and the increment desired for each element. For example, to create a row vector with a first element of 1 and a last element of 9 and an increment of 1 , use the syntax in the example below. The default increment is 1 so even without the second parameter 1 , >>F = (1:9), still the result is the same. Try to experiment and change the increment, and see what happens. >> F = (1:1:9) F= 123456789 >> F = (1:9) F= 123456789

Special matrices can be generated given in the table below. Table 2 Elementary Matrices eye Identity Matrix meshgrid X and Y arrays for 3-D plots ones Ones matrix zeros Zeros matrix rand Uniformly distributed random numbers randn Normally distributed random numbers

Elementary Mathematics Functions and Operators in MATLAB Elementary Math functions and operators in MATLAB are very easy to use since they are basic and straightforward in nature. Basic operations like addition, subtraction, multiplication, division can be represented by + , - , * , / respectively. In order to raise a number to a certain exponent, just insert ^ after the number and before the exponent. For example, to evaluate 26, just type >>2^6. For matrices or arrays, operations are different (for exponents, multiplication and division) as stated earlier. Some MATLAB basic math functions automatically operate element by element on an array. Functions that operate element by element are given in the table below. Elementary Math Functions abs Absolute value angle Phase angle acos Inverse cosine asin Inverse sine acosh Inverse hyperbolic cosine asinh Inverse hyperbolic sine atan Inverse tangent atanh Inverse hyperbolic tangent conj complex conjugate cos Cosine exp Exponential cosh Hyperbolic cosine floor Round towards infinity fix Round towards zero imag Complex Imaginary part log Natural logarithm log10 Common logarithm real Complex real part rem remainder after division round Round towards nearest integer sign Signum function sinh Hyperbolic sine sqrt Square root tan Tangent tanh Hyperbolic tangent Plots
MATLAB can create high-resolution, publication-quality 2-D, 3-D, linear, log, semilog, polar, bar chart and contour plots on plotters, dot-matrix printers, and laser printers. Some of the 2-D graph types are plot, loglog, semilogx, semilogy, polar, and bar. The command grid adds a grid to the graph, and the command title(text ), xlabel(text ), ylabel(text ), and text(text ) can be used for labeling and placing text on the graph. MATLAB provides automatic scaling. The function axis([xmin, xmax, ymin, ymax])enforces manual scaling. As an example, to plot a sinusoidal function, let y = 2sinx, where x is the abscissa (an angle) and y is the ordinate. Take note that angles are in radians. The listing is shown below. >> x = (0:1/1000:2*pi); >> y = 2*sin(x); >> plot(x,y), title('Sinusoidal waveform'),

Plot of y = 2sin(x)

MATLAB for Control Systems Analysis and Design The functions to be presented are basic and are commonly used in Control Systems applications. These functions are not used directly for solving Control System problems but rather application of these commands will greatly help in solving them. Complex numbers In MATLAB, imaginary numbers are represented using i or j. Mathematical operations are straightforward, similar to operations in real numbers. Given: (25+j65) and (30+j80) (addition and division) >> %Addition >> (25+65j) + (30+80j) ans = 5.5000e+001 +1.4500e+002i >> %Division >> (25+65j)/(30+80j) ans = 0.8151 - 0.0068i Some parameters of the complex numbers can be also extracted like the magnitude and phase angle, real part and imaginary part. , we need to get the magnitude and phase angle of the complex number (25+j65). We have to convert the angle in degrees so we multiplied 180/ to the answer of angle (always in radians).

>> abs(25+65j) ans = 69.6419 >> angle(25+65j)*180/pi ans = 68.9625 >> real(25+65j) ans = 25 >> imag(25+65j) ans = 65

Other basic functions for control engineering applications laplace Generates the Laplace transform of a polynomial in the time domain. ilaplace Generates the inverse-Laplace transform of a polynomial in the s domain.

>> %Laplace Transform >> syms t >> x = 2 + 3*exp(-2t) - 2*t*exp(-2t) - t^2 >> laplace(x) ans = 2/s+3/(s+2)-2/(s+2)^2-2/s^3 >>

Given f(t)= 4 cos (4t) , find its Laplace transform >> %Laplace Transform of Cosine >> syms t >> f = 4*cos(4*t); >> laplace(f) ans = 4*s/(s^2+16)

>> % Inverse Laplace of 2/s+3/(s+2)-2/(s+2)^2-2/s^3 >> syms s >> f = 2/s+3/(s+2)-2/(s+2)^2-2/s^3; >> ilaplace(f) ans = 2+3*exp(-2*t)-2*t*exp(-2*t)-t^2

>> % Inverse Laplace of 4*s/(s^2+16) >> syms s >> f = 4*s/(s^2+16); >> ilaplace(f) ans = 4*cos(16^(1/2)*t) The inverse Laplace is f(t)= 4cos(4t)


Laboratory Exercises