Beginning Programming for Engineers Matlab Conditional Computation.

Slides:



Advertisements
Similar presentations
Chapter 5: Control Structures II (Repetition)
Advertisements

CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Microsoft® Small Basic
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
CMPS 1371 Introduction to Computing for Engineers
CS0004: Introduction to Programming Repetition – Do Loops.
 Control structures  Algorithm & flowchart  If statements  While statements.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Chapter 4 Repetitive Execution. 2 Types of Repetition There are two basic types of repetition: 1) Repetition controlled by a counter; The body of the.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
General Computer Science for Engineers CISC 106 Lecture 10 Roger Craig Computer and Information Sciences 3/06/2009.
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Loops For loop for n = [ ] code end While loop while a ~= 3 code end.
General Computer Science for Engineers CISC 106 Lecture 05 Dr. John Cavazos Computer and Information Sciences 2/20/2009.
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
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.
Adapted from slides by Marie desJardins
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Fall Week 3 CSCI-141 Scott C. Johnson.  Say we want to draw the following figure ◦ How would we go about doing this?
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Basic Control Structures
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
 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.
Overview Go over parts of quiz? Another iteration structure for loop.
Xiaojuan Cai Computational Thinking 1 Lecture 8 Loop Structure Xiaojuan Cai (蔡小娟) Fall, 2015.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
L6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Selection Using IF THEN ELSE CASE Introducing Loops.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Follow up from lab See Magic8Ball.java Issues that you ran into.
REPETITION CONTROL STRUCTURE
Sequence, Selection, Iteration The IF Statement
Python: Control Structures
Chapter 5: Repetition Structures
Ch 7: JavaScript Control Statements I.
Week 8 - Programming II Today – more features: Loop control
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Computer Science Core Concepts
Presentation transcript:

Beginning Programming for Engineers Matlab Conditional Computation

Learning goals for class 3 Understand how conditional computation can be used in an algorithm, and know how to express it using the if and else statements. Know how to use Matlab's relational and logical operators. Understand how iteration can be used in in an algorithm, and know how to express it using the while statement. Understand several algorithm design idioms based on iteration: counter variables, running sums, accumulating results, and related ideas.

Conditional execution (flowchart)

Conditional execution: The if statement Format is: if expression some statements elseif expression some statements else some statements end There can be more than one elseif stanza. Both elseif and else stanzas can be omitted. The expressions must return true or false.

Relational Operators Relational operators return either true or false. o True is represented by 1. o False is represented by 0. Relations can be further combined using the logical operators. Try vector forms: A = [ ]; A >= 3 A ~= 4 A(4) == 4 a equals b a == b a not equal ba ~= b a less than ba < b a less than or equal to b a <= b a greater than ba > b a greater than or equal to b a >= b

Logical operators Logical operators combine expressions that are either true or false. Examples: isreal(a) ~isreal(a) if (time ==0) && (dist == 0) speed = 0; else speed = dist / time end a and ba & b a && b a or ba | b a || b not a~a

Conditional execution: Basic hi-lo % Script to play a very basic hi-lo game. clear clc M = 100; % Maximum value % Pick a number. myNum = floor(1+M*rand()); % Get player's guess. yourNum = input('Enter your guess: '); % Display message if yourNum < myNum disp('Too low!'); elseif yourNum > myNum disp('Too high!'); else disp('That is it!'); end disp('My number was'); disp(myNum);

Design Technique: Break into cases Suppose you want to calculate the point of intersection of two lines represented as: y = m1*x + b1 y = m2*x + b2 Solve for x yields: x = (b2 - b1) / (m1 - m2) if m1 ~= m2 x = (b2 - b1) / (m1 - m2); y = m1*x + b1; else disp('Lines are parallel!'); end

Iteration with a test (flowchart)

Iteration with a test: The while statement Format is: while expression statements end The expression is evaluated; if true, the statements are executed and the test repeated... The expression should depend on something that will eventually be false.

Iteration: Better hi-lo % Script to play a better hi-lo game. clear clc M = 100; % Pick a number. myNum = floor(1+M*rand()); % Get player's guess. disp('Enter a guess between 1 and') disp(M) yourNum = input('Enter your guess: '); % Repeat until correctly guessed. while yourNum ~= myNum if yourNum < myNum disp('Too low!') else disp('Too high!') end yourNum = input('Next guess: '); end % Finish up disp('You got it!')

Design Technique: Counters Sometimes we need to build up results in pieces: running sums, running products, etc. % simple_counting.m: Counting. clear clc c = 1; while c <= 10 disp(c); c = c + 1; end The variable c is a counter.

Design Technique: Running sums function [t] = triangle(n) % TRIANGLE computes triangle numbers. i = 0; t = 0; while i <= n t = t + i; i = i+1; end return; end The variable t is a running sum. The variable i is another counter (loop variable, index variable, control variable).

Multiple running sums... function [f] = fib(n) % FIB computes fibonacci sequence f = 1; f1 = 0; f2 = 1; i = 2; while i <= n f = f1 + f2; f1 = f2; f2 = f; i = i+1; end return end

Design Technique: Accumulating vectors function [ft] = fib_table(c) % FIB_TABLE creates table of fibonacci's. ft = []; i = 1; while i <= c; ft = [ft fib(i)]; i = i+1; end return end