Professional Documents
Culture Documents
Lecture 2 Programming in MATLAB
Lecture 2 Programming in MATLAB
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
.== Equal to
~= Not equal to
Relational Operator
>> 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
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