Guide to Matlab Programming


The purpose of this page is to help you write a short Matlab script to integrate the state equations of a linear system. Useful information on Matlab is also available at http://web.mit.edu/vchudnov/www/matlab.html, as well as the references at Barker library.


In order to have Matlab integrate a set of linear state equations, one has to follow these steps,

1. Create the A, B, C, D matrices;

2. Construct the state equations of the linear system with these matrices;

3. Specify the quantities necessary for numerical integration;

4. Use Matlab function to perform the integration and plot the result.


Now let us go through the steps one by one.


1. In the Matlab representation of a matrix, space is used to separate two adjacent elements on the same row, semicolon is used to indicate the end of a row, and brackets are used to form the matrix. For example, if one desires to create a matrix,

    1   2   3
T = 4   5   6
    7   8   9 
One can use the command,

T = [1, 2, 3; 4, 5, 6; 7, 8, 9]


2. The Matlab command of 'ss' is for constructing the state equations of a linear system. For example, command,

system1 = ss(A,B,C,D)

constructs a linear system named 'system1' whose state equations are,

dx/dt = Ax + Bu
y = Cx + Du

3. To integrate a set of state equations, Matlab has to know the initial conditions, the relevant time interval and the time increment (since numerical integration method is utilized), and the input. If the state vector is x = [x1, x2] and the initial condition is x1 = 2 and x2 = 3, one can use the command,

x0 = [2, 3]

to specify the initial conditions. If the integration extends from t = 0s to t = 10s, with the desired time increment dt = 0.1s, the corresponding command shall be,

T = [0: 0.1: 10];
And the command to set the input be, for example, u(t) = 5 during that time period is,

U = 5*ones(length(T), 1);

4. Now it is time to see how easy it is to have Matlab do the integration for you. If you have a linear system named 'system1' and you have specified all the necessary quantities as above, you can just type,

lsim(system1, U, T, x0)

and a pretty plot shall appear on your screen.
You can type all the commands in a text file and name the file *.m (e.g. hw5_3.m) as we did in our previous homework assignments, and run the *.m file in Matlab. Please use plain text editor only, such as notepad or emacs, to generate the text file. Don't use fancy word processors like MS Word or FrameMaker.

Some final details. If you find it is annoying that Matlab displays the result of every command, you can get help at

http://web.mit.edu/vchudnov/www/matlab.html#echo

If you want to add title and labels to your plot, you can get help at

http://web.mit.edu/vchudnov/www/matlab.html#gtitles