Introduction to Programming for Mechanical Engineers (ME 319)

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Conditional Statements Introduction to Computing Science and Programming I.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
MATLAB FUNDAMENTALS: INPUT/OUTPUT LOGIC CONTROL STRUCTURES HP 101 – MATLAB Wednesday, 9/24/2014
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
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.
October 3, 2005 Lecture 8 - By Paul Lin 1 CPET 190 Lecture 8 Problem Solving with MATLAB
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (4): Control Flow (Chapter 2)
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CMSC 104: Peter Olsen, Fall 99Lecture 9:1 Algorithms III Representing Algorithms with pseudo-code.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
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.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
“Operators” (i.e. “symbols”)
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Software Testing.
University of Central Florida COP 3330 Object Oriented Programming
Introduction to Programming for Mechanical Engineers (ME 319)
Chapter 2 - Introduction to C Programming
L – Modeling and Simulating Social Systems with MATLAB
Python: Control Structures
2.5 Another Java Application: Adding Integers
University of Central Florida COP 3330 Object Oriented Programming
Engineering Problem Solving with C++, Etter/Ingber
Introduction to Programming for Mechanical Engineers (ME 319)
Introduction to Programming for Mechanical Engineers (ME 319)
Programming Fundamentals
Chapter 2 - Introduction to C Programming
Expressions and Control Flow in JavaScript
MATLAB: Structures and File I/O
Introduction to MATLAB
Selection By Ramin && Taimoor
Conditional Statements
Selection CIS 40 – Introduction to Programming in Python
Chapter 2 - Introduction to C Programming
Use of Mathematics using Technology (Maltlab)
Lecture 2 Python Programming & Data Types
Chapter 2 - Introduction to C Programming
Vectorized Code, Logical Indexing
Chapter-3 Operators.
Selection Control Structure
Conditional statements
Chapter 2 - Introduction to C Programming
Selection Statements.
MATLAB Logical Expressions
Lecture 2 Python Programming & Data Types
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.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 2 - Introduction to C Programming
Chapter 3: Selection Structures: Making Decisions
Basic Concepts of Algorithm
Matlab Basics.
Chapter 3: Selection Structures: Making Decisions
Introduction to C Programming
Structural Program Development: If, If-Else
Presentation transcript:

Introduction to Programming for Mechanical Engineers (ME 319) Lecture 6.1

INTRODUCTION: Programming “The purpose of programming is to create a program that exhibits a certain Desired Behavior (customization). The process of writing source code often requires expertise in many different subjects, including knowledge of the application domain, specialized algorithms and formal logic.” wikipedia.org. Keywords: Algorithms, Logic, Desired Behavior. Desired Behavior : according to this, we didn’t start programming yet! Logic is reasoning Algorithm is a step-by-step procedure for calculations

Programming: an overview Flow of normal script and function files are fairly easy and simple. Normal flow is the flow that executes any command only once in the same order as they appear. Real programming involves the ability of creating a decision making algorithm. This decision making is responsible for steering the flow of the program. With Decision making, some command may be executed once, some may be skipped, others may not execute at all, and some may execute many times in a single program run. Decision making is built with the use of logical and relational operators.

‘Flow chart’ of an algorithm to solve the GCD of two numbers Mohammad Y. Saadeh

In logic design, the output is either 0 (false) or 1 (true) Relational Operators Relational operator Description < Less than > Greater than <= Less than or equal >= Greater than or equal == Equal to ~= Not equal In logic design, the output is either 0 (false) or 1 (true) >> a = [1 8 0 5 16]; >> b = [3 7 4 5 19]; >> c = a>=b % find the values in a that are greater or equal to values in b c = 0 1 0 1 0

& | ~ Logical Operators Built-in logical Functions: >> and (A,B) Name Description & AND (A & B) If both A AND B are true, the result is true (1). If one is not true, the result is false (0) | OR (A | B) If A OR B OR both are true, the result is true (1). If both are not true, the result is false (0) ~ NOT ( ~ A) Operates in one operand A. True if the operand is false (0), and false if the operand is true (≠ 0). Built-in logical Functions: >> and (A,B) >> or (A,B) >> not (A) >> a = [1 8 0 5 16]; >> b = [3 7 4 5 19]; >> a & b % both A AND B are true ans = 1 1 0 1 1 >> a | b % A OR B OR both are true 1 1 1 1 1 >> ~ a % NOT A, if true(≠ 0) result is false ans = % if false (A(i) = 0) results is true 0 0 1 0 0

1 Highest 2 3 4 5 6 7 8 Lowest Order of Precedence Precedence Operation 1 Highest Parenthesis ( ) 2 Exponentiation x^3 3 Logical NOT ~ 4 Multiplication, Division * / 5 Addition, Subtraction + - 6 Relational Operators > <= == ~= 7 Logical AND & 8 Lowest Logical OR |

Built-in logical and relational commands (functions) Description Example xor (A,B) Exclusive OR: true only if either A or B is true only. >> xor ([1 2 3],[0 0 4]) ans = 1 1 0 all (A) True only if all elements are true (non-zeros). False if At least one element is zero. >> all ([1 1 0 -3]) any (A) True if at least one element is non-zero. False if all elements are zeros. >> any ([0 0 0 -3]) 1 find (A) find (A >= c) Returns the indices of elements satisfying the condition provided. If no condition is provided, then elements are compared to zero (0). >> find ([2 -7 4 -3]>1) 1 3 INPUTS OUTPUTS A B AND A&B OR A|B XOR (A,B) NOT A ~A NOT B ~B F T

Multi logical and relational operators >> Q = [4 8 0 5 16]; >> P = [3 7 4 5 19]; Conditions required Example Description Find elements in Q that are less than or equal to P >> Q<=P ans = 0 0 1 1 1 Q and P should have the same size, element to element comparison Find elements in Q that are greater than 5 >> Q>5 0 1 0 0 1 Comparing to scalar is different, the size doesn’t matter, each element is compared to 5 Find elements in Q that are less than or equal to P and greater than 5 >> Q<=P & Q>5 0 0 0 0 1 Notice the use of and as a word in the condition and how it is translated into MATLAB expression Find elements in Q that are less than or equal to P or greater than 5 >> Q<=P | Q>5 0 1 1 1 1 Notice the use of or as a word in the condition and how it is translated into MATLAB expression Important: You can not join two or more relational operator in the same relation, i.e: (0<x<=4). Instead, a logical operator should be placed between each single relation, i.e: (0<x & 4>=x).

Thank you for the attention Any Questions and Suggestions please…