LPPD, University of Illinois at Chicago
Director: Prof. Andreas A. Linninger

Objective
Research
People
Publications
Teaching
Reports
Employment
Contact Us
Home

 

CHE 341-PROCESS CONTROL

SPRING 2002

LABORATORY -2 (TUTORIAL 2)

INTRODUCTION TO MATLAB

 

  Developed by Chandra Singavarapu under the guidance of Prof. A. Linninger
 

To get started:

START-> PROGRAMS-> MATLAB

MATLAB stands for MATRIX LABORATORY

Using the Environment:

On the PC, the installer creates a shortcut to the program file in the installation directory. You can move this shortcut to your desktop if you want. Double click on this shortcut icon to start MATLAB.

Quitting Matlab:

To quit Matlab at any time, type quit at the Matlab prompt. On the PC, you may prefer using Exit or Quit from the File Menu.

The Command Window:

The Command Window is the main window in which you communicate with the MATLAB interpreter.

The Matlab interpreter displays a prompt (>>) indicating that it is ready to accept commands from you. For example, to enter a 3-by-3 matrix-

To enter a matrix:

  1. Separate the elements of a row with blanks or commas
  2. Use a semi-colon to indicate end of each row
  3. Use [ ] to enclose all elements of matrix

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

When you press the Enter or Return key, Matlab responds with

A=

 
1 2 3
4 5 6
7 8 10

sum(A)
 
12 15 19

sum (A) returns you the sum of the column elements.
 

Interrupting a Running Program:

You can interrupt a running program by pressing Ctrl-c at any time.

Clearing Sessions:

You can save your work to a file or clear you session by going to the Edit button in the window and Clear the Session. To save the session in a file, click the File button and save your work. The extension to Matlab files is .m and you just need to give a name to your file and the extension will be created automatically.

Suppressing Output:

If you simply type a statement and press return or Enter, Matlab automatically displays the results on screen. However, if you end e line with a semi-colon, Matlab performs the computation but does not display any output. This is particularly useful when you generate large matrices.

Long Command Lines:

If a statement does not fit on one line, use three periods, . . . , followed by Return or Enter to indicate that the statement continues on the next line. For example

S= 1 –1/2 + 1/3 – 1/4 + 1/5 – 1/6 + 1/7…

-1/8 + 1/9 – 1/10 + 1/11;

Blank spaces about the =, + and – signs are optional but they improve readability. The maximum number of characters allowed on a single line is 4096.

Matrices :

The representation of a matrix has been discussed early.

Addition and Subtraction of matrices: Addition and Subtraction of matrices is defined just as it is for element-by-element.

Ex: X= A + B gives us X matrix as a result of addition of A and B matrices.

Y = P – Q gives us Y matrix as a result of subtraction of P and Q matrices.

Addition and subtraction require both matrices to have the same dimension, or one of them to be a scalar ( a scalar matrix is a 1-by-1 matrix) . If the dimensions are incompatible, an error results.

Transpose of a matrix: Matlab uses the apostrophe (or single quote) to denote transpose. Transposition turns a row vector into a column vector.

X= B’

gives us the matrix X which is the transpose of matrix B.

Multiplication: Matlab uses a single asterisk to denote matrix multiplication. Rectangular matrix multiplications must satisfy the dimension compatibility conditions.

Identity matrix: Generally accepted mathematical notation uses the capital letter I to denote identity matrices. In Matlab, the function eye (m, n) returns an m-by-n rectangular identity matrix and eye (n) returns an n-by-n square identity matrix.

Inverses and Determinants: The inverse of a matrix is computed by the function inv and the determinant by the function det.

D= det (A)

X= inv (A)

Matrix powers: If A is a square matrix and ‘p’ is a positive integer, then A^p multiplies A by itself ‘p’ times.

Eigen values: The statement

lambda = eig (A)

produces a column vector containing the eigen values.

Polynomials:

Matlab provides functions for standard polynomial operations, such as polynomial roots, evaluation, and differentiation. In addition there are functions for more advanced operations, such as curve fitting and partial fraction expansion.

The polynomial functions live in a directory called polyfun in the Matlab.

Function  Description
Roots Find polynomial roots
Poly Polynomial with specified roots
Polyval Polynomial evaluation
Polyfit Polynomial curve fitting
Residue Partial-fraction expansion
Polyder Polynomial derivative
Conv Multiply polynomials
Deconv Divide polynomials

 

Representing Polynomials:

Matlab represents polynomials as row vectors containing coefficients ordered by descending powers. For example, consider the equation

P(x) = x3 – 2x –5

To enter this polynomial into Matlab, use

P= [ 1 0 2 –5];

Polynomial roots:

The roots function calculates the roots of a polynomial.

r = roots(P)

r =

2.0946

-1.0473 + 1.1359i

-1.0473 - 1.1359i

By convention, Matlab stores roots in column vectors. The function poly returns to the polynomial coefficients.

p2 = poly(r )

 p2 =

1 8.8818e-16 -2 -5

poly and roots are inverse functions.
 

Polynomial evaluation:

The polyval function evaluates a polynomial at a specified value. To evaluate P at x= 5, use

polyval (p, 5)

ans =

110
 

Convolution and Deconvolution:

Polynomial multiplication and division correspond to the operations convolution and deconvolution. The functions conv and deconv implement these operations.

Consider the polynomials a(s) = s2 + 2s + 3 and b(s) = 4s2 +5s +6. To compute their product,

a= [ 1 2 3 ]; b= [ 4 5 6];

c= conv(a,b)

c =

4 13 28 27 18

Use deconvolution to divide a(s) back out of the product:

[ q, r] = deconv(c,a)

q =

4 5 6

r =

0 0 0 0 0
 

Representing a Transfer Function:

Suppose a Transfer function is given by h(s) = (s + 1) / (s2 + 2s + 3).

Then it is represented in matlab as

h= tf ( [ 1 1], [2 2 3])
 
 

Then the Linear Time Invariant (LTI) viewer can be used to see how the system responds to an input.

To generic syntax is:

ltiview ( plottype, sys, extras)

where sys is the name of an LTI object in the MATLAB workspace and plottype is one of the following strings:

'step'

'impulse'

'initial'

'lsim'

'bode'

'nyquist'

'nichols'

'sigma'

For example, you can initialize an LTI viewer showing the step response of the LTI model sys by:

Ltiview ('step', h) where h= tf ([1 1], [2 2 3])

extras is a compilation of the additional input arguments supported by the function named in plottype.

In the example above, plottype is 'step' and an additional input argument specifying the final time of the response may be entered, as in:

Ltiview ('step', sys, Tfinal)