Matlab Help
We will use Matlab to analyze data, estimate models, and design experiments. The description below provides a general idea of how to work with vectors and matrices in Matlab. Detailed links to other useful Matlab pages will be provided soon.
Getting Around in Matlab
Matlab uses a command line interface, which can be intimidating initially if you are used to working primarily with graphical interfaces (e.g., Excel). Matlab's command line is similar to that in Maple, except that commands don't have to end in with a semi-colon.
For example,
A = 5
assigns the value of 5 to the variable "A". If you do put a semi-colon after the command, Matlab won't echo the result.
Matlab has all the standard arithmetic and function operations:
e.g., A +b, log(A), A*b, etc.
One feature of Matlab that is very powerful is the ability to work with matrices and vectors directly. For example, to assign values to a matrix "A":
A = [ 1 2 ; 3 4 ]
Try entering this command in Matlab, and observe the result. You should obtain a matrix with the first row consisting of [ 1 2], and the second row consisting of [3 4]. The semi-colon in the middle of the square brackets tells Matlab where the row ends; the square brackets tell Matlab that this is a matrix. Note that the entries within a given row or column are separated by spaces.
You can access elements in a matrix just as you would in a two dimensional array:
A(1,1)
gives you the 1-1 element of the matrix "A". Suppose you want to change this element to 7. You can use the following command:
A(1,1)=7
If you want to access a whole row, you can use ":" in the following way:
A(1,:)
will give you the first row of the A matrix. The command is interpreted as follows - give me the elements of A with row index 1, over all columns (that is what the colon means). Similarly, to access column 2 of A,
A(:,2)
which takes all entries with column index 2, over all rows. So, if I want to change row 2 of the A matrix to be [ -7 -11],
A(2,:)=[-7 -11]
A column vector is simply a matrix with 1 column. A row vector is a matrix with one row. By default, Matlab treats a vector as a row vector. For example,
jim = [5 25]
is a row vector with elements 5 and 25.
Note that Matlab DOES distinguish between upper and lower case characters.
Comments
You can add comments to Matlab functions by placing a "%" symbol at the beginning of the line.
Plotting
To plot responses, use the "plot" command. For example, suppose I have a vector of time values in the variable "t", and a vector of responses in the vector "y". To plot y vs t, I would enter the command:
plot(t,y)
If I want to specify that the line be blue, I can modify the command:
plot(t,y,'b')
where the 'b' (single quote b single quote) indicates the use of blue. I can put a title on the figure using the title command
title('plot of response')
and I can label the axes using the xlabel and ylabel commands:
xlabel('time')
ylabel('response')
Note that character strings are enclosed in single quotes.
If I have a matrix "y" and I want to plot the first column vs. time, I would go:
plot(t,y(:,1))
where I have made use of the method for accessing a column from a matrix described above. If you enter
plot(t,y)
then Matlab will plot each column of y vs. t, all on the same graph.
Help
You can obtain more information about commands by using the "help" command:
help plot
would give you information about the plot command.
|