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

Slides:



Advertisements
Similar presentations
MATLAB Examples. CS 1112 MATLAB Examples Find the number of positive numbers in a vector x = input( 'Enter a vector: ' ); count = 0; for ii = 1:length(x),
Advertisements

Week 4: Control Structures - Repetition
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
© 2006 Baylor University EGR 1301 Slide 1 Lecture 18 Statistics Approximate Running Time - 30 minutes Distance Learning / Online Instructional Presentation.
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
Looping While-continue.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
Arrays. Introduction This chapter serves as an introduction to the important topic of data structures. Arrays are data structures consisting of related.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: Φ Fax:
UFCEKS-20-2Multimedia Authoring Times Table Quiz.
CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions.
EGR 115 Introduction to Computing for Engineers Formatted File Input / Output Wednesday 12 Nov 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Introduction to Computer Programming Wednesday 27 Aug 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 4: Intro to Plotting Wednesday 10 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Matlab Programming for Engineers
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Introduction to Matlab
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
Loops and Simple Functions CS303E: Elements of Computers and Programming.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 5: Applications – Vector Math Friday 12 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers 2D Plotting – Part II Friday 19 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 1: Variables & Arrays Wednesday 03 Sept 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Complex Numbers & 3D Plots – Part 1 Wednesday 05 Nov 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Improving Matlab Performance CS1114
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 3 Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
REPETITION CONTROL STRUCTURE
EGR 115 Introduction to Computing for Engineers
CS1371 Introduction to Computing for Engineers
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Outline Altering flow of control Boolean expressions
Loop Statements & Vectorizing Code
Looping III (do … while statement)
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
Chapter 4: Repetition Structures: Looping
Loop Statements & Vectorizing Code
Programming Fundamental
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

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

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

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

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

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 = a = a = Slide 6 of 18

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: x(i) = i; end Non-Preallocated Array Case x=1: ; Preallocated Array Case Elapsed time = sec Elapsed time = sec 84 Times FASTER!! 84 Times FASTER!! Slide 7 of 18

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 = ; 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 = ; t = 0:1/N:1; a = sin(3*t).*t.^2; Vectorized Array Case Elapsed time = secElapsed time = sec 198 Times FASTER!! 198 Times FASTER!! Slide 8 of 18

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

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

Loops and Vectorization Quiz Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers What is the result from executing the following? 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

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? 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

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

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

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

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

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

Next Lecture Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers Nested Loops Profiling Slide 18 of 18