Electrical and Computer Engineering Department SUNY – New Paltz

Slides:



Advertisements
Similar presentations
Lab5 (Signal & System) Instructor: Anan Osothsilp Date: 20 Feb 07 Due Date 09 March 07.
Advertisements

BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
CMPS 1371 Introduction to Computing for Engineers
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
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.
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.
For Loops 1 ENGR 1181 MATLAB 8. For Loops and Looped Programming in Real Life Looping within programs has long been a useful tool for completing mundane.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
SUNY-New Paltz Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz “Lecture 6”
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
CMPS 1371 Introduction to Computing for Engineers MatLab.
Review for Exam2 Key Ideas 1. Key Ideas: Boolean Operators (2 > 3) || (3 < 29.3) A.True B.False C.Impossible to determine (22 > 3) && (3 > 29.3) A.True.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Scientific Computing Introduction to Matlab Programming.
Even more problems.. Mean (average) I need a program that calculates the average of student test scores. I need a program that calculates the average.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matlab Programming for Engineers
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
ENG 1181 College of Engineering Engineering Education Innovation Center 1 Script File Input – Output : Chapter 4 PLEASE HAVE STUDENTS START MATLAB NOW.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
CSE123 - Lecture 4 Structured Programming- Loops.
Computer Programming -1-
Loops & More 1.Conditionals (review) 2.Running totals and Running products 3.the for loop, examples 4.Nesting for loops, examples 5.Common errors 1.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
MATLAB – More Script Files
EEE 161 Applied Electromagnetics
Release Numbers MATLAB is updated regularly
REPETITION CONTROL STRUCTURE
ECE Application Programming
Computer Application in Engineering Design
Solving Simultaneous Equations using Matlab
Repetition Structures Chapter 9
CS1371 Introduction to Computing for Engineers
Computer Simulation Lab
Topics Introduction to Repetition Structures
Introduction to MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Lecture 4B More Repetition Richard Gesick
Control Structure Senior Lecturer
Control Structure Senior Lecturer
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
MATH 493 Introduction to MATLAB
MATLAB – Basic For Loops
While Loop Design ENGI 1020 Fall 2018.
Introduction to Problem Solving and Control Statements
CSC115 Introduction to Computer Programming
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
Islamic University of Gaza
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Using Script Files and Managing Data
Topics Introduction to Repetition Structures
EECE.2160 ECE Application Programming
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Week 7: Computer Tools for Problem Solving and Critical Thinking
Electrical and Computer Engineering Department SUNY – New Paltz
CS Problem Solving and Object Oriented Programming Spring 2019
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Computer Simulation Lab
Computer Simulation Lab
Electrical and Computer Engineering Department SUNY – New Paltz
Electrical and Computer Engineering Department SUNY – New Paltz
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

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