Modelling Lecture JP 08

download Modelling Lecture JP 08

of 12

Transcript of Modelling Lecture JP 08

  • 8/2/2019 Modelling Lecture JP 08

    1/12

    Lecture 07

  • 8/2/2019 Modelling Lecture JP 08

    2/12

    What we about to learn..

    Content:

    Basic Logical Functions

    true and false Logicals

    Loops: for Loop

    Loops: while Loop

    Loops: if Loop

    Function

  • 8/2/2019 Modelling Lecture JP 08

    3/12

    Loops/Logicals 01 Sometime we would like to perform some repeated functions/routines in

    programme, and we could use loops.

    There are different type of logicals/loops, like

    some basic ones: & and, | or, > greater than, (greater than): it is a function to express comparison of event with the

    first greater than the second. =,

  • 8/2/2019 Modelling Lecture JP 08

    4/12

    Loops/Logicals 02 MATLAB represents the logicals oftrue or false by 1 or 0, respectively.

    Logicals in statement-testing:>> a=1; a>2, a> b=[-1 2.2; 10 0];

    >> b>=1 & b*3~=0

    ans =

    0 1

    1 0

  • 8/2/2019 Modelling Lecture JP 08

    5/12

    Loops/Logicals 03 for loop expresses repeated statements for a specific number of times:

    Example 1 (Single Layer):

    >> y = -1:0.05:1;

    >> for k = 1:8subplot(4,2,k), plot(y,sin(k*pi()*y))

    end

    Example 2 (Multiple Layers):

    >> y = -1:0.05:1;

    >> for i=4

    for j=2

    for k=1:8subplot(i,j,k), plot(y,sin(i*j*k*pi()*y));

    end

    end

    end

  • 8/2/2019 Modelling Lecture JP 08

    6/12

    Loops/Logicals 04 for loop results:

    Example 1 (Single Layer): Example 2 (Multiple Layers):

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-2

    0

    2x 10

    -14

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

    -1 -0.5 0 0.5 1-1

    0

    1

  • 8/2/2019 Modelling Lecture JP 08

    7/12

    Loops/Logicals 05 while loop expresses repeated statements for a infinity number of times:

    Example:

    >> dX=0.01;

    >> X=0:dX:2;>> Y=0*X;

    >> Y(1)=1;

    >> i=1;S=size(X);

    >> SM=max(size(X))

    SM =

    201

    >> while(i> plot(X,Y)

  • 8/2/2019 Modelling Lecture JP 08

    8/12

    Loops/Logicals 06 while loop result:

    0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

    0.7

    0.8

    0.9

    1

    1.1

    1.2

    1.3

    1.4

  • 8/2/2019 Modelling Lecture JP 08

    9/12

    Loops/Logicals 07 if loop expresses conditionally executed statements:

    Example 1:

    >> A=[1:4;1:4;1:4;1:4];

    >> if A(1,2)==1A(1,1)=1;

    elseif A(4,4)==2

    A(3,3)=2;

    else

    A(1,1)=A(1,1)+1;

    end

    >> AA =

    2 2 3 4

    1 2 3 4

    1 2 3 41 2 3 4

  • 8/2/2019 Modelling Lecture JP 08

    10/12

    Loops/Logicals 08 Combined-looping:

    Example 2 (To Continue from 1st Example):

    >> icount0=0;icount1=0;icount2=0;error=0;

    >> for i=1:4for j=1:4

    if A(i,j)==0

    icount0=icount0+1;

    elseif A(i,j)==1

    icount1=icount1+1;

    elseif A(i,j)==2

    icount2=icount2+1;else

    error=error+1;

    end

    endend

  • 8/2/2019 Modelling Lecture JP 08

    11/12

    Loops/Logicals 09 Example 2 (Continue As what results we can get):

    >> icount0

    icount0 =

    0>> icount1

    icount1 =

    3

    >> icount2

    icount2 =

    5

    >> error

    error =

    8

  • 8/2/2019 Modelling Lecture JP 08

    12/12

    Loops/Logicals 10

    Function a streams of simple operations that use for constructing

    a routine/sub-routine:

    In M-File, type:

    function [X] = multiplication(x,y,z)k = x+y+z;

    X = sqrt(k)*x*y*z;

    Then, save under filename and folder you want: e.g.

    multiplication.m

    After that, we could execute:

    H=multiplication(1,-2,3)

    H =

    -8.4853