cheg220 week 3 lec 1
DESCRIPTION
introduction to matlab simulationTRANSCRIPT
Computational Method Course CHEG 220
Week_3_Lec_1
Programming with MATLAB
Previously in CHEG220
• Structured MATLAB Programs
• Top-down Structure
• Built-in and user-defined Functions
Outline
• Designing and developing programs • Relational Operators and Logical Variables • Logical operators and Functions • Conditional Statements • Loops and Switch Structures • Application to programming
Logical Operators (1)
MATLAB has five logical or Boolean operators ~ NOT & AND | OR && Short circuit AND || Short circuit OR Logical operators have lower precedence than the arithmetic operators
Logical Operators (2)
Examples
1- A[1,0,1]. ~A[0,1,0]
2- A[1,0,1], B[0,0,1]. A & B = C[0,0,1]
3- A[1,0,1], B[0,0,1]. A | B = C[1,0,1]
Logical Operators (2)
Order of precedence for operator types
1- Parentheses, evaluated starting with the innermost pair 2- Arithmetic operators and logical NOT (~), evaluated from left to right 3- Relational operators, evaluated from left to right 4- Logical AND 5-Logical OR
Logical operators (3)
Examples x[0,3,9], y[14,-2,9] z=~x , z[1,0,0] u=~x>y, u[0,1,0] v=~(x>y), v[1,0,1] z =[5,-3,0,0]&[2,4,0,5] z=[1,1,0,0] x[6,3,9] ,y[14,2,9], a[4,3,12],z=(x>y)&a, z[0,1,0] z =[5,-3,0,0]|[2,4,0,5] z=[1,1,0,1]
Logical operators (4)
The exclusive OR function xor(A,B) is defined as the following:
z=xor(A,B) = (A|B) & ~(A&B)
z=xor([3,0,6],[5,0,0]) z[0,0,1]
z=[3,0,6]|[5,0,0] z[1,0,1]
Logical operators (5)
Table of truth x y ~x x|y x&y xor (x,y) true true false true true false True false false true false true false true true true false true false false true false false false
Logical operators (6)
find Function
The function find(x) computes an array containing the
indices of the nonzero elements of the array x
Example: x=[5,-3,0,0,8]; y[2,4,0,5,7]
z=find[x&y] z=[1,2,5]
Conditional statements(1)
The MATLAB conditional statements enable us to write programs that make decisions
Conditional statements contain one or more of the if, else, and else if statements.
The end statement denotes the end of a conditional statement.
Conditional statements(2)
The if statement
if logical expression if x >= 0
statements y = sqrt(x)
end end
Conditional statements(3)
Another example z=0;w=0; if (x>=0)&(y>=0) z=sqrt(x)+sqrt(y) w=log(x)-3*log(y) end
Conditional statements(4)
Nested if statements
if logical expression 1 if x<0 statement group 1 y=abs(x) if logical expression 2 if y>4 statement group 2 z=sqrt(y) end end end end
Conditional statements(7)
The if statement if logical expression 1 if logical expression 1 & logical expression 2 if logical expression 2 statements statements end end end
Conditional statements(5)
Flowchart
x<0?
y>4?
y=abs(x)
z=sqrt(y)
End
x
false
false
true
true
Conditional statements(6)
The else statement
if logical expression if x >= 0 statement group 1 y = sqrt(x) else else statement group 2 y=exp(x)-1 end end
Conditional statements(8)
The else statement
Logical
Expression
Statement Group 1 Statement Group 2
start
False
True
End
Conditional statements(9)
The elseif statement
if logical expression 1 if x >= 5 statement group 1 y = log(x) elseif logical expression 2 elseif x>=0 statement group 2 y=sqrt(x) else else statement group 3 y=exp(x)-1 end end
Conditional statements(10)
The elseif statement
Logical Expression 1
Logical
Expression 1
Statement Group 2
Statement Group 3
Statement Group 1
Start
False
True False
True
End
Conditional statements(12)
flowchart
x>10
y=5x z=7x
y=log(x)
z=4y z=2y
z=0
y≥2.5? y≥3?
x
No
Yes
No No
Yes Yes
y,z
Conditional statements(11)
The program describing the flowchart on the last slide : if x > 10 y=log(x) if y >= 3 z=4*y elseif y >= 2.5 z=2*y else z=0 end else y=5*x z=7*x end