February 14, 2005 Lecture 9 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 9

Slides:



Advertisements
Similar presentations
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Advertisements

UNIT 3 PROBLEM SOLVING WITH LOOP AND CASE LOGIC STRUCTURE
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
ITEC113 Algorithms and Programming Techniques
Repeating Actions While and For Loops
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
MatLab – Palm Chapter 4, Part 3 For and While Loops
Do Loop The syntax of DO loop: DO variable = initial_value, final_value[, increment] [statements] END DO Example: PROGRAM LINES ! Illustration of DO-loops.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Chapter 4 MATLAB Programming Logical Structures Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
February 21, 2005 Lecture 10 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 10
Control Structures Session 03 Mata kuliah: M0874 – Programming II Tahun: 2010.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 6.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
ENG 1181 College of Engineering Engineering Education Innovation Center MAT – Conditional Statements Topics: 1.Conditional statements if-end if-else-end.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
ITEC113 Algorithms and Programming Techniques
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Introduction to Engineering MATLAB - 13 Agenda Conditional statements  If – end  If – else – end  if – elseif – else - end.
ENG 1181 College of Engineering Engineering Education Innovation Center P. 1 MAT - Conditional Statements Topics Covered: 1. if based conditional statements.
Chapter 7 Problem Solving with Loops
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
1 Conditional Statements + Loops ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
Program design Program Design Process has 2 phases:
Computer Application in Engineering Design
Python: Control Structures
ALGORITHMS AND FLOWCHARTS
ECE 1304 Introduction to Electrical and Computer Engineering
Chapter 4 MATLAB Programming
JavaScript: Control Statements I
CHAPTER 5A Loop Structure
CS1371 Introduction to Computing for Engineers
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
ALGORITHMS & FLOWCHARTING II
Ch 7: JavaScript Control Statements I.
INC 161 , CPE 100 Computer Programming
Looping.
Chapter 5 Repetition.
Repetition and Loop Statements
Structured Program Development in C
MSIS 655 Advanced Business Applications Programming
Types of Flow of Control
MatLab – Palm Chapter 4, Part 2 The if and switch structure
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Loop Construct.
Repetition (While Loop) LAB 9
REPETITION Why Repetition?
Presentation transcript:

February 14, 2005 Lecture 9 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 9

February 14, 2005 Lecture 9 - By Paul Lin 2 Lecture 9: MATLAB Program Design & Flow Control II 9-1 Control Flow – if, else, elseif, endif, else, elseif, end switch, case, otherwiseswitch, case, otherwise forfor whilewhile break, continuebreak, continue 9-2 Loop Control forfor whilewhile break, continuebreak, continue 9-3 Conditional Statements if, else, elseif, endif, else, elseif, end

February 14, 2005 Lecture 9 - By Paul Lin Control Flow MATLAB Control Flow (keywords) if, else, elseif, endif, else, elseif, end switch, case, otherwiseswitch, case, otherwise forfor whilewhile break, continuebreak, continue Specifying conditions and selections Specifying conditions and selections If, else, elseif, endIf, else, elseif, end switch-case statementsswitch-case statements Repeating a set of commands or functions Repeating a set of commands or functions ForFor whilewhile

February 14, 2005 Lecture 9 - By Paul Lin Loop Control FOR Loop FOR Repeat statements a specific number of times. FOR Repeat statements a specific number of times. The general form of a FOR statement is: The general form of a FOR statement is: FOR variable = n:s:m statement,..., statement END where variable is the loop counter, n is the initial value, s is the loop counter increment or decrement size, m is the final value where variable is the loop counter, n is the initial value, s is the loop counter increment or decrement size, m is the final value Example: Example: for n = 1:10 for n = 1:10 statement 1 statement 2 statement statement n statement n end end

February 14, 2005 Lecture 9 - By Paul Lin Loop Control Example 9-1: Nested For Loops % multiplytable.m N = 9; A = zeros(N); for row = 1:N, for col = 1:N, for col = 1:N, A(row, col) = A(row, col) = row * col; end endenddisp(A)

February 14, 2005 Lecture 9 - By Paul Lin Loop Control Example 9-2: While loop %whileloop.m n = 1; while n < 6 disp(n) disp(n) n = n + 1; n = n + 1;end Output: WHILE Loop - WHILE Repeat statements an indefinite number of times. The general form of a WHILE statement is: WHILE expression statements statementsEND

February 14, 2005 Lecture 9 - By Paul Lin Loop Control Terminate Loop or Exit Conditional Statements Break – stop execution of loopBreak – stop execution of loop Continue – Exit the inner loop and continuing on outer loopContinue – Exit the inner loop and continuing on outer loop Example 9-3 %break_continue.m k = 1; while k < 10 x = 10 - k^2; x = 10 - k^2; if x < 0 if x < 0 break break end end y = sqrt(x) y = sqrt(x) k = k + 1; k = k + 1;end fprintf('The %d time in the loop -> x less than zero = %d\n', k, x); %The 4th time in the loop -> x %less than zero = -6

