msum ch1 concepts.pptx

33
 Introduction to MATLAB® concepts Georg Fries

Upload: mario-h-perez

Post on 04-Nov-2015

22 views

Category:

Documents


0 download

TRANSCRIPT

  • Introduction to MATLAB concepts Georg Fries

  • Georg Fries Modelling and Simulation using MATLAB

    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

  • Georg Fries Modelling and Simulation using MATLAB

    6

  • Georg Fries Modelling and Simulation using MATLAB

    7

  • Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB

    8

  • 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% 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,%%int32,%int64%

    Integers, Use,for,signed,whole,numbers,(integers), 4,sizes:,8,,16,32,and,64,bits,

    uint8,%uint16,%uint32,%uint64%

    Unsigned,Integers, Use,for,unsigned,whole,numbers,(integers), 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,

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

  • Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB

  • Georg Fries Modelling and Simulation using MATLAB

    12

    Array and Matrix Arithmetic Operators 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 conj. transpose)

    matrix,transpose, A',,,,,,,,,,,,,:,,is,the,linear,algebraic,transpose,of,A,,

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

  • Georg Fries Modelling and Simulation using MATLAB

    13

    Relational Operators 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,,

    %=,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,

  • Georg Fries Modelling and Simulation using MATLAB

    14

    Logical Operators 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,

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

  • Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB

    15

  • Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB

    16

  • Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB

    17

  • Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB

    18

  • 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

    20

  • Georg Fries Modelling and Simulation using MATLAB Georg Fries Modelling and Simulation using MATLAB

  • Georg Fries Modelling and Simulation using MATLAB

    22

    Control Flow 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,

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

  • 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

    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

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

  • 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 switch switch_expression case case_expression1 action_1 case case_expression2 action_2 ... otherwise action_n end

    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!

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

  • 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

    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

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

  • 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

    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

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

  • Georg Fries Modelling and Simulation using MATLAB

    32

    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

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

  • 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