Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Chapter 4_Programming with Matlab_Part 2

The document provides an overview of programming concepts in Matlab, focusing on loops, conditional statements, and the switch structure. It explains the use of for and while loops, including their syntax and rules, along with examples and practices for creating matrices. Additionally, it discusses the break and continue commands for loop control and offers a comparison between if statements and switch structures for better readability.

Uploaded by

vpnqynh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Chapter 4_Programming with Matlab_Part 2

The document provides an overview of programming concepts in Matlab, focusing on loops, conditional statements, and the switch structure. It explains the use of for and while loops, including their syntax and rules, along with examples and practices for creating matrices. Additionally, it discusses the break and continue commands for loop control and offers a comparison between if statements and switch structures for better readability.

Uploaded by

vpnqynh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming with Matlab

➢Program Design and Development


➢Relational Operators and Logical Variables
➢Logical Operators and Functions
➢Conditional Statements
➢Loops
➢The switch Structure
➢Debugging Matlab Programs
➢Applications to Simulation
A LOOP: a structure for repeating a calculation a
number of times. Each repetition of the loop is a
pass.

There are two types of explicit loops:

▪ for loop: the number of passes is known ahead of


time.

▪ while loop: the looping process must terminate


when a specified condition is satisfied, and thus
the number of passes is not known in advance.
for loop variable = m:s:n
statements
space end

m: initial value
s: step value or incremental value
n: terminating value

The loop variable is assigned the initial value of m and is


incremented by the value s  the statements are executed
once during each pass, using the current value of the loop
variable.
Start
Set k = m

k>n
True
?
Increment k
by s
False

Statements

End
Statements
following the
end statement Flow chart of a for loop
Ex:
for k = 5:10:35 for k = 5:10:35 , x = k^2 , end
x = k^2
end less readable
The loop variable k is initially assigned the value 5, and x is
calculated from x = k^2.
Each successive pass through the loop increment k by 10 and
calculates x until k exceeds 35.
k takes on the values 5, 15, 25 and 35 with the corresponding
values of x are: 25, 225, 625, and 1225.
The program then continues to execute any statements
following the end statement.
Rules when using for loops:
1. The step value s may be negative.
2. If s is omitted, the step value defaults to one.
3. If s is positive, the loop will not be executed if m is greater
than n.
4. If s is negative, the loop will not be executed if m is less than
n.
5. If m equals n, the loop will be executed only once.
6. If the step value s is not an integer, round-off errros can
cause the loop to execute a different number of passes than
intended.
When the loop is completed, k retains its last value.
 Should not alter the value of the loop variable within the
statements.
Create a special square matrix that has ones in the first row and first
column, and whose remaining elements are the sum of two elements, the
element above and the element to the left, if the sum less than 20.
Otherwise, the element is the maximum of those two elements.
function A = specmat (n)
A = ones(n) % create the matrix
for r = 1:n % for row
for c = 1:n % for column
if(r>1)&(c>1)
s = A(r-1,c) + A (r, c-1);
if(s<20)
A(r,c)= s; % create the elements ≠ A(1,1)
else
A(r,c)=max(A(r-1,c),A(r,c-1));
end
end
end
end
Practice:
Write a program to produce the following matrices:
 4 8 12 
10 14 18 
A= 
16 20 24
 
22 26 30 

1 3 57
3 6 0 0 
B=
5 0 10 0 
 
7 0 0 14
break command: terminates the loop but does not stop
the entire program.

for k = 1:10
x = 50 – k^2
if x<0
break
end
y=sqrt(x)
end

break command can be replaced by while loop (in next part)


continue command: is used in applications where we want to
not execute the case producing an error but continue executing
the loop for the remaining passes.
 passes control to the next iteration of the for or while loop in
which it appears, skipping any remaining statements in the
body of the loop. x = [10,1000,-10,100];
y = NaN*x;
for k = 1:length (x)
if x(k) < 0
continue
end
y(k) = log10 (x(k));
End
y
The result is: y = 1 , 3 , NaN , 2
while Loops: is used when the looping process terminates
because a specified condition is satisfied, and thus the number
of passes is not known in advanced.

while logical expression


statements
end
The logical expression is tested first. If the logical expression is
true, the statements are executed.
1. The loop variable must have a value before the while
statement is executed.
2. The loop variable must be changed somehow by the
statements.
Start

Logical False
expressio
n

True

Statements
(which
increment the
loop variable)

End
Statements
following the
end statement
Flow chart of a while loop
Ex: x=5;
5
while x<25 9
disp(x) 17
x=2*x-1;
end

while x<25 An error message will occur


disp(x)
x=2*x-1;
end
Practice:
1. Rewrite the following code using a while loop to avoid using
the break command.
for k=1:10
x=50-k^2;
if x<0
break
end
y=sqrt(x)
end
2. Display all values and factorials of n if they are less than
200000.
switch structure: provides an alternative to using the if, elseif,
and else commands. switch can be used to replace for if and
vice versa, but more readable.

switch input expression (scalar or string)


case value 1
statement group 1
case value 2
statement group 2
.
.
.
otherwise
statement group n
end
Ex:
angle=input('Enter angle:');
switch angle
case 45
disp('Northeast')
case 135
disp('Northeast')
case 225
disp('Southwest')
case 315
disp('Northwest')
otherwise
disp('Direction Unknown')
end

You might also like