You are on page 1of 19

Process Simulation Lab

(An Undergraduate Lab Course)

Lecture 4
Introduction to MATLAB

Course Instructor: Dr. Shabih Ul Hasan & Dr. Parul Katiyar


Motilal Nehru National Institute of Technology Allahabad
Prayagraj - 211004

1
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

PROGRAMMING
IN
MATLAB
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Programming in MATLAB

• Relational and Logical Operators


• Conditional Statements
• The if-end structure
• The if-else-end structure
• The if-elseif-else-end structure
• Loops
• for-end loops
• while-end loops
• Nested Loops and Nested Conditional Statement
• The break command
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Relational Operators
• A relational operator compares two numbers by determining whether a comparison statement (e.g.,
5<8) is true or false. If the statement is true, value 1 is assigned and if the statement is wrong, value 0
is assigned.

Relational Operator Description


< Less Than
> Greater Than
<= Less than or equal to
>= Greater than or equal to
== Equal to
~= Not equal to
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Logical Operators
• A logical operator examines true/false statements and produces a result that is
true (1) or false (0) according to specific operator.

Logical Operator Name Description


& AND Operates on two operands (A and B). If both are true, the
Example: A&B result is true (1); otherwise the result is false (0).

| OR Operates on two operands (A and B). If either one, or


Example: A|B both, are true, the result is true (1),; otherwise (both are
false) the result is false (0).

~ NOT Operates on one operand (A). Gives the opposite of the


Example: A~B operand; true (1) if the operand is false, and false (0) if
the operand is true.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Order of Precedence
Parentheses

Exponential

Logical NOT (~)

Multiplication, Division

Addition, Subtraction

Relational Operators (>, <, >=, <=, ==, ~=)

Logical AND (&)

Logical OR (|)
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Conditional Statements
• The if-end Structure
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

• The if-end Structure


• Example script file

• Execution on command window


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

• The if-else-end Structure


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

• The if-else-end Structure


• Example function file

• Execution on command window


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

• The if-elseif-end Structure


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

• The if-else-end Structure


• Example Script file • Execution on command window
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Loops
• A loop control is used to execute a set of commands repeatedly.

• The set of commands is called the body of the loop.

• MATLAB has two loop control techniques


• Counted loops - executes commands a specified number of times. (for loop)
• Conditional loops - executes commands as long as a specified expression is true. (while loop)

• The keyword “end” is used to show the end of a Loop.


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

For Loop
• Repeats for specified number of times.

• ALWAYS executes computation loop at least once!!!

• Can use + or – increments.

• Can escape (BREAK) out of computational loop.


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

For Loop
• Example function file using for loop

• Execution on matlab command window:


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

While Loop
• Will do computational loop ONLY if while condition is met.

• Be careful to initialize while variable.

• Can loop forever if while variable is not updated within loop!!!


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

While Loop
• Example file for while loop

• Execution on matlab command window


Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

Nested statements and loops


• When an conditional statement is used inside another conditional statement. It is called nested
statement.

• You can see the nesting in the provided example file.

• In the similar way we can also place loops inside conditional statements or other loops.
Adarsh Jain (Department of Chemical Engineering) MNNIT Allahabad, Prayagraj

The Break Command


• Break Command

• When inside a loop (for or while), the break command terminates the execution of loop (the
whole loop, not just the last pass).

• If the break command is inside a nested loop, only the nested loop is terminated.

• When a break command appears outside a loop in a script of function file, it terminates the
execution of the file.

• The break command is usually used within a conditional statement. In loops it provides a
method to terminate the looping process if some condition is met.

You might also like