Download as pdf or txt
Download as pdf or txt
You are on page 1of 70

School of Engineering

Computing Application for Engineers


(Data Measurement Analysis and Experimental Design)

Lecture 2 – Programming in MATLAB


About me

HELLO
Hardie Hidayat M. M. (PhD)
Lecturer, School of Engineering (ME)
Telephone Ext: 5263

Email: hardie.hidayat@taylors.edu.my

Office: C-9-17

TIMeS message.
1 Relational Operator

2 Conditional Statements
Table
of
Contents 3 Loops

4
Function
Relational Operator
Use to make comparisons
Relational Operator Definition

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

.== Equal to

~= Not equal to
Relational Operator

The result of a comparison using the relational


operators is a logical value, i.e.,

0 - if the comparison is false


1 - if the comparison is true
Relational Operator – Example 1
If x = 2 and y = 5, predict the solution when z = x < y

>> x = 2;
>> y = 5;
>> z = x < y
>> z =

1
Relational Operator – Example 2
If x = 2 and y = 5, predict the solution when z = x==y

>> x = 2;
>> y = 5;
>> z = x==y
>> z =

0
Relational Operator – Example 3
If x = [6, 3, 9] and y = [14, 2, 9], predict the following
solution

>> x = [6, 3, 9];


>> y = [14, 2, 9];
>> z1 = (x < y); 1 0 0
>> z2 = (x > y); 0 1 0
>> z3 = (x ~= y); 1 1 0
Relational Operator – Example 3
If x = [6, 3, 9] and y = [14, 2, 9], predict the following
solution

>> x = [6, 3, 9];


>> y = [14, 2, 9];
>> z1 = (x < y); 1 0 0
>> z2 = (x > y); 0 1 0
>> z3 = (x ~= y); 1 1 0
1
Relational Operator

2
Table Conditional Statements
of
Contents 3
Loops

4 Function
Conditional
Variable
Statements
Conditional
Variable
Statements
Variable
Explanation (1)
Variable
Explanation (2)
Variable
Explanation (3)
Variable
Explanation (4)
Variable
Example 4
1
Relational Operator

2
Table Conditional Statements
of
Contents 3
Loops

4 Function
Loops
for loop
for loop
for loop
for loop
for loop
for loop
for loop
for loop
for loop
for loop
for loop
Example
Example
while loop
while loop
while loop
while loop
while loop
while loop
while loop
while loop
while loop
while loop
while loop
while loop
while loop
while loop
Example
Example
Example
1
Relational Operator

2
Table Conditional Statements
of
Contents 3
Loops

4 Function
M-files
• ACSII text file with file extension .m

• It is called a script
each line is executed just as if you had typed it
into MATLAB’s command window
Function Declaration
Function Declaration
Function Declaration
Function Declaration
Input Arguments
Input Arguments
Return Value
Return Value
Multiple Return Values
Multiple Return Values
switch
switch
Switch - written in M-file
Switch - written in M-file (cont)
Switch - written in M-file (break)
Useful Functions
• Create arrays
ones, zeros, rand,
Please find out more info
• Getting information
about these commands
length, size

• Array operations
find, max, min, sum, sort
1
Relational Operator

2
Table Conditional Statements
of
Contents 3
Loops

4 Function

You might also like