You are on page 1of 33

Introduction to MATLAB concepts

Georg Fries

Georg Fries

Modelling and Simulation using MATLAB

What is MATLAB?
MATLAB is an interactive system for numerical computation, programming,
and visualization.
MATLAB can be used for a wide range of applications, i.e. digital signal
processing, image processing, robotics, control engineering, financial
computing, or computational biology.
The built-in tools and math functions enable you to solve complex numerical
problems in a fraction of the time required when using traditional programming
languages such as Java or C++ .
With its relatively simple programming concept, MATLAB can be easily
extended to create scripts and new functions.
You can import and export your data for analyzing and visualizing.
Please use the latest version to avoid compatibility issues!
Georg Fries

Modelling and Simulation using MATLAB

Command
Window

Georg Fries

Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

10

Fundamental MATLAB Data Types


Name%
double,%single%
%
int8,%int16,%%
int32,%int64%
uint8,%uint16,%
uint32,%uint64%
char%
logical%
func,on%handle%
table%
struct%
cell%%
Georg Fries

Reference:,hUp://www.mathworks.com/help/matlab,

Descrip,on%
Use%
Floa%ng(Point,Numbers, Use,for,frac%onal,numeric,data,
Double,and,single,precision,
Default,numeric,type,in,MATLAB,
Integers,
Use,for,signed,whole,numbers,(integers),
4,sizes:,8,,16,32,and,64,bits,
Unsigned,Integers,
Use,for,unsigned,whole,numbers,(integers),
4,sizes:,8,,16,32,and,64,bits,
Characters,and,Strings, Data,type,for,text,,for,mul%ple,strings,,use,cell,arrays,
Logical,Opera%ons,
Use,in,rela%onal,condi%ons,or,to,test,state,,
Two,values:,true,or,false,
Func%on,Handles,
Pointer,to,a,func%on,
Tables,
Container,for,mixed(type,,column(oriented,data,
Structures,
Fields,store,arrays,of,varying,classes,and,sizes,
Cell,Arrays,
Cells,store,arrays,of,varying,classes,and,sizes,
Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

12

Array and Matrix Arithmetic Operators


Operator%
%%+%%,(plus),
%%B,,,,(minus),
%.*%%%(%mes),
,,*,,,(m%mes),
%./%%%(rdivide),
,,/%%%(mrdivide),
%.\%%%(ldivide),
%%\%%%(mldivide),
%.^%%(power),
,,^,,(mpower),
%.'%%%(transpose),
%'%%%%(complex
conj. transpose)
Georg Fries

Name%
array,and,matrix,addi%on,
array,and,matrix,subtrac%on,
array,element-wise multiplication
matrix,mul%plica%on,
array,right,division,
matrix,right,division,
array,le^,division,
matrix,le^,division,
array,element(wise,power,
matrix,power,
array,transpose,
matrix,transpose,

Reference:,hUp://www.mathworks.com/help/matlab,

Comments%
C,=,A+B,,:,,adds,A,and,B,
C,=,A(B,,,:,,subtracts,B,from,A,
C,=,A.*B,:,,is,the,element(by(element,product,of,A,and,B,
C,=,A*B,,:,,is,the,linear,algebraic,product,of,matrices,A,and,B,
A./B,,,,,,,,:,,is,the,matrix,with,elements,A(i,j)/B(i,j),
x,=,B/A,,,:,,is,the,solu%on,of,the,equa%on,xA,=,B,
A.\B,,,,,,,,:,,is,the,matrix,with,elements,B(i,j)/A(i,j),
x,=,A\B,,,:,,is,the,solu%on,of,the,equa%on,Ax,=,B,
A.^B,,,,,,,,:,,is,the,matrix,with,elements,A(i,j),to,the,B(i,j),power,
A^B,,,,,,,,,:,,is,A,to,the,power,B,,if,B,is,a,scalar,
A.',,,,,,,,,,,,:,,is,the,array,transpose,of,A.,
A',,,,,,,,,,,,,:,,is,the,linear,algebraic,transpose,of,A,,

Modelling and Simulation using MATLAB

13

Relational Operators

Reference:,hUp://www.mathworks.com/help/matlab,

Name%

Descrip,on%

==%%(eq)%

A,==,B,returns,a,logical,array,with,elements,set,to,logical,1,(true),where,arrays,A,and,B,are,equal,
Otherwise,,it,returns,logical,0,(false),,
Compares,both,real,and,imaginary,parts,of,numeric,arrays,

~=%%(ne)%
%

A,~=,B,returns,a,logical,array,with,elements,set,to,logical,1,(true),where,arrays,A,and,B,are,not,equal,
Otherwise,,it,returns,logical,0,(false),,
Compares,both,real,and,imaginary,parts,of,numeric,arrays,

%>%%%(gt)%

A,>,B,returns,a,logical,array,with,elements,set,to,logical,1,(true),where,A,is,greater,than,B,
Otherwise,,it,returns,logical,0,(false),,;,,Compares,only,the,real,part,of,numeric,arrays,,

%<%%%(lt)%

A,<,B,returns,an,array,with,elements,set,to,logical,1,(true),where,A,is,less,than,B,
Otherwise,,it,returns,logical,0,(false),,;,,Compares,only,the,real,part,of,numeric,arrays,

>=%%(ge)%

A,>=,B,returns,a,logical,array,with,elements,set,to,logical,1,(true),where,A,is,greater,than,or,equal,to,B,
Otherwise,,it,returns,logical,0,(false),,;,,Compares,only,the,real,part,of,numeric,arrays,

<=%%(le)%

A,<=,B,returns,a,logical,array,with,elements,set,to,logical,1,(true),where,A,is,less,than,or,equal,to,B,
Otherwise,,it,returns,logical,0,(false),,;,,Compares,only,the,real,part,of,numeric,arrays,

Georg Fries

Modelling and Simulation using MATLAB

14

Logical Operators

Reference:,hUp://www.mathworks.com/help/matlab,

Name% Descrip,on%
&%(and), A,&,B,,performs,a,logical,AND,of,the,input,arrays,A,and,B,and,returns,an,array,containing,

elements,set,to,either,logical,1,(true),or,logical,0,(false),,
An,element,of,the,output,array,is,set,to,1,if,all,input,arrays,contain,a,nonzero,element,at,that,
same,array,loca%on;,otherwise,,that,element,is,set,to,0,

~%(not), ~A,performs,a,logical,NOT,of,input,array,A,,and,returns,an,array,containing,elements,set,to,either,
logical,1,(true),or,logical,0,(false),,
An,element,of,the,output,array,is,set,to,1,if,the,input,array,contains,a,zero,value,element,at,that,
same,array,loca%on;,otherwise,,that,element,is,set,to,0,

|%%(or)%% A,|,B,performs,a,logical,OR,of,the,input,arrays,A,and,B,and,returns,an,array,containing,elements,
set,to,either,logical,1,(true),or,logical,0,(false),,
An,element,of,the,output,array,is,set,to,1,if,any,input,arrays,contain,a,nonzero,element,at,that,
same,array,loca%on;,otherwise,,that,element,is,set,to,0,

xor%

C,=,xor(A,,B),performs,an,exclusive,OR,opera%on,on,the,corresponding,elements,of,arrays,A,and,B,
The,resul%ng,element,C(i,j,...),is,logical,true,(1),if,A(i,j,...),or,B(i,j,...),,but,not,both,,is,nonzero,

Georg Fries

Modelling and Simulation using MATLAB

15

Georg Fries

Modelling and Simulation using MATLAB

16

Georg Fries

Modelling and Simulation using MATLAB

17

Georg Fries

Modelling and Simulation using MATLAB

18

Georg Fries

Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

20

Georg Fries

Modelling and Simulation using MATLAB

Georg Fries

Modelling and Simulation using MATLAB

22

Control Flow
Name%
if%%elseif%%else,

Reference:,hUp://www.mathworks.com/help/matlab,

Descrip,on%
Execute,statements,based,on,a,specied,logical,condi%on,

for%
Execute,statements,for,a,xed,number,of,%mes
switch%%case%%otherwise, Switch,among,several,cases,based,on,expression

,,
,,

while%

Repeat,execu%on,of,statements,while,condi%on,is,true,

try%%catch,

Execute,statements,and,catch,resul%ng,errors

break%
con,nue%

Terminate,execu%on,of,a,for,or,a,while,loop,,
Pass,control,to,the,next,itera%on,of,a,for,or,a,while,loop,

end%
pause%

Terminate,block,of,code,
Halt,execu%on,temporarily,

return%

Return,to,the,invoking,func%on,

Georg Fries

Modelling and Simulation using MATLAB

,,

23

Control Flow
Selection statements, that enable us to make choices as to whether
statements are executed or not during run time
if statement
switch statement
Loop control statements, that allow us to repeatedly execute a block of code
for loop
while loop

Georg Fries

Modelling and Simulation using MATLAB

24

if statement
if condition
action
end
if condition_1
action_1
elseif condition_2
action_2

else
action_n
end

Georg Fries

Reference:,hUp://www.mathworks.com/help/matlab,

The if statement evaluates the condition, and


executes the following action if the condition
is true
elseif and else are optional
they execute actions only when previous
conditions are false!

An if block can include multiple elseif


statements
The if statement requires the end keyword

Modelling and Simulation using MATLAB

25

if statement example
clc
% generate a random integer number between 1 and 1000
a = randi(1000)
% check the size
% include alternate choices, using elseif or else.
if a > 500
disp('The number is large, it is greater than 500')
elseif a > 100
disp('The number is greater than 100 and smaller than 501')
else
disp('The number is small')
end
Georg Fries

Modelling and Simulation using MATLAB

26

if statement example
clc
% generate a random integer number between 1 and 1000
a = randi(1000)
% check the size
% include alternate choices, using elseif or else.
if a > 500
disp('The number is large, it is greater than 500')
elseif a > 100
disp('The number is greater than 100 and smaller than 501')
else
disp('The number is small')
end
Georg Fries

Modelling and Simulation using MATLAB

27

switch statement
switch switch_expression
case case_expression1
action_1
case case_expression2
action_2
...
otherwise
action_n
end

Georg Fries

Reference:,hUp://www.mathworks.com/help/matlab,

A switch block conditionally executes one set of


actions from several choices
Each choice is a case
An evaluated switch_expression is a scalar or string
An evaluated case_expression is a scalar, a string,
or a cell array of scalars or strings
The switch block tests each case until one of the
cases is true
When a case is true, MATLAB executes the
corresponding actions, and then exits the switch
block!

Modelling and Simulation using MATLAB

28

switch statement example


% Check if it is Weekend
% Get the day of the week in the variable dayString
[dayNum, dayString] = weekday(date, 'long', 'en_US');
switch dayString
case 'Saturday'
disp(dayString)
disp('Hi - tomorrow is Sunday!')
case 'Sunday'
disp(dayString)
disp('Hi - it is Sunday, enjoy your weekend')
otherwise
disp(dayString)
disp('Hi - sorry you have to work today!')
end
Georg Fries

Modelling and Simulation using MATLAB

29
Reference:,hUp://www.mathworks.com/help/matlab,

for loop
for index = values
program statements
:
end

A for loop executes one or more statements in a loop


values has one of the following forms:
initval:endval
increments the index variable from initval to endval by 1 and
repeats execution of program statements until index is greater
than endval

initval:step:endval
increments index by the value step on each iteration, or
decrements when step is negative

valArray
creates a column vector index from subsequent columns of
array valArray on each iteration

Georg Fries

Modelling and Simulation using MATLAB

30

for loop example


% plotting an animated cosine
% Using drawnow in a loop causes the display
% to update while the loop executes:
clc
x = 0:pi/100:2*pi;
y = cos(x);
h = plot(x,y,'YDataSource','y');
% use a step of 0.01 on each iteration
for k = 1:0.01:5
y = cos(x.*k);
refreshdata(h,'caller')
drawnow
end
Georg Fries

Modelling and Simulation using MATLAB

31

while loop
while expression
action
end

Reference:,hUp://www.mathworks.com/help/matlab,

A while loop repeatedly executes an action in a


loop as long as an expression remains true
An evaluated expression is true when the result
is nonempty and contains all nonzero elements
(logical or real numeric). Otherwise, the
expression is false
Expressions can include
relational operators (such as < or ==) and
logical operators (such as &&, ||, or ~)

MATLAB evaluates compound expressions from


left to right, adhering to operator precedence rules

Georg Fries

Modelling and Simulation using MATLAB

32

while loop example

adopted,and,modied,from,hUp://www.mathworks.com/help/matlab,

clc
% Find the first integer n for which factorial(n) is a 52-digit number
n = 1;
nFactorial = 1;
while nFactorial < 1e51
n = n + 1;
nFactorial = nFactorial * n;
end
% echo n and nFactorial
disp('The first integer n for which factorial(n) is a 52-digit number is:')
n
nFactorial
Georg Fries

Modelling and Simulation using MATLAB

33

while loop example


clc
% Find the first integer n for which factorial(n) is a 52-digit number
n = 1;
nFactorial = 1;
while nFactorial < 1e51
n = n + 1;
nFactorial = nFactorial * n;
end
% echo n and nFactorial
disp('The first integer n for which factorial(n) is a 52-digit number is:')
n
nFactorial
Georg Fries

Modelling and Simulation using MATLAB

You might also like