Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming for Mechanical Engineers (ME 319)

Similar presentations


Presentation on theme: "Introduction to Programming for Mechanical Engineers (ME 319)"— Presentation transcript:

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

2 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

3 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.

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

5 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 = [ ]; >> b = [ ]; >> c = a>=b % find the values in a that are greater or equal to values in b c =

6 & | ~ 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 = [ ]; >> b = [ ]; >> a & b % both A AND B are true ans = >> a | b % A OR B OR both are true >> ~ a % NOT A, if true(≠ 0) result is false ans = % if false (A(i) = 0) results is true

7 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 |

8 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 = all (A) True only if all elements are true (non-zeros). False if At least one element is zero. >> all ([ ]) any (A) True if at least one element is non-zero. False if all elements are zeros. >> any ([ ]) 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 ([ ]>1) INPUTS OUTPUTS A B AND A&B OR A|B XOR (A,B) NOT A ~A NOT B ~B F T

9 Multi logical and relational operators
>> Q = [ ]; >> P = [ ]; Conditions required Example Description Find elements in Q that are less than or equal to P >> Q<=P ans = Q and P should have the same size, element to element comparison Find elements in Q that are greater than 5 >> Q>5 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 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 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).

10 Thank you for the attention
Any Questions and Suggestions please…


Download ppt "Introduction to Programming for Mechanical Engineers (ME 319)"

Similar presentations


Ads by Google