0% found this document useful (0 votes)
40 views33 pages

Introduction To MATLAB® Concepts: Georg Fries

Uploaded by

danilosilvestri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views33 pages

Introduction To MATLAB® Concepts: Georg Fries

Uploaded by

danilosilvestri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Introduction to MATLAB concepts

Georg Fries
2

Georg Fries Modelling and Simulation using MATLAB


3

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


4

Command
Window

Georg Fries Modelling and Simulation using MATLAB


Georg Fries Modelling and Simulation using MATLAB
6

Georg Fries Modelling and Simulation using MATLAB


7

Georg Fries Modelling and Simulation using MATLAB


8

Georg Fries Modelling and Simulation using MATLAB


Georg Fries Modelling and Simulation using MATLAB
10

Fundamental MATLAB Data Types Reference:,hUp://www.mathworks.com/help/matlab,

Name% Descrip,on% Use%


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

Georg Fries Modelling and Simulation using MATLAB


Georg Fries Modelling and Simulation using MATLAB
12

Array and Matrix Arithmetic Operators Reference:,hUp://www.mathworks.com/help/matlab,

Operator% Name% Comments%


%%+%%,(plus), array,and,matrix,addi%on, C,=,A+B,,:,,adds,A,and,B,
%%B,,,,(minus), array,and,matrix,subtrac%on, C,=,A(B,,,:,,subtracts,B,from,A,
%.*%%%(%mes), array,element-wise multiplication C,=,A.*B,:,,is,the,element(by(element,product,of,A,and,B,
,,*,,,(m%mes), matrix,mul%plica%on, C,=,A*B,,:,,is,the,linear,algebraic,product,of,matrices,A,and,B,
%./%%%(rdivide), array,right,division, A./B,,,,,,,,:,,is,the,matrix,with,elements,A(i,j)/B(i,j),
,,/%%%(mrdivide), matrix,right,division, x,=,B/A,,,:,,is,the,solu%on,of,the,equa%on,xA,=,B,
%.\%%%(ldivide), array,le^,division, A.\B,,,,,,,,:,,is,the,matrix,with,elements,B(i,j)/A(i,j),
%%\%%%(mldivide), matrix,le^,division, x,=,A\B,,,:,,is,the,solu%on,of,the,equa%on,Ax,=,B,
%.^%%(power), array,element(wise,power, A.^B,,,,,,,,:,,is,the,matrix,with,elements,A(i,j),to,the,B(i,j),power,
,,^,,(mpower), matrix,power, A^B,,,,,,,,,:,,is,A,to,the,power,B,,if,B,is,a,scalar,
%.'%%%(transpose), array,transpose, A.',,,,,,,,,,,,:,,is,the,array,transpose,of,A.,
%'%%%%(complex matrix,transpose, A',,,,,,,,,,,,,:,,is,the,linear,algebraic,transpose,of,A,,
conj. transpose)

Georg Fries 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 Reference:,hUp://www.mathworks.com/help/matlab,

Name% Descrip,on%
if%%elseif%%else, 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% Terminate,execu%on,of,a,for,or,a,while,loop,,
con,nue% Pass,control,to,the,next,itera%on,of,a,for,or,a,while,loop,
end% Terminate,block,of,code,
pause% 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 Reference:,hUp://www.mathworks.com/help/matlab,

if condition
action The if statement evaluates the condition, and
end executes the following action if the condition
is true

if condition_1 elseif and else are optional


action_1 they execute actions only when previous
elseif condition_2 conditions are false!
action_2
An if block can include multiple elseif
else statements
action_n
end The if statement requires the end keyword

Georg Fries 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 Reference:,hUp://www.mathworks.com/help/matlab,

switch switch_expression A switch block conditionally executes one set of


case case_expression1 actions from several choices
action_1 Each choice is a case
case case_expression2 An evaluated switch_expression is a scalar or string
action_2 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
otherwise cases is true
action_n When a case is true, MATLAB executes the
end corresponding actions, and then exits the switch
block!

Georg Fries 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 A for loop executes one or more statements in a loop
program statements values has one of the following forms:
: initval:endval
end 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 Reference:,hUp://www.mathworks.com/help/matlab,

while expression A while loop repeatedly executes an action in a


action loop as long as an expression remains true
end 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