You are on page 1of 27

What is Matlab?

One of the most commonly used mathematical programs is


Matlab. We’ll learn the basics over the next few weeks.

Matlab was released in 1984, as a tool to assist with numerical


linear algebra (MATrix LABoratory). It has since expanded into an
all-purpose mathematical toolbox, capable of doing numerical and
symbolic calculations, as well scientific computation, signal
processing, data analysis and visualisation, ...
Where do I get Matlab?

The university provides students with access to Matlab.


Information may be found here:

https://au.mathworks.com/academia/tah-portal/
macquarie-university-916052.html

This page has installation instructions. You will need to sign up


with your university email account in order to access the free
Matlab installation.

Matlab is available on most platforms, although the installation


process will be different. There’s no one-size-fits-all instructions
here, unfortunately.
The Matlab Window
When you open Matlab, you’ll see something like this:
The Matlab Window

There are three main sections of the screen:


• Left: Working directory (scripts, functions, saved output, etc.)
• Centre: Command window
• Right: Workspace (variables stored)
The Matlab Window

There are three main sections of the screen:


• Left: Working directory (scripts, functions, saved output, etc.)
• Centre: Command window
• Right: Workspace (variables stored)

To begin with, you’ll type commands into the command window.


This will often create variables which are stored in the workspace.
You can access these directly by clicking on them.
The Matlab Command Line

You can enter expressions into the command window:


• Adding and subtracting: 1+1, 5-2
• Multiplying and dividing: 3*2, 8/3
• Powers, parenthesis: 2^4, 3^(1/2)
When you press Enter, Matlab will output the answer.

Matlab respects the standard order of operations.


The Matlab Command Line

Matlab understands a few special quantities:

• pi: 3.141592...
• ans: The output of the last calculation
• eps: The smallest number Matlab can distinguish
• inf: Infinity
• NaN: Not a number (eg. 0/0)
• i, 1i, 7i: Imaginary numbers (get in the habit of using 1i
instead of i, to save frustration later)

There are others, but you won’t need them this semester.
Creating variables

Matlab can assign variables using the = sign, and call them later.
Creating variables

You don’t always want the output printed on the screen, especially
if you’re defining variables. If you end a line with a semi-colon (;),
it suppresses the output.
Saving variables

A list of saved variables is kept in the workspace. You can clear


individual variables by typical clear var1. You can clear all
variables by typing clear all.
You can save your workspace to a file by typing
save(‘filename.mat’). A .mat file contains a list of variables
that Matlab can load into the workspace in another session by
typing load(‘filename.mat’).
Floating point

Matlab typically stores numbers using “double-precision floating


point”. This uses 64 bits to describe a number:
• Sign (1 bit)
• Exponent (11 bits)
• Fraction (52 bits)

The number is read in the form

Sign (1.fraction) × 10exponent

This gives 16 digits of precision for each stored value.


In-built functions

Matlab has a lot of in-built functions which we can make use of:
• Trigonometric: sin, cos, tan, asin, acos, atan
• Exponentials and logarithms: exp, log, log10
• Hyperbolic: cosh, sinh, tanh, acosh, asinh, atanh
• Rounding: round, floor, ceil
• Complex numbers: real, imag, abs, angle, conj
• Sign: sgn
• Square root: sqrt
Matrices and vectors

Matlab is called Matrix Laboratory for a reason. It is easy to


input matrices and vectors:

• Row vector: [1 1 2 3 5 8]
• Column vector: [3; 1; 4; 1; 5; 9]
• Matrix: [1 0 0; 0 1 0; 0 0 1]

• Alternatively, rows can use commas: [1, 1, 2, 3, 5, 8]

As far as Matlab is concerned, everything is a matrix. A vector is a


1×N or N×1 matrix, and a scalar is a 1×1 matrix.
Matrices and vectors

There are two kinds of operations on matrices or vectors:


element-wise and full operations.
Element-wise operations treat each element of a matrix
individually. If we set M = [1 0; 0 1]:
• M+1 gives [2 1; 1 2]
• 5*M gives [5 0; 0 5]

Most of the in-built functions will perform element-wise


operations:
• exp(M) gives [2.7183 1; 1 2.7183]
Matrices and vectors

