7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
1/26
[email protected] ENGR-25_Programming-3.ppt1Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Chp4 MATLAB
Programming-3
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
2/26
[email protected] ENGR-25_Programming-3.ppt2Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Learning Goals
Write MATLAB Programs That canMAKE Logical Decisions that AffectProgram Output
Write Programs that Employ LOOPing
Processes
For No. Loops know a priori
while Loop Terminates based onLogic Criteria
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
3/26
[email protected] ENGR-25_Programming-3.ppt3Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
if Decision Statement
The if Statement Basic Formiflogical expression
statements
end Every if statement must have
an accompanying end
statement.
The end statement marks theterminous of the statements that
are to be executed ifthe logical
expression is true.
Start
if Logical
Expression
Statements
end
Statements
True
False
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
4/26
[email protected] ENGR-25_Programming-3.ppt4Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
else Decision Statement
else StatementBasic Form
iflogical expression
Statement Group-1
else
Statement Group-2
end
Start
if Logical
Expression
Statements-1
end
Statements
True
False
Statements-2
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
5/26
[email protected] ENGR-25_Programming-3.ppt5
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Logical Expression on Arrays
When the test, iflogical expression, isperformed, where the logical
expression may be an ARRAY, the test
returns a value oftrue only ifALL the
elements of the logical expression
evaluated as true
Logic Gate equivalent statement (and) ALL High Output High
ELSE Output Low
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
6/26
[email protected] ENGR-25_Programming-3.ppt6
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Logical Array Example
For example, if we fail to recognize how thetest works, the following statements may not
perform as expectedx = [4,-9,25];
if x < 0disp(Some elements of x are negative.)else
y = sqrt(x)end
Because the test ifx < 0 is false, when this
program is run it gives the resulty =
2.0000 0 + 3.0000i 5.0000
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
7/26
[email protected] ENGR-25_Programming-3.ppt7
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Logical Array Example cont
Now Fine-Tune to Test forPositivexx = [4,-9,25];if x >= 0
y = sqrt(x)else
disp('Some elements of x are negative.')end
This File Produces Only the Message
Some elements of x are negative.
The test ifx < 0 is false, and the test if
x >= 0 also returns a false value because
x >= 0 returns the vector[1,0,1].
ENTIRE Statement is FALSE
since ONE element is False
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
8/26
[email protected] ENGR-25_Programming-3.ppt8
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Combine Decisions & Logicals
The following statementsif logical expression 1if logical expression 2
statementsend
end
Can be written more Concisely with
if logical expression 1 & logical expression 2
statementsend
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
9/26
[email protected] ENGR-25_Programming-3.ppt9
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Example: Decisions & Logicals
The Coder = 7; s = -11;% Double ifif r < 13
if s > -17t = log(abs(r)+abs(s))
endend% if-n-&if (r < 13)&(s > -17)
q = log(abs(r)+abs(s))end
The OutPutt =
2.8904q =
2.8904
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
10/26
[email protected] ENGR-25_Programming-3.ppt10
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
The elseif Statement
Theelseif
Statement Basic Formif logical expression 1
statement group 1
elseif logical expression 2
statement group 2
else
statement group 3
end
The else andelseif statements
may be omitted if not
required. However, if
both are used, theelse statement must
come AFTER theelseif statement to
since the else yieldsthe DEFAULT result
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
11/26
[email protected] ENGR-25_Programming-3.ppt11
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
elseif FlowChartStart
if Logical
Expression
Statements-1
end
Statements
elseif
Logical Expr
True
else
Statements-2
True
Statements-3
else
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
12/26
[email protected] ENGR-25_Programming-3.ppt12
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
elseif Example
Given y = log(x) for
x > 10
y = sqrt(x) for
0 = 0z = sqrt(x);
elsez = exp(x) - 1;
end% output to usery = z
The InPut & OutPutx =
-3.7000y =
-0.9753
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
13/26
[email protected] ENGR-25_Programming-3.ppt13
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Nested if FlowChartStart
x < 13?
y = sinh(x)
Yes
y, z out
y > 43?
y = 7.3x
z = -19x + 29
y 17?
z = y z = y1.89z = 0
No
No No
Yes Yes
Nesting if statements
is a alternative to theelseif form
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
14/26
[email protected] ENGR-25_Programming-3.ppt14
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Strings
A STRING is a variable that containsCharacters.
Strings are useful for creating input prompts &messages, and for storing & operating on data
such as names and addresses To create a string variable, enclose the
characters in single quotes.
For example, Create string variable NYgiant:
>> NYgiant = 'Mel Ott'NYgiant =Mel Ott
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
15/26
[email protected] ENGR-25_Programming-3.ppt15
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Strings cont
The following string, digits, is not thesame as the variable number created bytyping digits = 987.
>> digits = '987'digits =987
>> p = log(digits)
??? Function 'log' is not definedforvalues of class 'char'.
No Indent for String
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
16/26
[email protected] ENGR-25_Programming-3.ppt16
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Strings & the input Statement
The prompt program on the next slide usesthe isempty(x) function, which returns a 1
if the array x is empty and 0 otherwise
The Program also uses the input function,
with syntax
This function displays the string prompt on
the screen, waits for input from the keyboard,
and returns the entered value in the stringvariable x.
The function returns an empty matrix if youpress the Enterkey without typing anything.
x = input(prompt, string)
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
17/26
[email protected] ENGR-25_Programming-3.ppt17
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Strings and Decisions
The following prompt program is a script filethat allows the user to answerYes by typingeitherY ory or by pressing the Enterkey.
Any other response is treated as the answer No.
response = input('Want to continue?Y/N:','s')if (isempty(response))|(response==Y)|response==y)
response = Yelseresponse = N
end
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
18/26
[email protected] ENGR-25_Programming-3.ppt18
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Example: Prob 4-20
Given Spring-Loaded Platform
In This Case the
ENGR36 Force-
Balance Model
In ENGR36 we
Learn that the
Spring Force
xkFs Where
k the SpringConstant in N/m
x the CompressionDistance in m
dxdxkxkW
dxxkW
:2
:
21
1
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
19/26
[email protected] ENGR-25_Programming-3.ppt19
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Problem 4-20 cont
Find or Createa. A Function to find x given W, k1, k2, and d
b. A Plot of x vs. W for 0 W 3000 Ngiven
k1 = 10 kN/m = 10 N/mm k2 = 15 kN/m = 15 N/mm d = 100 mm
SolveForce-BalanceEqns for x dx
kk
dkWx
dxkWx
:2
2
:
21
2
1
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
20/26
[email protected] ENGR-25_Programming-3.ppt20
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
The Prob 4-20 Plots
Before Brk-Pt
After Brk-Pt
11
1
1
kdW
dxm
212
22
1
kkdW
dx
m
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
21/26
[email protected] ENGR-25_Programming-3.ppt21
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
All Done for Today
A NiceSpring
Platform
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
22/26
[email protected] ENGR-25_Programming-3.ppt22
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Appendix 6972 23 xxxxf
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
23/26
[email protected] ENGR-25_Programming-3.ppt23
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Prob 4-20: spring_plat.m
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
24/26
[email protected] ENGR-25_Programming-3.ppt24
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Prob-20 Test Session
Case-1 W = 500N>> spring_plat(500,10e3,15e3,0.1)The Compression distance in m, x =
0.0500
Case-2 W = 2 kN
>> spring_plat(2e3,10e3,15e3,0.1)The Compression distance in m, x =
0.1250
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
25/26
[email protected] ENGR-25_Programming-3.ppt25
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
.m-file = Prob4_20_plot.m
Documentation & Inputs% Bruce Mayer, PE * 26Sep11% ENGR25 * Problem 4-20% file = Prob4_20_plot.m
% Plot Displacement vs. Wt for Spring Platform%% INPUT SECTION
Wmax = input('Max Weight in N = ');k1 = input('Main-Spring Const in N/m = ');k2 = input('Secondary-Spring Const in N/m = ');d = input('distance to 2nd Springs in m = ');
7/30/2019 ENGR-25 Lec-11 Sp12 Programming-3 Decisions
26/26
[email protected] ENGR-25_Programming-3.ppt26
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
.m-file = Prob4_20_plot.m cont%CALCULATION SECTION
% calc Critical-Load for Curve Break-PointWcrit = k1*d% Plot 3 pts for PieceWise Linear rangeif Wcrit
Top Related