Lecture 4 Programmng With Matlab
Lecture 4 Programmng With Matlab
B) Flowcharts
• Are useful for developing and documenting
programs that contain conditional
statement, because they can display the
various paths (called "branches") that a
program can take, depending on how the
conditional statements are executed.
• The flowchart representation of the if
statement is shown in Figure 4. 1-2.
• Flowcharts use the diamond symbol to
indicate decision points.
Pseudocode
• Use of natural language, such as English, to describe algorithms often
results in a description that is too verbose and is subject to
misinterpretation.
• Pseudocode may also use some simple MATLAB syntax to explain the
operation of the program.
Finding Bugs
• Debugging a program is the process of finding and removing the
"bugs," or “errors” in a program.
Syntax:
if logical expression
Start
statements
end
If x>=0
Example:
True
if x >= 0 False
y = sqrt (x) y = sqrt(x)
end
OR
end
Example:
z = O; w = 0 ;
if (x >= O) & (y >= 0)
z = sqrt(x) + sqrt(y)
w = log(x) - 3*log(y)
End
Conditional Statements
if Statement
We may "nest" if statements, as shown by the following example.
if logical expression I
statement group 1
if logical expression 2
statement group 2
end
End
Syntax: Start
if logical expression
statement group J
else False
statement group 2 If x>=0
end
True
Example:
if x >= 0 y = sqrt(x) y = exp(x) - 1
y = sqrt (x )
else
y = exp (x) - 1
end
end
Flow Chart of else Statement
Conditional Statements
Example
x = [4 ,- 9,25);
if x < 0
disp ( ' Some of the elements of x are negative .' )
else
y = sqrt (x)
end
x = [-4 ,- 9,-25);
if x < 0
disp ( ' Some of the elements of x are negative .' )
else
y = sqrt (x)
end
Conditional Statement
Example
Conditional Statements
C) The elseif Statement Start
Syntax:
if logical expression 1
statement group 1 logical
False
exp 1
elseif logical expression 2
statement group 2 True False
logical
exp 2
else Statement 1
end
Statement 2 Statement 3
end
Start
False
X>10
True False
X>=0
y = log(x)
True
y = sqrt(x) y = exp(x) - 1
end
Example:
To find Even and odd number
Example:
clear
N = input('Give numerator: ');
D = input('Give denominator: ');
if D==0
'Sorry, cannot divide by zero‘
else
ratio = N/D
end
Conditional Statements
Example:
clear
month = input('Give month number (1-12): ' );
a) for loop:
when the number of passes is known ahead of time.
b) while loop:
when the looping process must terminate when a specified
condition is satisfied, and thus the number of passes is not
known in advance.
Loops
a) for Loops:
The typical structure /syntax of a for loop is
for i = 1:2
for j = 1:6
H(i,j) = 1/(i+j);
end
End
>>H
H=
0.5000 0.3333 0.2500 0.2000 0.1667 0.1429
0.3333 0.2500 0.2000 0.1667 0.1429 0.1250
Loops
b) while Loops:
•The while loop is used when the looping process terminates
because a specified condition is satisfied, and thus the number
of passes is not known in advance.
Start
Syntax
while logical expression False
Log
statements exp
end
True
Example:
Statement
x=5; Which
increment the
while x < 25 loop variable
disp (x)
x = 2*x - 1;
end
end
Flow Chart of while loop
Loops
b) while Loops: Start
Example:
x=1; False
while x ~= 5 Log
exp
disp (x)
x = x +1; True
end Statement
Which
increment the
loop variable
end
Angle=input(‘Enter Angle’)
switch Angle
case 45
disp ( ' Northeast' )
case 135
disp ( ' Southeast' )
case 225
disp ( ' Southwest ' )
case 315
disp ( 'Northwes t ' )
otherwise
disp ( ' Direction Unknown ' )
end
The switch Structure
• Example:
switch angle
case {0 ,360}
disp ( 'North ' )
case {-180 ,180}
disp ( ' South ' )
case {-270 , 90}
disp( ' East ' )
case {-90 , 270}
disp( ' West ' )
otherwise
disp ( ' Direction Unknown ' )
end
The switch Structure
• Example: