0% found this document useful (0 votes)
21 views20 pages

Chapter 6

This document discusses MATLAB programming concepts including relational operators, logical operators, logical built-in functions, conditional statements like if-else and switch-case statements, loops like for, while, and nested loops, and break and continue commands. It provides examples of how to use these programming structures and concepts in MATLAB code.

Uploaded by

Fotus Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Chapter 6

This document discusses MATLAB programming concepts including relational operators, logical operators, logical built-in functions, conditional statements like if-else and switch-case statements, loops like for, while, and nested loops, and break and continue commands. It provides examples of how to use these programming structures and concepts in MATLAB code.

Uploaded by

Fotus Smith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Chapter 6

Programming in
MATLAB
1

MATLAB
Ch 6
RELATIONAL OPERATOR

If two scalars are compared, the result is a scalar 1 or 0. If two


arrays are compared (only arrays of the same size can be
compared), the comparison is done element-by-element
2

MATLAB
Ch 6
LOGICAL OPERATOR

MATLAB
Ch 6
Order of Precedence

Precedence Operation
1 (highest) Parentheses
2 Exponentiation
3 Logical NOT (~)
4 Multiplication, division
5 Addition, subtraction
6 Relational operators (>, <, >=, <=, = =, ~=)
7 Logical AND (&)
8 (lowest) Logical OR ( | )

MATLAB
Ch 3
Logical Built-in Functions

Symbol Description Example

all(A) Returns 1 (true) if all elements >> A=[6 2 15 9 7 11];


in a vector A are true (nonzero). >> all(A)
Returns 0 (false) if one ans =
or more elements are false (zero). 1
If A is a matrix, treats columns of A as >> B=[6 2 15 9 0 11];
vectors, and returns a vector with 1s >> all(B)
and 0s. ans =
0
any(A) Returns 1 (true) if any element in a >> A=[6 0 15 0 0 11];
vector A is true (nonzero). >> any(A)
Returns 0 (false) if all elements are ans =
false (zero). 1
If A is a matrix, treats columns of A as >> B = [0 0 0 0 0 0];
vectors, and returns a vector with 1s >> any(B)
and 0s. ans = 5

0
MATLAB
Ch 3
Logical Built-in Functions

Symbol Description Example

find(A) If A is a vector, returns the indices >> A=[0 9 4 3 7 0 0 1


of the nonzero elements. 8];
>> find (A)
ans =
234589

find(A>d) If A is a vector, returns the >> A=[0 9 4 3 7 0 0


address of the elements that 1 8];
are larger than d (any relational >> find(A>4)
ans =
operator can be used).
259

MATLAB
Ch 3
Conditional Statement

❖ The if-end Structure

MATLAB
❖ The if-end Structure Ch 3

1. Initialize a
2. Initialize x to a/2
3. Repeat 6 times (say)
Replace x by (x + a/x)/2
Display x
4. Stop.

a = 2;
x = a/2;
disp([’The approach to sqrt(a) for a = ’, num2str(a)])
for i = 1:6
x = (x + a / x) / 2;
disp( x )
end
disp( ’Matlab’’s value: ’ )
8
disp( sqrt(2) )
MATLAB
Eg. The if-end Structure Ch 3

n! = 1 × 2 × 3 × . . . × (n − 1) × n.

n = 10;
fact = 1;
for k = 1:n
fact = k * fact;
disp( [k fact] )
end

MATLAB
Ch 3
Conditional Statement

❖ The if-else-end Structure

10

MATLAB
Ch 3
Conditional Statement

❖ The if-elseif-else-end Structure

11

MATLAB
Ch 3
Eg. The if-elseif-else-end Structure

bal = 15000 * rand;


if bal < 5000
rate = 0.09;
elseif bal < 10000
rate = 0.12;
else
rate = 0.15;
end
newbal = bal + rate * bal;
format bank
disp( ’New balance is:’ )
12
disp( newbal )
MATLAB
Mark = input(‘ Enter Marks: ‘)
Mark = input(‘ Enter Marks: ‘)
if Mark < 20
if Mark < 50
disp( Grade E )
disp( Fail )
elseif Mark < 40
else

ME - 41019
disp( Grade D)
disp( Pass )
elseif Mark < 60
end
disp( Grade D)
elseif Mark < 80
disp( Grade D)
elseif Mark >= 80
disp( Grade A)
end

13
Ch 3
The switch-case Statement

14

MATLAB
The switch-case Statement

d = floor(10*rand);
switch d
case {2, 4, 6, 8}
disp( ’Even’ );
case {1, 3, 5, 7, 9}
disp( ’Odd’ );
otherwise
disp( ’Zero’ );
end
15

MATLAB
Ch 3
LOOPS

❖ For and End Loop

for k=1:3:10
x = k^2 16

end
MATLAB
Ch 3
LOOPS

❖ While and End Loop

x=1
while x<=15
x=2*x
17
end

MATLAB
Ch 3
NESTED LOOPS

18

MATLAB
Ch 3
THE break AND continue COMMANDS

for t = 0:2:100
height = 60 + 2.13*t^2 – 0.0013*t^4 + 0.000034*t^4.75*t;
if height < 0
break
end
end
fprintf (‘At %3d sec rocket altitude = %8.3f ft \n’, t, height)

19

MATLAB
20

MATLAB

You might also like