Matrix operations treat a matrix in its entirety.


If we set M = [1 0; 0 1] and N = [2 2; 2 2]:
• M+N gives [3 2; 2 3]
• M*N gives [2 2; 2 2]
• N^2 gives [8 8; 8 8]

Matlab will give you an error if matrix sizes are incompatible.


To perform an operation element-wise, include a period:
• N.^2 gives [4 4; 4 4]
• M.*N gives [2 0; 0 2]
Matrices and vectors

You can pick out elements of a vector.

Note that Matlab uses 1-indexing, rather than 0-indexing.


Matrices and vectors

You can pick out elements of a matrix in the same way:


• M = [1 2 3; 4 5 6; 7 8 9]

• M(3,2) = 8
• M(2,:) = [4 5 6]
• M(1,[2 3]) = [2 3]
• M([1 2],[1 3]) = [1 3; 4 6]
• M(:,[1,2]) = [1 2; 4 5; 7 8]
• M(1,end) = 3

The end and : instructions are useful shortcuts.


Matrices and vectors

We used the colon operator to reference entire rows and columns.


It’s actually useful for more than that:
• 1:7 gives [1 2 3 4 5 6 7]
• 1:0.25:2 gives [1 1.25 1.5 1.75 2]
• 1:4:10 gives [1 5 9]
• 7:-1:1 gives [7 6 5 4 3 2 1]
• 1:-1:7 gives a 1×0 empty vector

Another convenient way to do this is with linspace. It instead


prescribes the beginning, the end, and the number of entries:
• linspace(1,2,5) gives [1 1.25 1.5 1.75 2]
Matrix Operations

There are a number of operations that are useful for matrices


• ones(p,q), zeros(p,q), rand(p,q) create a p×q matrix of
ones, zeroes, or uniform random numbers on [0,1].
• eye(p) generates a p×p identity matrix.
• diag(M) finds the diagonal elements.
• det(M) finds the determinant (square only).
• inv(M) finds the inverse (square only).
• rank(M) finds the rank.
• size(M) finds the dimensions.
• length(M) finds the longest dimension (useful for vectors and
lists of numbers).
Matrix Operations

The transpose operator is sneaky:

• M’ finds the conjugate transpose (or Hermitian transpose). It


takes the transpose, and then the conjugate of any complex
numbers.
This is a useful operation in linear algebra, but gives really
annoying and difficult-to-find errors if you forget about it.
• M.’ gives the regular, non-conjugate transpose.
Matrix Operations

One particularly helpful operation is eig:

The (normalised) eigenvectors are the columns of the first output.


Corresponding eigenvalues are the diagonals of the second output.
Matrix Operations

It’s alright that you don’t know what eigenvalues and eigenvectors
are yet. You don’t have to know for this subject.

You will learn what these mean in MATH1020 or MATH1025, and


now you know how to find them with Matlab.
Solving linear systems
Matlab is particularly good at solving linear systems, written in
the form A x = b.
You might try calculating x = inv(A)*b, but this is very
computationally expensive and inefficient, and prone to floating
point accuracy errors.
Instead, use the backslash operator:
Solving linear systems

We can compare the time required to compute the inverse with the
time required to use the backslash operator:

This doesn’t seem like much, but numerical linear algebra often
involves performing these operations millions of times. It adds up.
List Operations

Some operations that are useful for lists of numbers:


• max, min: find the maximum or minimum element.
• sum, prod: find the sum or product of list elements.
• mean, median: find the mean or median of a list.
• std, var: find the standard deviation or variance of a list.

If you perform these operations on an matrix, it will treat each


column as a seperate list and return a row vector.
Finally:
• norm(V,p) finds the p-norm of a vector V . Typically you
would use p = 1, 2 (the Euclidian norm), or infinity.
• Matrix norms also work, but you don’t know those yet.
Matlab Errors
Always read your error messages. Matlab wants to help, and will
try to tell you what’s gone wrong:

Once you start programming next week, it will also tell you on
what line of code the error occurred:
Go and Practice

Have fun with the first computer lab session!


If you get stuck, Matlab has very comprehensive documentation
and examples. Also, if you forget how to use a command, type
help then the command:

You might also like