Presentation is loading. Please wait.

Presentation is loading. Please wait.

Electrical and Computer Engineering Department SUNY – New Paltz

Similar presentations


Presentation on theme: "Electrical and Computer Engineering Department SUNY – New Paltz"— Presentation transcript:

1 Electrical and Computer Engineering Department SUNY – New Paltz
Computer Simulation “Lecture 8” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz

2 Loops objectives code determinate loops with “for”
code indeterminate loops with “while” SUNY-New Paltz

3 Exercise Write a MATLAB program that prompts the user for a number, n. Your program should use a “for” loop to calculate n! SUNY-New Paltz

4 Determinate repetition with for
There are 4 balls: R, B, G and Y. How many combinations of pairs of 2 balls are there? (R,B), (R,G), (R,Y), (B,G), (B,Y), (G,Y) SUNY-New Paltz

5 Combination of r out of n
SUNY-New Paltz

6 Exercise Write a short MATLAB program that uses 2 for loops, to calculate the combination formula in the previous slide. SUNY-New Paltz

7 Exercise Write a short MATLAB program that uses ONLY 1 for loop, to calculate the combination formula in the previous slide. SUNY-New Paltz

8 MATLAB Code SUNY-New Paltz

9 Nested for’s N=4; M=4; L=4; for i=1:N for j=1:M for k=1:L
a(:,:,1) = a(:,:,2) = a(:,:,3) = a(:,:,4) = N=4; M=4; L=4; for i=1:N for j=1:M for k=1:L a(i,j,k)=i+j+k; end SUNY-New Paltz

10 Exercise 1- Manually create vector ‘a’ and use imshow(a) to display the grey view of it: 2- Manually create matrix ‘b’ and display: 3- Manually create matrix ‘c’ to display a checkered board. * Hint – use (‘InitialMagnification’, 1000 ) to magnify it! SUNY-New Paltz

11 Exercise 1- Write a MATLAB program (using for loops) to create vector ‘a’ in last exercise. 2- Write a MATLAB program (using for loops) to create matrix ‘b’ in last exercise. 3- Write a MATLAB program (using for loops) to create matrix ‘c’ in last exercise. SUNY-New Paltz

12 How can we terminate a for loop?
Use “break” inside the loop and it will terminate the loop! for k = 1:100 disp(k) if( k == 50) break; end for k = 1:100 disp(k) end SUNY-New Paltz

13 Exercise Prompt the user to enter a number. If the number is positive then print the square root of it and ask for another number. Otherwise, exit the loop. Hint: initially use a loop that iterates for 20 times. SUNY-New Paltz

14 Indeterminate repetition with while
SUNY-New Paltz

15 Doubling time of an investment
We would like to know how long it takes for the investment to double: 1. Initialize balance, year, interest rate 2. Display headings 3. Repeat Update balance according to interest rate Display year, balance until balance exceeds twice original balance 4. Stop a = 1000; r = 0.1; bal = a; year = 0; disp( ’Year Balance’ ) while bal < 2 * a bal = bal + r * bal; year = year + 1; disp( [year bal] ) End Year Balance SUNY-New Paltz

16 Exercise Repeat the previous exercise, but use a while loop.
SUNY-New Paltz

17 Exercise Find the sum of the series s=1 +1/(2*2) + 1/(3*3) + … for a 1000 terms. Use a for loop. Use a while loop to do the above and stop when a new term is less than .01 SUNY-New Paltz

18 Solution clc clear all format long mysum = 0; n = 1;
delta = ; while 1/(n*n) > delta mysum = mysum + 1/(n*n); n = n +1; end disp(mysum) SUNY-New Paltz

19 break and continue Break : exits the while loop
Continue : starts a new iteration SUNY-New Paltz

20 exercise In the previous example, write a while loop that runs for ever, i.e. while(1), then break the loop when the condition takes place. SUNY-New Paltz

21 Exercise clc clear all format long mysum = 0; n = 1;
delta = ; while 1 mysum = mysum + 1/(n*n); if 1/(n*n) < delta break end n = n +1; disp(mysum) SUNY-New Paltz

22 Menus k = menu( ’Click on your option’, ’Do this’, ’Do that’, ’Quit’ ); k = 0; while k ~= 3 k = menu( ’Click on your option’, ’Do this’, ... ’Do that’, ’Quit’ ); if k == 1 disp( ’Do this ... press any key to continue ...’ ) pause elseif k == 2 disp( ’Do that ... press any key to continue ...’ ) end SUNY-New Paltz

23 Exercise Set up a menu that prompts the user to select a waveform from the following selections: 1- sin(x) 2- cos(x) After the user makes a selection your program should show the graph ([0 – 2pi] in a new window. SUNY-New Paltz


Download ppt "Electrical and Computer Engineering Department SUNY – New Paltz"

Similar presentations


Ads by Google