Introduction To MATLAB® Concepts: Georg Fries
Introduction To MATLAB® Concepts: Georg Fries
Georg Fries
2
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!
Command
Window
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,
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,
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
for loop
while loop
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 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
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
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
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