Lecture 2

You might also like

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

9/8/2022

NUMERICAL METHODS WITH


MATLAB
LEC 2: CONDITIONAL, & LOOP STATEMENTS

Eng. Sherif Ibrahim Abdelmaksoud

OBJECTIVES

• M-File, & live script


• Subplot, hold on/off command
• To learn how to write syntax (program) with MATLAB

1
9/8/2022

CONTENTS
• Function
• if/else Structure
• switch structure
• for loop
• while loop
• Improving the efficiency of the loop
• Break and continue
• Tic toc

• Nested loop

M-File

Live script

2
9/8/2022

Subplot, hold on/off command

subplot(m,n,p)

hold on
hold off

Function File
The velocity of a free-falling bungee jumper can be computed

This equation can be implemented repeatedly to compute velocity as a function of time.


g = 9.81; m = 68.1; t = 12; cd = 0.25;
v = sqrt(g * m / cd) * tanh(sqrt(g * cd / m) * t)
v = 50.61

The syntax for the function file can be represented generally as


function output_variable = function_name(input_variable)
MATLAB statements
Note:
output_variable = value; MATLAB statements compute
the value that is assigned to
end output_variable

3
9/8/2022

FUNCTION FILE (CONT.)

function v = freefall(t, m, cd)


% v : bungee velocity, cd : second-order drag coefficient, m : mass, t : specific time
g = 9.81;
Note
v = sqrt(g * m / cd)*tanh(sqrt(g * cd / m) * t); To run the function
file:
end The name of the
file must have the
same name of the
function and is
located in the
current folder
Reading Assignment?
Local and global variable , Input function, disp function, and fprintf function

IF STRUCTURE (CASE 1)
if statement has the following form:
if comparison
statements
end
If the comparison is true, the statements between the if statement and the end
statement are executed. If the comparison is false, the program jumps
immediately to the statement following end .
G = input('G is equal to:')
if G>50
disp('pass')
end

4
9/8/2022

IF/ELSE STRUCTURE (CASE 2)


else statement allows us to execute one set of statements if the comparison is
true and a different set if the comparison is false.
x = input('x is equal to:')
if x >0;
y = log(x)
else
disp('The input to the log function must be positive')
end

IF/ELSE STRUCTURE (CASE 3)


elseif statement allows you to check multiple criteria while keeping the code easy to
read.
age = input('your age is : ')
if age<16;
disp('Sorry – You have to wait')
elseif age<18;
disp('You may have a youth license')
elseif age<70;
disp('You may have a standard license')
else
disp('Drivers over 70 require a special license')
end

10

5
9/8/2022

SWITCH STRUCTURE
switch/case structure is often used when a series of programming path options exists for a given
variable, depending on its value.
city = input('To know the Apartment rent, please, enter the name of the city in single quotes : ');
switch city
case 'dammam'
disp('$250')
case 'jeddah'
disp('$150')
case 'makkah'
disp('300')
otherwise Note
disp('Not on file') case '---'
end
11

FOR LOOP
The structure of a for loop can be summarized as
for index = start:finish
How often will the
commands to be executed loop run?
end

for k = [1,3,7,9,11] for k = 1:5 for k = [1,3,7,9,11] i=1:5


k a = 11 a(k) = k^2 for k = 0:length(i) 1*1

end end end a (k) = k^2 1*1 1*2

for k = 1:5 for k = 1:5 end


1*1 1*2 1*3
a = 5^k a (k) = k^2 1*1 1*2 1*3 1*4
end end OR !!
i=1:5
for k = 1:length(i)
a (k) = k^2
Is it working ??
end
12

6
9/8/2022

FOR LOOP (CONT.)


Vectorization Your Conclusion

i = 0;
Container
for t2= 0:0.02:50 t1 = 0:0.02:50;
i = i + 1; y1 = cos(t1);
y2(i) = cos(t2);
end

13

WHILE LOOPS
While loops are similar to for loops. The big difference is the way MATLAB ®
decides how many times to repeat the loop. While loops continue until some
criterion is met.
while condition
commands to be executed
end
k = 0;
while k<3 for k=1:3
k = k+1; disp(k)
disp(k) end
end
In this case, we initialize a counter, k ,
OR !! before the loop. Then the loop
while k<3 repeated as long as k was less than 3.
disp(k) Is it working ??
end

14

7
9/8/2022

DIFFERENCE BETWEEN FOR AND WHILE


For Loop While Loop

s = [76,45,98,97]; s = [76,45,98,97];

c = 0; c = 0;
k = 0; counter
for k=1:length(s) while k<length(s)
k = k+1; counter = 1+ counter
if s(k)>90 if s(k)>90

c = c + 1; c = c + 1;

end end

end end

disp(c) disp(c)

15

NESTED LOOPS
If & for loops can be nested inside each other. The main point to note is that the index of the
inner for moves faster.

for i = 1:10 if i = 1:10


…………… ……………
for j = 1:5 if j = 1:5
............... ...............
end end
end end

16

8
9/8/2022

IMPROVING THE EFFICIENCY OF LOOPS


BREAK, PAUSE AND CONTINUE
The break command can be used to terminate a loop (while the comparison in the first line is
still true).
The continue command is similar to break ; however, instead of terminating the loop, it will be
infinite loop
The pause command is number of seconds to pause execution

x=5 for n = 1:50


while (1) for n = 3:10 if mod(n,7)
if x < 0 mesh (magic (n)) continue
break pause end
end end disp(['Divisible by 7: ' num2str(n)])
x=x-5 end
end

17

IMPROVING THE EFFICIENCY OF LOOPS


BREAK AND CONTINUE
>> tic and toc

tic
i = 0;
for t = 0:0.01:10
i = i + 1;
y(i) = sin(t);
end
toc

tic
t = 0:0.01:10;
y = sin(t);
toc

18

9
9/8/2022

13

19

14

20

10

You might also like