32A.2 MATLAB

Back

MATLAB interactive computer environment is very useful in computational activities in Mechatronics.

Computations involving scalars, vectors, and matrices can be carried out and the results can be

1MATLAB and SIMULINK are registered trademarks and products of The MathWorks, Inc. LabVIEW is a product of

National Instruments, Inc.

Vibration Design and Control 32-73

© 2005 by Taylor & Francis Group, LLC

graphically displayed and printed. MATLAB toolboxes are available for performing specific tasks in a

particular area of study such as control systems, fuzzy logic, neural network, data acquisition, image

processing, signal processing, system identification, optimization, model predictive control, robust

control, and statistics. User guides, Web-based help, and on-line help from the parent company, Math-

Works, Inc., and various other sources. What is given here is a brief introduction to get started in

MATLAB for tasks that are particularly related to Control Systems and Mechatronics.

32A.2.1 Computations

Mathematical computations can be done by using the MATLAB command window. Simply type in the

computations against the MATLAB prompt “>>” as illustrated next.

32A.2.2 Arithmetic

An example of a simple computation using MATLAB is given below:

>> x ¼ 2; y ¼ 2 3;

>> z ¼ x^2 2 x p y þ 4

z ¼14

In the first line, we have assigned values 2 and 3 to two variables x and y: In the next line, the value of an

algebraic function of these two variables is indicated. Then, MATLAB provides the answer as 14. Note

that if you place a “;” at the end of the line, the answer will not be printed/displayed.

Table 32A.1 gives the symbols for common arithmetic operations used in MATLAB.

Following example shows the solution of the quadratic equation ax2 þ bx þ c ¼ 0:

>> a ¼ 2; b ¼ 3; c ¼ 4;

>> x ¼ (2 b þ sqrt(b^2 2 4 p a p c))

􀀋

(2 p a)

x ¼

2 0.7500 þ 1.1990i

The answer is complex, where i denotes

ffiffiffiffi

21 p : Note that the function sqrt( ) is used, which provides the

positive root only. Some useful mathematical functions are given in Table 32A.2.

32A.2.3 Arrays

An array may be specified by giving the start value, increment, and the end value limit. An example is

given below.

>> x ¼ (0.9: 2 0.1:0.42)

x ¼

0:9000 0:8000 0:7000 0:6000 0:5000

TABLE 32A.1 MATLAB Arithmetic Operations

Symbol Operation

þ Addition

2 Subtraction

p Multiplication

/ Division

^ Power

32-74 Vibration and Shock Handbook

© 2005 by Taylor & Francis Group, LLC

The entire array may be manipulated. For example, all the elements are multiplied by p as below:

>> x ¼ x p pi

x ¼

2:8274 2:5133 2:1991 1:8850 1:5708

The second and the fifth elements are obtained by

>> x([2 5])

ans ¼

2:5133 1:5708

Next, we form a new array y using x; and then plot the two arrays, as shown in Figure 32A.1:

>> y ¼ sin(x);

>> plot(x,y)

A polynomial may be represented as an array of its coefficients. For example, the quadratic equation

ax2 þ bx þ c ¼ 0 as given before, with a ¼ 2;

b ¼ 3; and c ¼ 4; may be solved using the function

“roots” as below:

>> p ¼ ½2 3 4􀀉;

>> roots(p)

ans ¼

20:7500 þ 1:1990i

20:7500 2 1:1990i

The answer is the same as we obtained before.

32A.2.4 Relational and Logical Operations

Useful relational operations in MATLAB are given

in Table 32A.3. Basic logical operations are given in

Table 32A.4.

TABLE 32A.2 Useful Mathematical Functions in MATLAB

Function Description

abs() Absolute value/magnitude

acos() Arc-cosine (inverse cosine)

acosh() Arc-hyperbolic-cosine

asin() Arc-sine

atan() Arc-tan

cos() Cosine

cosh( ) Hyperbolic cosine

exp() Exponential function

imag() Imaginary part of a complex number

log() Natural logarithm

log10() Log to base 10 (common log)

real() Real part of a complex number

sign() Signum function

sin() Sine

sqrt() Positive square root

tan() Tan function

Note that MATLAB is case sensitive.

1.5

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

2 2.5 3

FIGURE 32A.1 A plot using MATLAB.

Vibration Design and Control 32-75

© 2005 by Taylor & Francis Group, LLC

Consider the following example:

>> x ¼ (0:0.25:1) p pi

x ¼

0 0:7854 1:5708 2:3562 3:1416

>> cos(x) . 0

ans ¼

1 1 1 0 0

>> (cos(x) . 0)&(sin(x) . 0)

ans ¼

0 1 1 0 0

In this example, first an array is computed. Then the cosine of each element is computed. Next it is

checked whether the elements are positive. (A truth value of 1 is sent out if true and a truth value of 0 if

false.) Finally, the “AND” operation is used to check whether both corresponding elements of two arrays

are positive.

32A.2.5 Linear Algebra

MATLAB can perform various computations with vectors and matrices (see Appendix 3A and

Appendix 6A). Some basic illustrations are given here.

A vector or a matrix may be specified by assigning values to its elements. Consider the following

example:

>> b ¼ ½ 1:5 22 􀀉;

>> A ¼ ½2 1; 21 1􀀉;

>> b ¼ b0

b ¼ 1:5000

22:0000

>> x ¼ inv(A) p b

x ¼ 1:1667

20:8333

In this example, first a second-order row vector and 2 £ 2 matrix are defined. The row vector is

transposed to get a column vector. Finally the matrix – vector equation Ax ¼ b is solved according to

x ¼ A21b: The determinant and the eigenvalues of A are determined by

TABLE 32A.3 Some Relational Operations

Operator Description

, Less than

,¼ Less than or equal to

. Greater than

.¼ Greater than or equal to

¼ ¼ Equal to

, ¼ Not equal to

TABLE 32A.4 Basic Logical Operations

Operator Description

& AND

l OR

, NOT

32-76 Vibration and Shock Handbook

© 2005 by Taylor & Francis Group, LLC

>> det(A)

ans ¼

3

>> eig(A)

ans ¼1:5000 þ 0:8660i

1:5000 2 0:8660i

Both eigenvectors and eigenvalues of A computed as

>>[V,P] ¼ eig(A)

V ¼

0:7071 0:7071

20:3536 þ 0:6124i 20:3536 2 0:6124i

P ¼

1:5000 þ 0:8660i 0

0 1:5000 2 0:8660i

Here, the symbol V is used to denote the matrix of eigenvectors. The symbol P is used to denote the

diagonal matrix whose diagonal elements are the eigenvalues.

Useful matrix operations in MATLAB are given in Table 32A.5 and several matrix functions are given

in Table 32A.6.

32A.2.6 M-Files

The MATLAB commands have to be keyed in on the command window, one by one. When several

commands are needed to carry out a task, the required effort can be tedious. Instead, the necessary

commands can be placed in a text file, edited as appropriate (using text editor), which MATLAB can use

to execute the complete task. Such a file is called an M-file. The file name must have the extension “m” in

the form filename.m. A toolbox is a collection of such files, for use in a particular application area (e.g.,

control systems, fuzzy logic). Then, by keying in the M-file name at the MATLAB command prompt, the

file will be executed. The necessary data values for executing the file have to be assigned beforehand.