Introduction to Matlab

**Connecting to Matlab**

Logon to mason using your internet provider and follow directions below:

For Windows Users: Download Secure Shell from the mason website: http://itservices.gmu.edu/downloads/

Once you have it installed, open the program.

From the SSH client window, click on the 'Quick Connect' button.

In the Host Name Box typemason.gmu.eduand your user name.

Press Connect and you will be prompted to enter your password. (NOTE: This is not necessarily the same as your email password)

If you haven't done this before, you must activate your mason account.

See the web page below for instructions to activate your account and set up a password.

https://mason.gmu.edu/ISO/SysEng/Mason/account.html

For Mac Users: In Applications/Utilities open the terminal program.

At the command prompt, type : **ssh username@mason.gmu.edu **

After you press enter you will be prompted to enter your password.

If you haven't done this before, you must activate your mason account.

See the web page below for instructions to activate your account and set up a password.

https://mason.gmu.edu/ISO/SysEng/Mason/account.html

- To begin your Matlab session, at the osf1/mason prompt type
**matlab**and press enter.The matlab prompt looks like this: >>

- Copy from the SSH window by highlighting the text and right click to Copy (or use the edit menu). You can open notepad and paste the text there using the right click menu. This allows you to easily edit your work, erase errors, etc. before printing and turning in your assignment.

**Using Matlab**

- To
**define a matrix**you start with its name. Square brackets are used to begin and end each matrix. Commas separate the elements and you can press return to end each row.>> A = [1,4,-6

7,6,2

-2,1,0]

You can also separate elements with a space and end each row with a semi-colon like this:

>> A = [1 4 -6; 7 6 2; -2 1 0;]

In either case you have defined the same 3 by 3 matrix named A and you can use it at any time in a calculation or to define another matrix by using its name (A).

__Examples:__>> B = [2 -4 7 ] B is a 1 by 3 row matrix>> C = [2; -4; 7; ] C is a 3 by 1 column matrix

>>D = [ 5,8,-2;3,-4,5] D is a 2 by 3 matrix

- You can
**edit**individual elements of a matrix as follows:>> A(1,1) = -5 changes the element in row1 column1 of matrix A to -5

>> D(2,3) = 0 changes the element in row2 column3 of matrix D to 0

>>

**X=A(1:3,3)**this will extract the third column of A (rows 1-3) and put it into a matrix called X.

- To perform
**operations**on matrices you use the symbols (**+, -, ***) from the number keypad or above the numbers across the top of the keyboard.__Examples:__>> A + A adds the matrix A to itself.

>> B * C multiplies B and C

>> C*B - A A is subtracted from the product of C and B

The symbol

**^**(above the number 6) is used to raise a matrix to an exponent as follows:>> A^3 cubes the matrix A ( you might also use A*A*A for the same calculation)

>> C*D an error message is displayed Matlab will give an error message when the calculation cannot be done because of a dimension mismatch.

- Here are some basic matlab commands that you will need:
**quit***or***exit**either of these closes the program and ends your matlab session.**save**this command will save all variables that you have defined in the current session. This is helpful when you enter large matrices that you want to work with at a later date.*filename***load**this command loads the variables that you saved in a previous session.*filename***diary**if you are using a Unix terminal, you must use this command before you can print your work. The file you create will be saved in your home directory.*filename***diary off**the diary file starts at the point where you insert the command*diary*and ends where you type*diary off*. (This allows you to control what gets saved in the diary file.)**lpr**this is the command used to print your diary file. EXIT THE MATLAB PROGRAM.

At the osf1 prompt, type:**lpr -Pst220**(note: st220 is the printer in ST I room 220. At other locations you will use another printer name after the -*filename***P**)**who**displays variables you have defined in your current session**why**matlab answers the question.(again and again)**clear**clears all variables from your current session. Only use this command if you want to lose everything.**%**this is used for comments. Matlab ignores any line that begins with %

- Matlab has many built in matrix functions and operators. I have listed some here that you may find useful:
>>

**size**(A) gives the dimension of the matrix A>>

**inv**(A) calculates the inverse of the matrix A , if it exists.>>

**det**(A) calculates the determinant of the matrix A>>

**rref**(A) calculates the row reduced echelon form of the matrix A>>

**rrefmovie**(A) shows the individual pivot operations of rref(A)>> A

**'**forms the transpose of the matrix A.>>

**eye**(2,2) this is the 2 by 2 identity>>

**zeros**(3,3) builds the zero matrix of any dimension>>

**ones**(3,2) fills a matrix of any size with ones.

already defined as follows:**augmented matrix**you can create an augmented matrix from two others that you have**>> S = [ A C]**uses the matrix A and C defined previously and creates the matrix S>>

**rats**(A) displays elements of the matrix A as fractions. Be aware that this command changes the dimension of your matrix. Best used after all calculations have been completed.>>

**format long**displays all numbers with 15 digits instead of the usual 4 digits>>

**eig**(A) gives the eigenvalues of the matrix A>>

**poly**(A) gives the coefficients of the characteristic polynomial A. The roots of this polynomial are the eigenvalues of A.