Table of Contents

View page as slide show

Introduction to MATLAB

Mithat Konar
2019-04-26

What is it?

Alternatives

Two Modes

Variables

a = 3.14159
b = a + 5
c = sin(a);
foo = 'How''s it?'

Comments

% 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

x = 0.3 + 0.4i;

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

MATLAB scripts

plot_sinc.m
% Plot sin(x)/x for x = -4*pi to 4*pi.
 
x = linspace(-4*pi, 4*pi, 1000);
y = sin(x) ./ x;
 
plot(x, y); % will skip NaN points

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

Console input

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

Logical operators

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

traparea.m
function area = traparea(a, b, h)
  area = 0.5 * (a + b) * h;
end
calc_trap_area.m
% Uses traparea function defined in traparea.m
area1 = traparea(1, 2, 3);
disp(area1);

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?

MATLAB's IDE

1)
Recktenwald, Gerald. “MATLAB Functions – Basic Features.” MATLAB Functions – Basic Features. Accessed April 30, 2016. http://web.cecs.pdx.edu/~gerry/MATLAB/programming/basics.html.
All examples in in the “User functions” section are from Recktenwald.