Matlab Ch4 - An

advertisement
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
Matlab Programming for Engineers
Introduction to Matlab
Matlab Basics
Branching Statements
Loops
User Defined Functions
Additional Data Types
Input/Output Functions
Simulink Toolbox
Important Toolboxes (if time is available)
Dr. Nidal Farhat
Mechanical Engineering Department
1
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
OBJECTIVES
 The while Loop
 The for Loop
 Logical Arrays and Vectorization
 The Matlab Profiler
Mechanical Engineering Department
2
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE while LOOP
Structure:
The Code Block is repeated
until the expression is false.
If it is false, the program
executes the first statement
after the end.
Mechanical Engineering Department
3
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE while LOOP
Example:
Write a program that calculates the average,
and the standard deviation,
for a set of values (X), entered by the user.
Mechanical Engineering Department
4
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE while LOOP
Solution:
Mechanical Engineering Department
5
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE while LOOP
Solution (modified):
Mechanical Engineering Department
6
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE while LOOP
Solution (modified):
Mechanical Engineering Department
7
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Structure:
Repeats a block of statements (Body) specified number of times.
Matlab generates an array by
evaluating this expression
column by column.
Legal Examples:
Mechanical Engineering Department
8
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Legal Examples:
Mechanical Engineering Department
9
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Example (The Factorial Function):
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
Mechanical Engineering Department
10
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Example (The Factorial Function):
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
Mechanical Engineering Department
11
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Example, calculating the day of year:
Mechanical Engineering Department
12
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Example:
Use THE for LOOP to calculate the average and standard deviation for
any (n) values (modify the previous while program).
Mechanical Engineering Department
13
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Details of operation:
1. Indent the bodies of loops (automatically done in Matlab).
2. Don’t modify the loop index within the body of a loop.
3. Preallocating arrays.
recall: arr = 1:4; arr(7) = 8;  arr == [1 2 3 4 0 0 8]. (i.e. Matlab
automatically extend/change the size of the array.
It is better to preallocate the array before THE for LOOP to make the
program much more faster.
Mechanical Engineering Department
14
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Details of operation: (4. Vectorizing Arrays)
Faster
Mechanical Engineering Department
15
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
The break and continue Statements:
The break statement terminates the execution of a loop and passes
control to the next statement after the end of the loop, example:
Mechanical Engineering Department
16
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
The break and continue Statements:
The continue statement terminates the current step of the loop and
return the control to the top of the loop, example:
continue
Mechanical Engineering Department
17
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Nesting Loops:
ii
jj
product
Output
Mechanical Engineering Department
18
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Nesting Loops:
Different loop index variables
ii
jj
product
Output
Mechanical Engineering Department
19
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Nesting Loops:
The break/continue statements they apply to the current loop, example:
Mechanical Engineering Department
20
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Logical array and vectorization:
Double array
Logical array
Mechanical Engineering Department
21
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Logical array and vectorization:
Logical arrays can serve as a mask for arithmetic operations. The
specified operation will be applied to the selected elements and not to
the remaining elements, example:
Will take the square root of all
elements for which the logical array b
is true and leave all the other elements
in the array unchanged
Mechanical Engineering Department
22
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Logical array and vectorization:
Example 1:
Calculate the sqrt of all the elements of array (a) > 5.
Faster
Mechanical Engineering Department
23
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Logical array and vectorization:
Example 2: Calculate the sqrt of all the elements of array (a) > 5, and the
square of the remaining (unselected by the previous operation).
Mechanical Engineering Department
24
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
The Matlab Profiler:
Used to identify the parts of the program that consume most execution
time.
Mechanical Engineering Department
25
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
The Matlab Profiler:
Mechanical Engineering Department
26
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Example: (Projectile Motion)
 General equation:
y (t )  y0  v y0 t  12 gt 2
v y0  v0 sin( )
x(t )  x0  vx0 t
 When the ball hits the ground
t  0,
Mechanical Engineering Department
vx0  v0 cos( )
t
2* v y0
g
28
CH4: LOOPS
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOP
Example: (Projectile Motion)
 General equation:
y (t )  y0  v y0 t  12 gt 2
v y0  v0 sin( )
x(t )  x0  vx0 t
 When the ball hits the ground
t  0,
Mechanical Engineering Department
vx0  v0 cos( )
t
2* v y0
g
29
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
THE for LOOP
Example: (Commands and Functions)
Mechanical Engineering Department
30
MATLAB PROGRAMMING FOR ENGINEERS
CH4: LOOPS
Home Work
Solve the following problems:
4.[16, 19, 21]
Mechanical Engineering Department
31
Download