Numerical Methods in Computing: Lectures By: DR Taimoor Iqbal
Numerical Methods in Computing: Lectures By: DR Taimoor Iqbal
Chapter 6
Programming in MATLAB
Department of Mathematics, UET, Lahore
Example
>> x=8:12
x = 8 9 10 11 12
>> x>10
ans = 0 0 0 1 1
>> x==11
ans = 0 0 0 1 0
>> x>=7
ans = 1 1 1 1 1
Department of Mathematics, UET, Lahore
6.1 Relational and Logical Operators
It helps to picture in your mind that the
result of a logical comparison
1. Is a vector
2. Has a 0 or 1 corresponding to each original
TIP
element
>> x=8:12
x = 8 9 10 11 12
>> x>10
ans = 0 0 0 1 1
Department of Mathematics, UET, Lahore
6.1 Relational and Logical Operators
EXAMPLE
TIP
How many of the numbers from 1-20
are prime?
• Use MATLAB isprime command, which returns
true (1) is number is prime and false (0) if it isn't
>> numbers = 1:20;
>> sum( isprime(numbers) ) ans =
8
6.1 Relational and Logical Operators
EXAMPLE
What are the numbers from 1-10 that are
multiples of 3?
>> numbers = 1:10
numbers = 1 2 3 4 5 6 7 8 9 10
>> multiples = rem( numbers, 3 ) == 0
multiples = 0 0 1 0 0 1 0 0 1 0
>> multiplesOf3 = numbers(multiples)
multiplesOf3 =
3 6 9
Department of Mathematics, UET, Lahore
6.1 Relational and Logical Operators
Example
Think of numbers(multiples) as pulling
out of numbers all elements that have a 1
in the corresponding element of
multiples
numbers = 1 2 3 4 5 6 7 8 9 10
multiples = 0 0 1 0 0 1 0 0 1 0
numbers(multiples) = 3 6 9
Department of Mathematics, UET, Lahore
6.1 Relational and Logical Operators
EXAMPLE
What are the prime numbers from 1-20?
• >> numbers = 1:20;
• >> numbers( isprime(numbers) ) ans =
•2 3 5 7 11 13 17 19
EXAMPLE
>> age=[45 47 15 1 11]
3
age = 45 47 15 13 11
Who is not a teenager?
>> ~(age>=13 & age<=19) ans = 1 1
0 1
0
Who is an adult or a child?
>> age>19 | age<13 ans = 1 1 0
0 1
6.1 Relational and Logical Operators
Built-in logical functions:
MATLAB has some built-in functions or
commands for doing logical operations
and related calculations. Three are
equivalent to the logical operators
• and(A,B) – same as A&B
• or(A,B) – same as A|B
• not(A) – same as ~A
6.1 Relational and Logical Operators
Fig. 6-3 shows the code and the flowchart for the
if-elseif-else-end structure
Department of Mathematics, UET, Lahore
6.2.3 The if-elseif-else-end Structure
Concept is
switch name case
'Bobby'
talk for a long time
case 'Susan'
talk for a little bit and then set a time to meet
case 'Hubert'
talk until you get the answer to the hard problem
case 'Mom'
say you're busy and can't talk
otherwise
have your room-mate say you'll call back later
end
Department of Mathematics, UET, Lahore
6.3 The switch-case Statement
Department of Mathematics, UET, Lahore
6.3 The switch-case Statement
switch evaluates
switch-expression
Body of loop
Body of loop
Body of loop
Body of loop
Body of loop
Body of loop
Output
k = 1
x = 1
EXAMPLE Script k = 4
for k=1:3:10 x = 16
k k = 7
x = k^2 x = 49
end k = 10
x = 100
After loop k = 10
fprintf('After loop k = %d\n', k);
Department of Mathematics, UET, Lahore
6.4.1 for-end Loops
EXAMPLE
Department of Mathematics, UET, Lahore
6.6 THE break AND continue COMMANDS
EXAMPLE
while( 1 ) Trick – "1" is always true so it makes loopiterate forever!
name = input( 'Type name or q to quit: ', 's' );
if length( name ) == 1 && name(1) == 'q'
break;
If user entered only one
else Only way to exit loop!
letter and it is a "q", jump
fprintf( 'Your name is %s\n', name );
out of loop
end
end
Otherwise print name
Output for inputs of "Greg", "quentin", "q"
Type name or q to quit: Greg
Your name is Greg
Type name or q to quit: quentin
Your name is quentin
Type name or q to quit: q
>>
Department of Mathematics, UET, Lahore
6.5 Nested Loops and Nested Conditional Statements
The continue command:
EXAMPLE
for ii=1:100
if rem( ii, 8 ) == 0
Every eight iteration reset
count = 0; count to zero, print the
iteration number, and skip
fprintf('ii=%d\n',ii); the remaining
continue; computations in the loop
end
% code
% more code
end
Department of Mathematics, UET, Lahore
THE END