Download presentation
Presentation is loading. Please wait.
Published byGerald Chandler Modified over 8 years ago
1
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers
2
Lecture Outline Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Loops and Vectorization Slide 2 of 15
3
Loops and Vectorization Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Computers are very good at performing repetitive tasks efficiently For example – Computer Animation o Calculate the RGB values of every pixel in a 1080p image every 1/30 of a second (i.e., HDTV) width*height*frame rate*colors*bits/pixel (16/9*1080)*1080*30*3*24 = 4,478,976,000 bits/sec = 4.5 billion bits / sec MATLAB offers two constructs for looping o The for loop, and o The while loop Slide 3 of 15
4
Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers For Loops: for k = start:step:stop some block of code end … start = initial value step = incremental value stop = terminating value start = initial value step = incremental value stop = terminating value Slide 4 of 15
5
Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: Code: Output: my_sum = 0; for k=0:2:11 my_sum = my_sum + k; fprintf(' k = %2.0f and sum = %2.0f \n', k, my_sum); end k = 0 and sum = 0 k = 2 and sum = 2 k = 4 and sum = 6 k = 6 and sum = 12 k = 8 and sum = 20 k = 10 and sum = 30 start = 0 step = 2 stop = 11 start = 0 step = 2 stop = 11 Further examples for index variable: Step k with increments of 1 (indexing): for k = 1 : 10 (default step is 1) Step k with increments of -0.1 (indexing):: for k = 1.0 : -0.1 : 0.0 Step k with values 3, 2, and 7 (direct access): for k = [3, 2, 7] Further examples for index variable: Step k with increments of 1 (indexing): for k = 1 : 10 (default step is 1) Step k with increments of -0.1 (indexing):: for k = 1.0 : -0.1 : 0.0 Step k with values 3, 2, and 7 (direct access): for k = [3, 2, 7] Slide 5 of 15
6
Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Another Example: Will the loop terminate early? for k = 1:5 disp(k); if k == 3 k = 6; end disp(k); end CAVEAT: this command DO NOT change the next value of k in the loop 1111 output 2222 3636 4444 5555 Slide 6 of 15
7
Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: Develop a program to compute N! = 1 * 2 * 3 * … *(N-1)*N o Use a for loop Pseudo Code: o Prompt user for a positive integer N o Initialize N_Factorial o Loop from 1 to N and update N_Factorial o Display the result of N! Compare your result with MATLAB function “factorial” >> factorial(N) Slide 7 of 15
8
Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Solution: N = 7; % or N = input(' Input N: ') N_Factorial = 1; for i = 1:N N_Factorial = N_Factorial * i; end disp([num2str(N),'! = ', num2str(N_Factorial)]); >> 7! = 5040 Slide 8 of 15
9
Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers While Loops: while (logical expression) some block of code end … The Logical Expression MUST eventually go false in order to exit the loop!!! Slide 9 of 15
10
Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: Code: Output: my_sum = 0; k = 0; while k <= 10 my_sum = my_sum + k; fprintf(' k = %2.0f and sum = %2.0f \n', k, my_sum); k = k + 2; end k = 0 and sum = 0 k = 2 and sum = 2 k = 4 and sum = 6 k = 6 and sum = 12 k = 8 and sum = 20 k = 10 and sum = 30 Slide 10 of 15
11
Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Another Example: Will the loop terminate early? k=1; while k <= 5 disp(k); if k == 3 k = 6; end disp(k); k = k +1; end This command DOES change the next value of k in the loop 1111 output 2222 3636 Slide 11 of 15
12
Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example: Develop a program to compute N! = 1 * 2 * 3 * … *(N-1)*N o Use a while loop Pseudo Code: o Prompt user for a positive integer N o Initialize N_Factorial o Loop from 1 to N and update N_Factorial o Display the result of N! Compare your result with MATLAB function “factorial” >> factorial(N) Slide 12 of 15
13
Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Solution: N = 7; N_Factorial = 1; i = 1; while i <= N N_Factorial = N_Factorial * i; i = i + 1; end disp([num2str(N),'! = ', num2str(N_Factorial)]); >> 7! = 5040 Slide 13 of 15
14
Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Revisit the guessing game num = rand; % Generate a random number betw 0 &1 num = ceil(10*num); % Convert to an integer betw 1 & 10 % Prompt the user to guess the number guess = input('Guess a Integer from 1 to 10: '); while guess ~= num % Test the guess and issue a response if guess < num disp('Your Guess is Too Low!! Guess again'); else % guess > num disp('Your Guess is Too High!! Guess again'); end guess = input('Guess a Integer from 1 to 10: '); end disp('Your Guess is Correct!!'); Looping_Examples.m Slide 14 of 15
15
Next Lecture Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Vectorization Slide 15 of 15
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.