Download presentation
Presentation is loading. Please wait.
Published bySibyl Hamilton Modified over 9 years ago
1
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers
2
Lecture Outline Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Testing the Random Number Generator “rand” Vectorization Slide 2 of 18
3
Testing the Random Num Generator “rand” Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Problem: How can we test the “randomness” of the random number generator “rand” in MATLAB? Solution: Develop a histogram of the random numbers generated!! Generate N total random integers each between 1 and 10 Count how many times each of the numbers appear Plot the result as a bar chart o Scale the count to total 100% Plot the result as a pie chart Slide 3 of 18
4
Testing the Random Num Generator “rand” Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers PseudoCode: Prompt the user for N = # of random nums to be generated Initialize a histogram to store the 10 sums In a loop: 1:N o Generate a random integer (1 to 10) o Update the histogram (i.e., appropriate sum) Scale the histogram to total 100% Plot the result as a bar chart Plot the result as a pie chart o Explode the most frequently occurring integer RandTest.mRandTest.m courtesy of Hilken, Tanner R. Slide 4 of 18
5
Testing the Random Num Generator “rand” Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Results obtained for N = 500 points Slide 5 of 18
6
Loops and Vectorization Preallocation of Arrays Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers MATLAB allows arrays to grow dynamically. a = 1:4; for i = 1:6 a(i) = i^2; end Loop 1: i =1 a(1) = 1^2 = 1 Loop 2: i =2 a(2) = 2^2 = 4 Loop 3: i =3 a(3) = 3^2 = 9 Loop 4: i =4 a(4) = 4^2 = 16 Loop 5: i =5 a(5) = 5^2 = 25 Loop 6: i =6 a(6) = 6^2 = 36 a =1234 1234 1434 1494 14916 a =1491625 a =149162536 Slide 6 of 18
7
Loops and Vectorization Preallocation of Arrays Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Dynamically resizing arrays is VERY slow!! Pre-allocate array sizes appropriately N = 1000; a = zeros(1, N); for i = 1:N a(i) = i^2; end Initialization pre-allocates the size of the array “a” for i = 1:10000000 x(i) = i; end Non-Preallocated Array Case x=1:10000000; Preallocated Array Case Elapsed time = 1.9668 sec Elapsed time = 0.023375 sec 84 Times FASTER!! 84 Times FASTER!! Slide 7 of 18
8
Loops and Vectorization Vectorization Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers MATLAB natively “understands” arrays Operating on vector objects is fast E.g., Compute: N = 1000000; a = zeros(1, N); i = 0; for t = 0:1/N:1 i = i+1; a(i) = sin(3*t)*t^2; end Non-Vectorized Array Case N = 1000000; t = 0:1/N:1; a = sin(3*t).*t.^2; Vectorized Array Case Elapsed time = 1.249 secElapsed time = 0.0063078 sec 198 Times FASTER!! 198 Times FASTER!! Slide 8 of 18
9
Loops and Vectorization The break and continue statements Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers What if you want to exit a for or while loop or skip back to the top of the loop? break -> exits the loop to statement after end If the break statement is in a nested loop – control jumps the next loop level. E.g., break will not kick you all the way out of nested loops – just the current level. continue -> jumps to end and loops again (unless finished). Statements between continue and end are not executed! Slide 9 of 18
10
Loops and Vectorization The break and continue statements Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers The following code uses both continue and break. for i=1:10 if (i > 3) && (i <= 5) continue elseif i == 8 break end fprintf('This is loop %d\n',i); end Loop 1: This is loop 1 Loop 2: This is loop 2 Loop 3: This is loop 3 Loop 4: - continue - Loop 5: - continue - Loop 6: This is loop 6 Loop 7: This is loop 7 Loop 8: - break - This is loop 1 This is loop 2 This is loop 3 This is loop 6 This is loop 7 Output: Slide 10 of 18
11
Loops and Vectorization Quiz Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers What is the result from executing the following? 1. 2. 3. 4. 5. for k = 8:10 fprintf('k = %g \n',k); end k = 8 k = 9 k = 10 for j = 8:-1:10 fprintf(' j = %g \n',j); end NOTHING!! for l = 1:10:10 fprintf(' l = %g \n',l); end i = 1 for i = -10:3:-7 fprintf(' i = %g \n',i); end i = -10 i = -7 for m = [0 2 -3] fprintf(' m = %g \n',m); end m = 0 m = 2 m = -3 Slide 11 of 18
12
Loops and Vectorization Quiz Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers What is the value of “x” at the end of each loop? 1. 2. 3. x = 0; for m = 1:5 x = x + 1; end x = 5 x = 0; for m = 1:5 x = x + m; end x = 15 x = 1; for m = 1:5 if m == 2 continue; elseif x > 8 break; end x = x + m; fprintf(' m = %g & x = %g \n', m, x); end fprintf(' End: x = %g \n', x); m = 1 & x = 2 m = 3 & x = 5 m = 4 & x = 9 End: x = 9 Slide 12 of 18
13
Loops and Vectorization An Example - Fitting noisy data to a line Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Example: Fitting noisy data to a line Eqn. of a Line: y = m x + c Fit the following measured data to a line Least Squares Fit? Speed of a DC motor as the voltage was increased Slide 13 of 18
14
Loops and Vectorization An Example - Fitting noisy data to a line Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 14 of 18
15
Loops and Vectorization An Example - Fitting noisy data to a line Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 15 of 18
16
Loops and Vectorization An Example - Fitting noisy data to a line Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Given the data: x = [ 0.17, 1.02, 2.13, 3.13, 4.22, 5.12, 6.10, 7.17, 8.19, 9.13, 10.09]; y = [ 0.95, 3.34, 2.79, 4.10, 7.55, 6.70, 7.96, 10.07, 9.89, 12.16, 11.74]; Slide 16 of 18
17
Loops and Vectorization An Example - Fitting noisy data to a line Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Least Square fit of “noisy” data to a line LS_line_fit.m Slide 17 of 18
18
Next Lecture Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Nested Loops Profiling Slide 18 of 18
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.