egr 115 introduction to computing for engineers loops and vectorization – part 2 wednesday 15 oct...

18
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Upload: sibyl-hamilton

Post on 18-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

EGR 115 Introduction to Computing for Engineers

Loops and Vectorization – Part 2

Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Page 2: 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

Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

• Testing the Random Number Generator “rand”• Vectorization

Slide 2 of 18

Page 3: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

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

Page 4: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

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.m courtesy of Hilken, Tanner R. <[email protected]>

Slide 4 of 18

Page 5: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Testing the Random Num Generator “rand”

Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

• Results obtained for N = 500 points

1 2 3 4 5 6 7 8 9 100

2

4

6

8

10

12Random Number Generator Test: N = 500 points

Fre

eque

ncy

of O

ccur

ance

(%

)

Value Generated

10%

12%

9%

11%

11%

11%

9%

10%

9%

8%

Random Number Generator Test: N = 500 points

12

3

4

56

7

8

910

Slide 5 of 18

Page 6: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationPreallocation 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 = 1 2 3 4

a = 1 2 3 4

a = 1 4 3 4

a = 1 4 9 4

a = 1 4 9 16

a = 1 4 9 16 25

a = 1 4 9 16 25 36

Slide 6 of 18

Page 7: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationPreallocation 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!!

Slide 7 of 18

Page 8: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationVectorization

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 sec Elapsed time = 0.0063078 sec

198 Times FASTER!!

2 6Sin 3 , 0 :10 :1a t t t

Slide 8 of 18

Page 9: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationThe 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

Page 10: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationThe 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 1This is loop 2This is loop 3This is loop 6This is loop 7

Output:

Slide 10 of 18

Page 11: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationQuiz

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

Page 12: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationQuiz

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);endfprintf(' End: x = %g \n', x);

m = 1 & x = 2 m = 3 & x = 5 m = 4 & x = 9 End: x = 9

Slide 12 of 18

Page 13: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationAn 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

0 1 2 3 4 5 6 7 8 9 10 110

2

4

6

8

10

12

Noisy Data

x

y

noisy data

Least Squares Fit?

Speed of a DC motor as the voltage was increased

Slide 13 of 18

Page 14: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationAn Example - Fitting noisy data to a line

Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

• Problem: Given ‘n’ noisy data points [] and [], determine the “best”

choice of and such that the error between and [] is minimized.

2ˆi ie y y 2i iy m x c

2i i

ey mx c

m m

2 0i i iy mx c x 2i i i ic x m x y x

2i i

ey mx c

c c

2 0i iy mx c

i inc m x y Slide 14 of 18

Page 15: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationAn Example - Fitting noisy data to a line

Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

2i i i ic x m x y x i inc m x y i in x c y

m

2i i

i i i i

n x yc

x x x ym

1

2i i

i i i i

n x yc

x x x ym

2

22

i i i i i

i i

x y x x yc

n x x

2 2

i i i i

i i

x y n x ym

x n x

i

i i

ycM

x ym

1 i

i i

yM

x y

Slide 15 of 18

Page 16: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationAn 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];

111 56.47 77.25

56.47 400.29 519.74

1

2i i

i i i i

n x yc

x x x ym

1.29

1.12

ˆi iy m x c

ˆ 1.29 1.12i iy x

Slide 16 of 18

Page 17: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Loops and VectorizationAn 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

0 1 2 3 4 5 6 7 8 9 10 110

2

4

6

8

10

12

Least-Square fit of Noisy Data to a Line

x

y

noisy data

LS fitLS_line_fit.m

Slide 17 of 18

Page 18: EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

Next Lecture

Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers

• Nested Loops• Profiling

Slide 18 of 18