advertisement

EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Lecture Outline • Testing the Random Number Generator “rand” • Vectorization Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 2 of 18 Testing the Random Num Generator “rand” • 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 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 3 of 18 Testing the Random Num Generator “rand” • 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 o Generate a random integer (1 to 10) 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.m courtesy of Hilken, Tanner R. <[email protected]> Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 4 of 18 Testing the Random Num Generator “rand” • Results obtained for N = 500 points Random Number Generator Test: N = 500 points Random Number Generator Test: N = 500 points 12 8% 10% 9% 10 Freequency of Occurance (%) 12% 8 10% 6 9% 4 9% 11% 2 11% 0 11% 1 2 3 4 5 6 7 Value Generated Wednesday 15 Oct 2014 8 9 10 EGR 115 Introduction to Computing for Engineers Slide 5 of 18 1 2 3 4 5 6 7 8 9 10 Loops and Vectorization Preallocation of Arrays • MATLAB allows arrays to grow dynamically. a = 1:4; for i = 1:6 a(i) = i^2; end a= 1 2 3 4 Loop 1: i =1 a(1) = 1^2 = 1 a= 1 2 3 4 Loop 2: i =2 a(2) = 2^2 = 4 a= 1 4 3 4 Loop 3: i =3 a(3) = 3^2 = 9 a= 1 4 9 4 Loop 4: i =4 a(4) = 4^2 = 16 a= 1 4 9 16 Loop 5: i =5 a(5) = 5^2 = 25 a= 1 4 9 16 25 Loop 6: i =6 a(6) = 6^2 = 36 a= 1 4 9 16 25 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers 36 Slide 6 of 18 Loops and Vectorization Preallocation of Arrays • 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” Non-Preallocated Array Case Preallocated Array Case for i = 1:10000000 x(i) = i; end x=1:10000000; Elapsed time = 1.9668 sec Elapsed time = 0.023375 sec Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers 84 Times FASTER!! Slide 7 of 18 Loops and Vectorization Vectorization • MATLAB natively “understands” arrays Operating on vector objects is fast E.g., Compute: a Sin 3t t 2 , t 0 :106 :1 Non-Vectorized Array Case N = a = i = for 1000000; zeros(1, N); 0; t = 0:1/N:1 i = i+1; a(i) = sin(3*t)*t^2; Vectorized Array Case N = 1000000; t = 0:1/N:1; a = sin(3*t).*t.^2; 198 Times FASTER!! end Elapsed time = 1.249 sec Wednesday 15 Oct 2014 Elapsed time = 0.0063078 sec EGR 115 Introduction to Computing for Engineers Slide 8 of 18 Loops and Vectorization The break and continue statements • 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! Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 9 of 18 Loops and Vectorization The break and continue statements • 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 Output: This is loop 1 This is loop 2 This is loop 3 This is loop 6 This is loop 7 Wednesday 15 Oct 2014 Loop 7: This is loop 7 Loop 8: - break - EGR 115 Introduction to Computing for Engineers Slide 10 of 18 Loops and Vectorization Quiz • What is the result from executing the following? 1. for k = 8:10 fprintf('k = %g \n',k); end 2. for j = 8:-1:10 fprintf(' j = %g \n',j); end 3. k=8 k=9 k = 10 NOTHING!! for l = 1:10:10 fprintf(' l = %g \n',l); end i=1 4. for i = -10:3:-7 fprintf(' i = %g \n',i); end i = -10 i = -7 5. for m = [0 2 -3] fprintf(' m = %g \n',m); end m=0 m=2 m = -3 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 11 of 18 Loops and Vectorization Quiz • 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 = 0; for m = 1:5 x = x + m; end x=5 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); Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers m=1&x=2 m=3&x=5 m=4&x=9 End: x = 9 Slide 12 of 18 Loops and Vectorization An Example - Fitting noisy data to a line • Example: Fitting noisy data to a line Eqn. of a Line: y = m x + c Fit the following measured data to a line Noisy Data 12 10 noisy data 8 y Speed of a DC motor as the voltage was increased 6 4 Least Squares Fit? 2 0 0 1 2 3 4 5 6 7 8 9 10 11 x Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 13 of 18 Loops and Vectorization An Example - Fitting noisy data to a line • Problem: Given ‘n’ noisy data points [𝑥𝑖 ] and [𝑦𝑖 ], determine the “best” choice of 𝑚 and 𝑐 such that the error between 𝑦𝑖 = 𝑚𝑥𝑖 + 𝑐 and [𝑦𝑖 ] is minimized. e yi yˆi yi m xi c 2 e m m e c c y mx c 2 i i 2 2 yi mxi c xi 0 c xi m xi2 yi xi y mx c 2 i i 2 yi mxi c 0 nc m xi yi Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 14 of 18 Loops and Vectorization An Example - Fitting noisy data to a line nc m xi yi n n xxi icc yyii x x 2 mmx y i i i i c yi M x y m i i c xi m xi2 yi xi c n m x i m 1 xi yi xi2 xi yi xi yi nxi yi xi Wednesday 15 Oct 2014 2 nx 2 i c M 1 yi x y i i xi2 yi xi xi yi nx xi 2 i EGR 115 Introduction to Computing for Engineers 2 Slide 15 of 18 Loops and Vectorization An Example - Fitting noisy data to a line • 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]; c n m x i 1 xi yi xi2 xi yi 1 1.29 56.47 77.25 11 56.47 400.29 519.74 1.12 yˆi m xi c yˆi 1.29 xi 1.12 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 16 of 18 Loops and Vectorization An Example - Fitting noisy data to a line • Least Square fit of “noisy” data to a line Least-Square fit of Noisy Data to a Line 12 10 noisy data LS fit LS_line_fit.m y 8 6 4 2 0 0 1 2 3 4 5 6 7 8 9 10 11 x Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 17 of 18 Next Lecture • Nested Loops • Profiling Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 18 of 18