Colby's Octave Assignment

From Class Wiki
Jump to navigation Jump to search

Entering Matrices

Matrices can be entered into Octave quite easily. Typing v=[1 2 3] sets the variable v to be a 1x3 matrix.

To create a 2x2 matrix you can either type v=[1 2; 3 4] or you could type

v=[1 2
   3 4]

To create a 3x1 column vector you can type v=[1 2 3]'. The prime ' denotes a Hermitian transpose. Alternatively you could type

v=[1
   2
   3]

Typing the function eye(n) will create an n x n identity matrix. Typing the function zeros(n) will create an n x n matrix of zeroes. Typing the function zeros(m,n) will create an m x n matrix of zeroes. The function ones(m,n) will create an m x n matrix of ones. If m=1 it is a column vector, if n=1 it is a row vector.

To create a diagonal matrix use the diag function. Typing diag([1 2 3]) will create a 3x3 diagonal matrix with 1, 2 and 3 on the diagonal.

Matrix Operations

The inv function inverts matrices. Typing A = inv(B) makes A the inverse of B. In this case A*B and B*A are both the identity matrix.

Say we have entered the matrices v=[1 2 3] and w=[1 2 ; 3 4] and z=[5 6 7]'. We can find the dot product of v and z by typing v*z. We can do matrix-vector multiplication by typing w*[1 1]' or [1 1]*w. These will produce different results.

To find eigenvalues use the eig function. Given a square matrix stored in variable X the command

e = eig(x);

will put the eigenvalues of X into the column vector e. If they are real, they are sorted in ascending order.

To find eigenvectors the format of the output will be changed to

[v,e] = eig(X);

This will put the eigenvectors into the columns of v and the eigenvalues into the diagonal of e. In this case e will be a diagonal square matrix with the eigenvalues on the diagonal.