Introduction to MATLAB
What is it?
A high-level, interpreted language and environment targeting scientific computing.
A freaking huge, feature filled graphing calculator
Comprehensive
Expensive
Alternatives
Two Modes
Interactive
Scripted
Same syntax in both
Variables
a = 3.14159
b = a + 5
c = sin(a);
foo = 'How''s it?'
% a single line comment
a = 99; % this works too
%{
foo = 'multiple line comments';
baz = 'use curly brackets inside';
qux = 'a pair of percent signs.';
%}
Row vectors
a = [5 4 2 1]
b = 1 : 10 % [1, 2, ... , 10]
t = 0 : 0.1 : 1 % 0 to 1 in 0.1 steps
x = 0 : 0.1 : 2*pi % 0 to 2π in 0.1 steps
Vector functions
% x is a row vector
x = 0 : 0.1 : 2*pi
% compute sin(x) from 0 to 2π
y = sin(x)
% how big is a variable?
size(y)
% many, many more ...
2D Plotting
% plot row vector (index is abscissa)
plot(y)
% plot y vs. x (lengths must match)
plot(x, y)
Complex numbers
Higher dimensions
% separate matrix rows with ';'
a = [1 2 3; 4 5 6; 7 8 9];
b = [9 8 7; 6 5 4; 3 2 1];
Matrix operations
r = 1.25 * a; % scalar multiplication
r = a * b; % matrix multiplication
r = a .* b; % elementwise multiplication
r = a ./ b; % elementwise division
Matrix inverse and transpose
r = inv(a); % inverse
r = a'; % transpose
Indexing
% individual element (row, col)
a(3, 2);
% submatrix (row range, col range)
bigMatrix(1:3, 10:end);
% set values of submatrix, all cols
bigMatrix(1:3, :) = -1;
3D plots
t = 0 : 0.1 : 1;
data = sin(2*pi*t);
z = data' * data;
% create a surface plot
surf(z);
What variables?
% display info about variables in current workspace:
whos
Clearing
% clear the screen:
clc
% clear (delete) variables:
clear varname1 varname2
% clear all variables:
clear
Help!
help what-you-want-help-with
Some useful functions
sqrt(x)
: square root
mod(a, b)
: modulus after division
ones(m, n)
, zeros(m, n)
: matrix of all ones or zeros
rand(m, n)
: matrix of random numbers
linspace(x1, x2, n)
: n linear points between x1 and x2
-
MATLAB scripts
Console output
a = 'hello';
disp(a);
disp([a ' world']); % [] is one way to concatenate strings
c = 42;
disp(c);
disp(['The answer: ' num2str(c)]); % conversion required
prompt = 'What is the frequency? ';
x = input(prompt); % evaluates input
ans = input(prompt, 's'); % doesn't evaluate,
% can get string.
Control structures
if, if/else
if x > y
disp('Ponies!');
elseif x == y
disp('Skittles!');
disp('... don''t eat them all at once.');
else
disp('Rainbows!');
end
if x > y, disp('Ponies!'); else disp('Rainbows!'); end
switch
switch switch_expr
case case_expr,
statement, ..., statement
case {case_expr1, case_expr2, case_expr3,...}
statement, ..., statement
...
otherwise,
statement, ..., statement
end
while
while n < 10
y = y * n;
n = n + 1;
end
On one line:
while n < 10, y = y * n; n = n + 1; end
for
for n = 0 : 0.5 : 2
disp(n);
end
On one line:
for n = 0 : 0.5 : 2, disp(n); end
Nesting control structures
Relational operators
a < b
, lt(a, b)
a > b
, gt(a, b)
a <= b
, le(a, b)
a >= b
, ge(a, b)
a == b
, eq(a, b)
a ~= b
, ne(a, b)
Logical operators
a && b
: short-circuit logical AND (scalars only)
a || b
: short-circuit logical OR (scalars only)
a & b
, and(a, b)
: element-wise logical AND (scalars or vectors)
a | b
, or(a, b)
: element-wise logical OR (scalars or vectors)
~a
, not(a)
: logical NOT
xor(a, b)
: logical EXCLUSIVE OR
More logical operators
User functions
With "main" function
- main.m
function main()
area1 = traparea(1, 2, 3);
disp(area1);
end
function area = traparea(a, b, h)
area = 0.5 * (a + b) * h;
end
Stand alone function
Documenting functions
- traparea.m
function area = traparea(a, b, h)
% traparea(a, b, h) Computes the area of a trapezoid given
% the dimensions a, b and h, where a and b
% are the lengths of the parallel sides and
% h is the distance between these sides.
area = 0.5 * (a + b) * h;
end
Which path?
userpath
: list the directories where MATLAB looks for m-files.
addpath
: add additional directories where MATLAB looks for m-files.
savepath
: make changes permanent.
pathtool
: launch a
GUI to manage paths.
MATLAB's IDE