EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.

Slides:



Advertisements
Similar presentations
Chapter 3 IF & SELECT. Control Constructs: Branches Definitions: Code: statements or expressions in a program Block: a group of codes Branching: selecting.
Advertisements

Lecture 5.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Lecture 4 Structure plan Program design process Program design process Basic elements of code: Basic elements of code: –Input (assignment of variables)
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
ENGG 1801 Engineering Computing MATLAB Lecture 2: Background for Week 4 Tutorial Simple Programming in MATLAB.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Precedence Parentheses Arithemetic ^ * / + - (exception logical not ~ ) Relational > =
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Lecture 13 Decision structures
C++ for Engineers and Scientists Third Edition
Chapter 8 Branching Statements and Program Design.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
MATLAB and SimulinkLecture 11 To days Outline  Introduction  MATLAB Desktop  Basic Features  Branching Statements  Loops  Script file / Commando.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0.
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.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
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)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
1 Example: Solution of Quadratic Equations We want to solve for real values of x, for given values of a, b, and c. The value of x can be determined from.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
FT228/3 Web Development Error processing. Introduction READ Chapter 9 of Java Server Pages from O’reilly 2 nd Edition Need to be able to 1) Diagnose and.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
MATLAB 及其工程应用 MATLAB and It’s Engineering Application 主讲教师: 胡章芳
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.
Review if imag(x) > 0 fprintf('Sorry, but I don''t understand complex numbers.\n'); return end if x < 10 % leave this out, type the next line first, and.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers.
1 CS1371 Introduction to Computing for Engineers Control Statements 9/4/2003.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
General Computer Science for Engineers CISC 106 Lecture 13 - Midterm Review James Atlas Computer and Information Sciences 10/02/2009.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 Structured Programming EEN170 Programming in MATLAB.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Matlab Programming for Engineers
L2. Basics Variables and Expressions Assignment Statements
Selection Statements by Ahmet Sacan
ENGG 1801 Engineering Computing
Scripts & Functions Scripts and functions are contained in .m-files
MATLAB DENC 2533 ECADD LAB 9.
Lecture 3 MATLAB programming (1)
تصمیم‌گیری و کنترل روند، استفاده از حلقه‌ها و دستورات شرطی در متلب
CS1100 Computational Engineering
Introduction to Matlab LAB 4
Lecture 4 Structure plan
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.
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers

Lecture Outline Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers More Branching More Debugging Slide 2 of 16

Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers The “switch” statement is another branching construct  Typically used to test if an expression is equal to one of an enumerated set of choices.  General form of the structure: Slide 3 of 16 switch variable case {value1, …, valueL} some code; case {valueM, …,valueN} some more code;  otherwise even more code; end switch variable case {value1, …, valueL} some code; case {valueM, …,valueN} some more code;  otherwise even more code; end variable = value1, value2, …, or valueN } Code Block #1 } Code Block #2 

Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example:  Output the number of days in a user selected month (NOT a leap year): o Month = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, or 12 o Months with 30 days = 9, 4, 6, or 11 o Months with 31 days = 1, 3, 5, 7, 8, 10, or 12 o Months with 28 days = 2 Slide 4 of 16 switch month case {2} disp('This month has 28 days'); case {9, 4, 6, 11} disp('This month has 30 days'); case {1, 3, 5, 7, 8, 10, 12} disp('This month has 31 days'); otherwise disp('Invalid Month'); end GOOD PRACTICE: include otherwise statement to trap illegal values of variable NOTE: Only one case will ever be executed

Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Comparison between if and switch:  Switch better for enumerated variable  If better for testing variables over continuous ranges Slide 5 of 16 switch month case {2} disp('This month has 28 days'); case {9, 4, 6, 11} disp('This month has 30 days'); case {1, 3, 5, 7, 8, 10, 12} disp('This month has 31 days'); otherwise disp('Invalid Month'); end Code continues on next line if month == 2 disp('This month has 28 days'); elseif (month == 9 | month == 4 |... month == 6 | month == 11) disp('This month has 30 days'); elseif (month == 1 | month == 3 |... month == 5 | month == 7 |... month == 8 | month == 10|... month == 12) disp('This month has 31 days'); else disp('Invalid Month'); end

Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers In Class Programming Exercise:  Prompt the user for an integer from 1 to 10  Respond by telling the user whether it is odd or even number. 1. If your last name begins with A to L (inclusive)  Use the switch construct 2. If your last name begins with M to Z (inclusive)  Use the if construct Slide 6 of 16

