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:
The phase response can be computed using a smooth phase plot or a discontinous phase plot:
One can obtain help on any function by using the help command, e.g.
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, Help and Training
Here are excellent Matlab tutorials:
The following Matlab tutorial book is a useful reference:
Duane C. Hanselman and Bruce Littlefield, Mastering MATLAB, ISBN 9780136013303, Prentice Hall, 2011.
A full version of Matlab is available for your personal computers via a university site license:
Although the first few computer homework assignments will help step you through Matlab, it is strongly suggested that you take the free short courses that the Department of Statistics and Data Sciences will be offering. The schedule of those courses is available online at
Technical support is provided through free consulting services from the Department of Statistics and Data Sciences. Simple queries can be e-mailed to email@example.com. For more complicated inquiries, please go in person to their offices located in GDC 7.404. You can walk in or schedule an appointment online.
Running Matlab in Unix
On the Unix machines in the ECE Learning Resource Center, you can run Matlab by typing
module load matlab
When Matlab begins running, it will automatically execute the commands in your Matlab initialization file, if you have one. On Unix systems, the initialization file must be ~/matlab/startup.m where ~ means your home directory.