Getting started on Matlab

Illustration

Running Matlab

To run Matlab on your own computer, follow the instructions of the official website. No worries though if you don't have a Matlab license at hand. You can:

In the latter case, here is the procedure:

  1. Open a terminal window (Terminal on Mac/Linux and PuTTY or equivalent on Windows)
  2. Access the cluster via the command:
ssh your-sunetid@rice.stanford.edu
  1. Enter your SUNet password and choose your favorite 2FA method when prompted
  2. Run Matlab with the commands:
module load matlab
matlab

This is it, you are now ready to use Matlab!

Getting started

Basic operations

Here are the basic operations you can perform.

TypeCommandExampleResult
Addition+3 + 36
Subtraction-3 - 30
Multiplication*3 * 39
Division/3 / 31
Exponentiation^3 ^ 327

You have to be careful about the way you combine multiple operations one after another. Formulas are evaluated from left to right, and exponentiation has priority over multiplication and division, which have priority over addition and subtraction.

Also, use = to assign a value to a variable.

Built-in functions

Trigonometry

Here are some useful functions to tackle problems involving angles:

TypeCommandExampleResult
Sinesinsin(pi/2)1
Cosinecoscos(pi)-1
Tangenttantan(0)0
Arcsineasinasin(1)1.5708
Arccosineacosacos(-1)3.1416
Arctangentatanatan(0)0

Exponentials and square root

The following functions are often used:

TypeCommandExampleResult
Exponentialexpexp(1)2.7183
Natural logarithmloglog(exp(1))1
Logarithm base 10log10log10(100)2
Square rootsqrtsqrt(9)3

Rounding and remainder

The following functions can be useful:

TypeCommandExampleResult
Round to nearest integerroundround(2.6)3
Round towards zerofixfix(2.6)2
Round towards plus infinityceilceil(2.1)3
Round towards minus infinityfloorfloor(2.9)2
Remainder after divisionremrem(7, 3)1

Vectors

Syntax

To define a vector, use square brackets [ ] and use either spaces or commas to separate the elements.

v = [1 2 3]
v = [1 2 3]'
v = [1; 2; 3]

Quick generation

For vectors with a high number of elements, you can use the following commands:

CommandResult
[a:step:b]Vector starting from a to b with a step of step
linspace(a, b, n)Vector of n elements between a and b included
zeros(1, n)Vector of n zeros
ones(1, n)Vector of n ones

Common operations

By noting v and w two vectors and k a constant, here are common operations:

OperationCommand
Additionv + w
Subtractionv - w
Element-wise multiplicationv .* w
Element-wise divisionv ./ w
Element-wise exponentiationv .^ k
Dot productdot(v, w)
Vector lengthlength(v)

Matrices

Syntax

To define a matrix, use square brackets [ ] and use semicolons ; to separate the rows.

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

Quick generation

The following commands are often used:

CommandResult
zeros(m, n)Matrix of size $m\times n$ with zeros
ones(m, n)Matrix of size $m\times n$ with ones
eye(n)Identity matrix of size $n\times n$
rand(m, n)Matrix of size $m\times n$ with random elements between 0 and 1

Common operations

By noting A and B two matrices and k a constant, here are common operations:

OperationCommand
AdditionA + B
SubtractionA - B
MultiplicationA * B
Element-wise multiplicationA .* B
Element-wise divisionA ./ B
Element-wise exponentiationA .^ k
Matrix transposeA'
Matrix inverseinv(A)
Matrix determinantdet(A)
Matrix sizesize(A)

Writing your functions

To write your own function, you have to create a new .m file with the name of your function.

Simple case

If your function has one output y and one input x, the file my_function.m should look like this:

function y = my_function(x)
    y = x^2;
end

Advanced case

If your function has multiple outputs [y1, y2] and multiple inputs (x1, x2), the file my_function.m should look like this:

function [y1, y2] = my_function(x1, x2)
    y1 = x1 + x2;
    y2 = x1 * x2;
end

Mathematical operators

Logical operators

The following operators can be used to perform logical operations:

OperationCommand
AND&
OR|
NOT~

Relational operators

The following operators can be used to compare two values:

OperationCommand
Equal to==
Not equal to~=
Greater than>
Greater than or equal to>=
Less than<
Less than or equal to<=

Matlab operators

Loops

The following operators can be used to perform loops:

for i = 1:10
    disp(i)
end
i = 1;
while i <= 10
    disp(i)
    i = i + 1;
end

Statements

The following operators can be used to perform statements:

if x > 0
    disp('Positive')
elseif x < 0
    disp('Negative')
else
    disp('Zero')
end

Plots

Single plot

To plot a single vector y against x, use:

plot(x, y)
xlabel('x')
ylabel('y')
title('My plot')

Multiple plots

To plot multiple vectors in the same figure, use hold on:

plot(x, y1)
hold on
plot(x, y2)
legend('y1', 'y2')

Saving plots

To save a plot, use saveas:

saveas(gcf, 'my_plot.png')

Good practices

Extra tips