ch4: loops mechanical engineering department matlab programming for engineers 1 matlab programming...
TRANSCRIPT
![Page 1: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/1.jpg)
1
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers Matlab Programming for Engineers
Dr. Nidal Farhat
Introduction to MatlabIntroduction to Matlab Matlab Basics Matlab Basics Branching Branching Statements Statements LoopsLoops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)
Introduction to MatlabIntroduction to Matlab Matlab Basics Matlab Basics Branching Branching Statements Statements LoopsLoops User Defined Functions User Defined Functions Additional Data Types Additional Data Types Input/Output Functions Input/Output Functions Simulink Toolbox Simulink Toolbox Important Toolboxes (if time is available) Important Toolboxes (if time is available)
![Page 2: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/2.jpg)
2
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
OBJECTIVES OBJECTIVES
The while Loop
The for Loop
Logical Arrays and Vectorization
The Matlab Profiler
![Page 3: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/3.jpg)
3
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE while LOOPTHE 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.
![Page 4: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/4.jpg)
4
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE while LOOPTHE while LOOP
Example:
Write a program that calculates the average,
and the standard deviation,
for a set of values (X), entered by the user.
![Page 5: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/5.jpg)
5
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE while LOOPTHE while LOOP
Solution:
![Page 6: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/6.jpg)
6
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE while LOOPTHE while LOOP
Solution (modified):
![Page 7: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/7.jpg)
7
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE while LOOPTHE while LOOP
Solution (modified):
![Page 8: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/8.jpg)
8
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Structure:
Repeats a block of statements (Body) specified number of times.
Legal Examples:
Matlab generates an array by evaluating this expression
column by column.
![Page 9: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/9.jpg)
9
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Legal Examples:
![Page 10: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/10.jpg)
10
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Example (The Factorial Function):
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
![Page 11: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/11.jpg)
11
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Example (The Factorial Function):
N! = N * (N-1) * (N-2) * … * 3 * 2 * 1
![Page 12: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/12.jpg)
12
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Example, calculating the day of year:
![Page 13: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/13.jpg)
13
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Example:
Use THE for LOOP to calculate the average and standard deviation for any (n) values (modify the previous while program).
![Page 14: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/14.jpg)
14
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE 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.
![Page 15: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/15.jpg)
15
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Details of operation: (4. Vectorizing Arrays)
Faster
![Page 16: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/16.jpg)
16
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE 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:
![Page 17: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/17.jpg)
17
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE 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
![Page 18: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/18.jpg)
18
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Nesting Loops:
Output
ii jj product
![Page 19: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/19.jpg)
19
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Nesting Loops:
Output
ii jj product
Different loop index variables
![Page 20: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/20.jpg)
20
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Nesting Loops:
The break/continue statements they apply to the current loop, example:
![Page 21: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/21.jpg)
21
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Logical array and vectorization:
Double array
Logical array
![Page 22: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/22.jpg)
22
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE 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
![Page 23: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/23.jpg)
23
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Logical array and vectorization:
Example 1:
Calculate the sqrt of all the elements of array (a) > 5.
Faster
![Page 24: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/24.jpg)
24
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE 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).
![Page 25: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/25.jpg)
25
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
The Matlab Profiler:
Used to identify the parts of the program that consume most execution time.
![Page 26: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/26.jpg)
26
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
The Matlab Profiler:
![Page 27: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/27.jpg)
28
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Example: (Projectile Motion)
General equation:
When the ball hits the ground 0
2*0, yvt t
g
0
0
210 2
0
( )
( )
y
x
y t y v t gt
x t x v t
0
0
0
0
sin( )
cos( )
y
x
v v
v v
![Page 28: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/28.jpg)
29
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Example: (Projectile Motion)
General equation:
When the ball hits the ground 0
2*0, yvt t
g
0
0
210 2
0
( )
( )
y
x
y t y v t gt
x t x v t
0
0
0
0
sin( )
cos( )
y
x
v v
v v
![Page 29: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/29.jpg)
30
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
THE for LOOPTHE for LOOP
Example: (Commands and Functions)
![Page 30: CH4: LOOPS Mechanical Engineering Department MATLAB PROGRAMMING FOR ENGINEERS 1 Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab](https://reader036.vdocuments.us/reader036/viewer/2022062308/56649eef5503460f94bffa7f/html5/thumbnails/30.jpg)
31
CH4: LOOPS
Mechanical Engineering Department
MATLAB PROGRAMMING FOR ENGINEERS
Home WorkHome Work
Solve the following problems:
4.[16, 19, 21]