Electrical and Computer Engineering Department SUNY – New Paltz Computer Simulation “Lecture 8” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
Loops objectives code determinate loops with “for” code indeterminate loops with “while” SUNY-New Paltz
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
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
Combination of r out of n SUNY-New Paltz
Exercise Write a short MATLAB program that uses 2 for loops, to calculate the combination formula in the previous slide. SUNY-New Paltz
Exercise Write a short MATLAB program that uses ONLY 1 for loop, to calculate the combination formula in the previous slide. SUNY-New Paltz
MATLAB Code SUNY-New Paltz
Nested for’s N=4; M=4; L=4; for i=1:N for j=1:M for k=1:L a(:,:,1) = 3 4 5 6 4 5 6 7 5 6 7 8 6 7 8 9 a(:,:,2) = 7 8 9 10 a(:,:,3) = 8 9 10 11 a(:,:,4) = 9 10 11 12 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
Exercise 1- Manually create vector ‘a’ and use imshow(a) to display the grey view of it: 1 0 1 0 1 0 1 0 1 0 2- Manually create matrix ‘b’ and display: 1 0 1 0 10 1 0 1 0 3- Manually create matrix ‘c’ to display a checkered board. * Hint – use (‘InitialMagnification’, 1000 ) to magnify it! SUNY-New Paltz
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
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
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
Indeterminate repetition with while SUNY-New Paltz
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 1 1100.00 2 1210.00 3 1331.00 4 1464.10 5 1610.51 6 1771.56 7 1948.7 SUNY-New Paltz
Exercise Repeat the previous exercise, but use a while loop. SUNY-New Paltz
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
Solution clc clear all format long mysum = 0; n = 1; delta = 0.0000001; while 1/(n*n) > delta mysum = mysum + 1/(n*n); n = n +1; end disp(mysum) SUNY-New Paltz
break and continue Break : exits the while loop Continue : starts a new iteration SUNY-New Paltz
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
Exercise clc clear all format long mysum = 0; n = 1; delta = 0.0000000000000001; while 1 mysum = mysum + 1/(n*n); if 1/(n*n) < delta break end n = n +1; disp(mysum) SUNY-New Paltz
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
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