A Brief Introduction to Matlab

A Brief Introduction to Matlab

Math 446/OR 481

To start Matlab, at the osf1 prompt type matlab. The matlab prompt looks like this:

 >> 

Matrices and vectors (which is a matrix with a special form) are the underpinnings of everything in Matlab. To enter a matrix, type

A=[2 1 -3;5 7 9;0 8 -4]

Notice the rows are separated by semicolons, the entries in a row are separated by a space. You can also use a comma to separate entries in a row.

By setting A equal to this matrix you can use the letter A to represent this matrix. To recall what is in A, merely type A at the prompt and press enter. You can always change the definition of A by assigning it to something else.

To perform matrix operations on matrices you use the symbols +, -, *, and ^. However, if you want to do scalar operations on each entry in a matrix, you need to use a dot "." in front of these operations. (Functions like sin, cos, log, exp, and tan evaluate the corresponding function of each matrix element.)

To plot the function x squared from 0 to 1 with spacing 0.05, first define a vector x which is evenly spaced from 0 to 1:

x=[0:.05:1]
Then square each entry in the vector x using the ".*" or ".^" command:
y=x.*x
or alternatively
y=x.^2
and plot:
plot(x,y)
Here are some basic matlab commands that you will need. For more details, consult the appendix of the book. (Note: Matlab is CASE sensitive):