• Matlab







Connecting to Matlab


Two Options For Windows Users

  1. Use SSH
    Download Secure Shell from the mason website: https://itservices.gmu.edu/downloads
    Once you have it installed, open the program.
    SSH has both an FTP(Yellow Icon) and a Terminal(White Icon). Open the Terminal.
    From the SSH client window, click on the 'Quick Connect' button.
    In the Host Name Box type mason.gmu.edu and your user name.
    Press Connect and you will be prompted to enter your password.
    To begin your Matlab session, at the mason prompt type matlab and press enter.

  2. Use Virtual Computing Lab
    After clicking on New Reservation choose from drop down menu: Matlab 2013a
    Click here for PC instructions

Two Options For Mac Users

  1. Use Terminal App
    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.
    To begin your Matlab session, at the mason prompt type matlab and press enter.

  2. Use Virtual Computing Lab
    After clicking on New Reservation choose from drop down menu: Matlab 2013a
    Click here for Mac instructions


Using Matlab


The matlab prompt looks like this: >>

  • Copy from the SSH or Terminal 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.
  • To define a matrix you start with its name. Square brackets are used to begin and end each matrix. Use commas (or spaces ) separate the elements of each row. End each row with a semi-colon:

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


Here you have defined a 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, :) = 0    changes all elements in row2 of matrix D to 0

>> A(1:3, 3)=[7,8,9]    changes the elements in col 3 to 7,8,9.

>> X=A(:, 3)    extracts column 3 from the matrix A and puts the values in a column 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 filename this command will save all variables (and ONLY the variables - NOT the whole session) 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.

load filename this command loads the variables that you saved in a previous session.

who displays variables you have defined in your current session

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

>> 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

>> rats (A) writes elements of matrix A as fractions.

augmented matrix - - you can create an augmented matrix from two others that you have already defined as follows:

>> S = [ A C] uses the matrix A and C defined previously and

creates the matrix S

>> 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.


eobrien@gmu.edu