Introduction to Computation in Matlab

 

Prof. Brian L. Evans, Dept. of ECE, The University of Texas, Austin, Texas USA

 

Matlab’s forte is numeric calculations with matrices and vectors.  A vector can be defined as

vec = [1 2 3 4];

The first element of a vector is at index 1.  Hence, vec(1) would return 1.  A way to generate a vector with all of its 10 elements equal to 0 is

zerovec = zeros(1,10);

Two vectors, a and b, can be used in Matlab to represent the left hand side and right hand side, respectively, of a linear constant-coefficient difference equation:

a(3) y[n-2] + a(2) y[n-1] + a(1) y[n] = b(3) x[n-2] + b(2) x[n-1] + b(1) x[n]

The representation extends to higher-order difference equations.  Assuming zero initial conditions, we can derive the transfer function.   The transfer function can also be represented using the two vectors a (negated feedback coefficients) and b (feedforward coefficients).  For the second-order case, the transfer function becomes

We can factor a polynomial by using the roots command.

 

Here is an example of values for vectors a and b:

a = [ 1   6/8    1/8];

b = [ 1     2      3  ];

For an asymptotically stable transfer function, i.e. one for which the region of convergence includes the unit circle, the frequency response can be obtained from the transfer function by substituting z = exp(j w).  The Matlab command freqz implements this substitution:

[h, w] = freqz(b, a, 1000);

The third argument for freqz indicates how many points to use in uniformly sampling the points on the unit circle.  In this example, freqz returns two arguments:  the vector of frequency response values h at samples of the frequency domain given by w.  One can plot the magnitude response on a linear scale or a decibel scale:

plot(w, abs(h));

plot(w, 20*log10(abs(h)));

The phase response can be computed using a smooth phase plot or a discontinous phase plot:

plot(w, unwrap(angle(h)));

plot(w, angle(h));

One can obtain help on any function by using the help command, e.g.

help freqz

As an example of defining and computing with matrices, the following lines would define a 2 x 3 matrix A, then define a 3 x 2 matrix B, and finally compute the matrix C that is the inverse of the transpose of the product of the two matrices A and B:

A = [1 2 3; 4 5 6];
B = [7 8; 9 10; 11 12];
C = inv((A*B)');

Matlab Tutorials and Availability

Excellent Matlab tutorials are available on the UT Austin Web site:

  1. Matlab 7: https://www.utexas.edu/its/rc/tutorials/matlab/
  2. Matlab 6: http://www.utexas.edu/cc/math/tutorials/matlab6/matlab6.html

The following Matlab tutorial book is available in ENS 507 is available for reference:

Duane C. Hanselman and Bruce Littlefield, Mastering MATLAB 6: A Comprehensive Tutorial and Reference, ISBN 0-13-019468-9, Prentice Hall, 2001.

There are versions of Matlab for public use that are available in the ECE Learning Resource Centers in ENS 317, 334, 340, and 507.  A student version of Matlab for the PC or Mac may be purchased at the bookstore for roughly $100.

Although the first few computer homeworks will help step you through Matlab, it is strongly suggested that you take the short courses that the UT Computation Center will be offering. The schedule for the Computation Center short courses on Matlab is available at:

http://www.utexas.edu/computer/classes/topics/statistics.html

For further information on short courses, contact the Computation Center Help Desk in WCH 6, 471-2727.

Consulting for Matlab is provided by Mathematical Services, math@cc.utexas.edu, 475-9400.

Running Matlab in Unix

On the Unix machines in the Learning Resource Center, Matlab is installed in the directory /usr/local/packages/matlab. The Matlab executable is installed in the /usr/local/bin directory, which should already be on your path. To run Matlab, type

matlab

When Matlab begins running, it will automatically execute the commands in your initialization file, if you have one. On UNIX systems, your initialization file must be ~/matlab/startup.m. Note that at the other campus facilities, Matlab may be installed in the /usr/local/matlab directory.