Branching & Program Design Branching – The Try/Catch Construct Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Typically, when MATLAB encounters a run-time error in your code the program aborts The Try/Catch construct allows you to “trap” the error and continue with execution Slide 7 of 16 try some statements; catch other statements; end } tries to execute these statements } execute these if an error above

Branching & Program Design Branching – The Try/Catch Construct Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example:  Given a row vector a with 10-elements display a(index) Slide 8 of 16 a = [ ]; % A row vector with 10-elements try% Request an index and display a(index) index = input('Which element would you like to display? '); disp(['a(',num2str(index),') = ', num2str(a(index))]); catch% Run this code if an error occurs above disp('Indexing type Run-Time Error: 1 <= index <= 10 '); disp([' You entered index = ', num2str(index)]) end Try_catch_example.m

Branching & Program Design More on Debugging Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers More on Debugging:  Debug the program: o There are ~ 6 different errors Slide 9 of 16 more_debugging_w_errors.m

Syntax Error: Should be ‘ input ’ NOT ‘ Input ’ Syntax Error: Should be ‘ input ’ NOT ‘ Input ’ Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 10 of 16 … % Enter the real coefficients of a quadratic polynomial disp('Enter the coeffs of the quadratic: a x^2 + b x + c') a = Input('Enter the coeff. of x^2 term: a = '); b = Input('Enter the coeff. of x^1 term: b = '); c = Input('Enter the coeff. of x^0 term: c = '); % Compute the Discriminant: Delta = b^2 - 4ac Delta = b^2 - 4*a*c … Syntax Error: Missing a semicolon ‘;’ Syntax Error: Missing a semicolon ‘;’ ; ;

Arithmetic Run-Time Error: Should be ‘/ (2*a) ’ NOT ‘ /0*a ’ Arithmetic Run-Time Error: Should be ‘/ (2*a) ’ NOT ‘ /0*a ’ Logic Error: Should be ‘ x2 ’ NOT ‘ x1 ’ Logic Error: Should be ‘ x2 ’ NOT ‘ x1 ’ Logic Error: Should be ‘ > ’ NOT ‘ < ’ Logic Error: Should be ‘ > ’ NOT ‘ < ’ Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 11 of 16 … % Solve for the roots of the quadratic polynomial if Delta < 0 % Case of real and distinct roots x1 = (-b + sqrt(Delta)) / 0*a; x2 = (-b - sqrt(Delta)) / 0*a; fprintf(' Real and distinct roots: x1 = %4.2f x2 = %4.2f\n', x1, x2) elseif Delta == 0 % Case of real repeated roots x1 = -b / (2*a); x2 = x1; fprintf(' Real and repeated roots: x1 = %4.2f x2 = %4.2f\n', x1, x2) else % Case of complex roots x1 = (-b + sqrt(Delta)) / (2*a); x2 = (-b - sqrt(Delta)) / (2*a); fprintf(' Imag roots: x1 = %g + j%g and x2 = %g + j%g \n',... real(x1), imag(x1), real(x1), imag(x1)); end …

Assignment Run-Time Error: Should be ‘ num2str(x2) ’ NOT ‘ (x2) ’ Assignment Run-Time Error: Should be ‘ num2str(x2) ’ NOT ‘ (x2) ’ Branching & Program Design Branching – The Switch Statement Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers Slide 12 of 16 … % Plot the two roots plot(real(x1), imag(x1),'rx', real(x2), imag(x2),'bo', 'MarkerSize',16, 'LineWidth',5) title(['Roots of polynomial: f(x) = ', poly, ' = 0'], 'FontSize', 16) xlabel('Real Axis'); ylabel('Imag Axis') legend(['x_1=',num2str(x1)],['x_2=', (x2)]) grid

Next Lecture Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers An Application Example Slide 13 of 16