February 14, 2005 Lecture 9 - By Paul Lin Loop Control Continue Statement Continue – Pass control to the next iteration of the FOR or WHILE loopContinue – Pass control to the next iteration of the FOR or WHILE loop Example 9-4 %continue_ex.m for k=1:6 if k == 4; if k == 4; continue continue end end fprintf('k = %d\n', k); fprintf('k = %d\n', k);end disp('End of loop') Output: k = 1 k = 2 k = 3 k = 5 k = 6 End of loop

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Three Forms of if Statements Three Forms of if Statements if-endif-end if-else-endif-else-end if-elseif-else-endif-elseif-else-end IF- END format IF- END format Single statementSingle statement if condition if condition statement statementend Multiple statementsMultiple statements if condition Statement_1; Statement_2; Statement_2; …. …. Statement_n; Statement_n;end

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-5: compute only if x ≥ 0. %inf_cond1.m x = input('Enter a number: ') y_complex = sqrt(x) if x >= 0 y_real = sqrt(x) y_real = sqrt(x)end % x = 2 % y_complex = % y_real = % x = -2 % y_complex = i

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-6: A Grade Calculation Program – Problem Statement Example 9-6: A Grade Calculation Program – Problem Statement Compute average grade of three exams (input from keyboard), then calculate and print a letter grade (on the screen) based on the following policy: Compute average grade of three exams (input from keyboard), then calculate and print a letter grade (on the screen) based on the following policy: A grade: Average >= 90 B grade: 80 <= Avg < 90 C grade: 70 <= Avg <80 D grade: 60 <= Avg < 70 F grade: Avg < 60

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-6: A Grade Calculation Program – using Flowchart Example 9-6: A Grade Calculation Program – using Flowchart

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-6: A Grade Calculation Program – Solution Example 9-6: A Grade Calculation Program – Solution%grade_prog.m t1 = input('Enter Test 1 Score: ') t2 = input('Enter Test 2 Score: ') t3 = input('Enter Test 3 Score: ') % Grade calculation avg = (t1 + t2 + t3)/3; if avg >= 90 Grade = 'A'; Grade = 'A';end if (avg >= 80) & (avg = 80) & (avg < 90) Grade = 'B'; Grade = 'B';end if (avg >= 70) & (avg = 70) & (avg < 80) Grade = 'C'; Grade = 'C';end if (avg >= 60) & (avg = 60) & (avg < 70) Grade = 'D'; Grade = 'D';end if avg < 60 Grade = 'F'; Grade = 'F';end fprintf('Your average score is %g.\n', avg); fprintf('Your grade grade is %c.\n', Grade);

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements IF-ELSEIF-ELSE-END if expression statements statements elseif expression statements statementselse end if expression statements_1 statements_1 elseif elseif statements_2 statements_2 elseif elseif statements_3 statements_3 else else statements_4 statements_4 end end IF-ELSE-END Construct: if expression statements_1 statements_1else statements_2 statements_2end

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-7: A Full Wave Rectifier

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-7: A Full Wave Rectifier Simulation using IF-END and FOR loop. Pusedocode Transformer voltage ratio 110/10 volts Transformer voltage ratio 110/10 volts e1 = 10*sin(2*pi*f*t); f = 60Hz e1 = 10*sin(2*pi*f*t); f = 60Hz Rectifying signal e1 Rectifying signal e1 If e1 < 0 e1 = -e1; e1 = -e1;end Output – Full-wave rectified voltage (plot) Output – Full-wave rectified voltage (plot)

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-7: Solution %full_rec.m % Primary side voltage Vp = 110; % secondary side voltage Vs = 10; % Frequency is 60 Hz f = 60; % Period is ms T = 1/f; dt = 10E-3*T; % For showing 2 cycles t = 0:dt:T; e1 =Vs*sin(2*pi*f*t); len = length(e1); % Rectifier operation for n = 1: len, if e1(n) >= 0 if e1(n) >= 0 e2(n) = e1(n); e2(n) = e1(n); else else e2(n) = -e1(n); e2(n) = -e1(n); end endend % continue on next % slide

February 14, 2005 Lecture 9 - By Paul Lin Conditional Statements Example 9-7: Solution (cont.) %full_rec.m figure(1), plot(t, e2, 'r', t, e1,'b--'), grid on title('Full Wave Rectifier') xlabel('Time in sec') ylabel('Volts') figure(2), subplot(2,1,1), plot(t, e1), grid on title('Input signal') xlabel('Time in sec') ylabel('Volts') figure(2), subplot(2,1,2), plot(t, e2), grid on title('Full Wave Rectified Signal') xlabel('Time in sec') ylabel('Volts')

February 14, 2005 Lecture 9 - By Paul Lin 19 Summary Control Flow Control Flow Loop Control Loop Control forfor whilewhile break, continuebreak, continue Conditional Statements Conditional Statements if, else, elseif, endif, else, elseif, end Next - Multiple Decision using switch, case, whatever, end Next - Multiple Decision using switch, case, whatever, end And More applications And More applications

February 14, 2005 Lecture 9 - By Paul Lin 20 Question? Answers