matlab programming (flow control)

Upload: trust-emma

Post on 03-Jun-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 MATLAB PROGRAMMING (FLOW CONTROL)

    1/3

    ML102

    MATLAB PROGRAMMING

    Flow Control

    MATLAB has several flow control constructs but the basic flow control or

    command word for solving mathematical and engineering problems are:

    if

    switch and case

    for

    while

    continue

    break

    try-catch

    return

    if: The if statement evaluates a conditional expression and executes a group

    of statements when the expression is true. The optional elseif and else

    keywords provide for the execution of the alternate groups of statements. An

    end keyword, which matches the if, terminates the last group of statements.

    Examples:

    1. x=input('what is the value of x:');

    y = abs(x)

    ifx >= 0

    y = x;

    else

    y = -x;

    end

    Note: The second line of this M-file states that the function has a single input x

    and a single output y. If the input x is nonnegative, the ifstatement is

    determined by MATLAB to be true. Then the command between the ifand

    the elsestatements is executed to set y equal to x, while MATLAB skips the

    command between the else and end statements. However, if x is negative,

  • 8/12/2019 MATLAB PROGRAMMING (FLOW CONTROL)

    2/3

    then MATLAB skips to the else statement and executes the succeeding

    command, setting y equal to -x.

    2. x=input('what is the value of x:');

    y = sign(x)ifx > 0

    y = 1;

    elseifx == 0

    y = 0;

    else

    y = -1;

    end

    Note:The elseifstatement is useful if there are more than two alternatives

    and they can be distinguished by a sequence of true/false tests.In example 2, if the input x is positive, then the output y is set to 1 and all

    commands from the elseif statement to the end statement are skipped. (In

    particular, the test in the elseif statement is not performed.) If x is not

    positive, then MATLAB skips to the elseif statement and tests to see if x

    equals 0. If so, y is set to 0; otherwise y is set to -1. Notice that MATLAB

    requires a double equal sign == to test for equality in logic statement; a single

    equal sign is reserved for the assignment of values to variables.

    Exercises:

    i)

    Write a program to input students score and display the grade. ii) Write a program that will read the account of a customer and

    compute his interest.

    Note: The interest rate is 9% for balance that is less than 5,000. The

    interest is 12% for balance greater or equal to 5,000 and less than

    10,000. The interest is 15% for balance greater than or equal to

    10,000.

    for: Thefor statement repeats a group of statements a fixed, predetermined

    number of times. A matching enddelineates the statements.

    Examples:

    1)for n=(1:5);

    X=2.^n

  • 8/12/2019 MATLAB PROGRAMMING (FLOW CONTROL)

    3/3

    end

    2)s=0;

    for n= (1:3);

    x=2.^n

    s=s+x;end

    Exercises:

    1)Write a program that will generate first five terms of the sequence

    X(n)=(a^n)/n! and find its sum. (a=2).

    2)Write a program to find the sum of the first five terms of the series 1,

    1/(2^2), 1/(3^2), 1/(4^2),

    Solutions:

    Question 1 can solve via two methods, namely, vectorization method and the

    loop method. Loop method makes use offor statement but vectorization doesnot.

    Loop method: loop method:

    for N=1:5; S=0;

    X=2^N/factorial(N) for N=1:5;

    S=0 X=2^N/factorial(N)

    for N=1:5; S=S+X

    S=S+ X=2^N/factorial(N) end

    end

    end

    Vectorization method:

    N=[1:5];

    X=(2.^N)./factorial(N)

    S=sum(X)

    NOTE:for statement does not recognize element by element operations. For

    element by element operations, use dot (.) or else it will see it as a matrix. This

    means that for scalar multiplication dot (.) is used.