bruce mayer, pe licensed electrical & mechanical engineer bmayer@chabotcollege
DESCRIPTION
Engr/Math/Physics 25. Chp4 MATLAB Programming-3. Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected]. Learning Goals. Write MATLAB Programs That can MAKE “Logical” Decisions that Affect Program Output Write Programs that Employ LOOPing Processes - PowerPoint PPT PresentationTRANSCRIPT
[email protected] • ENGR-25_Programming-3.ppt1
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Chp4 MATLAB
Programming-3
[email protected] • ENGR-25_Programming-3.ppt2
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Learning Goals Write MATLAB Programs That can
MAKE “Logical” Decisions that Affect Program Output
Write Programs that Employ LOOPing Processes• For → No. Loops know a priori• while → Loop Terminates based on
Logic Criteria
[email protected] • ENGR-25_Programming-3.ppt3
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
if Decision Statement
The if Statement Basic Form
if logical expression
statements
end Every if statement must
have an accompanying end statement. • The end statement marks
the terminous of the statements that are to be executed if the logical expression is true.
Start
if LogicalExpression
Statements
end
Statements
True
False
[email protected] • ENGR-25_Programming-3.ppt4
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
else Decision Statement
else Statement Basic Form
if logical expression
Statement Group-1
else
Statement Group-2
end
Start
if LogicalExpression
Statements-1
end
Statements
True
False
Statements-2
[email protected] • ENGR-25_Programming-3.ppt5
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Logical Expression on Arrays
When the test, if logical expression, is performed, where the logical expression may be an ARRAY, the test returns a value of true only if ALL the elements of the logical expression evaluated as true• “Logic Gate” equivalent statement (“and”)
– ALL High → Output High– ELSE Output Low
[email protected] • ENGR-25_Programming-3.ppt6
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Logical Array Example
For example, if we fail to recognize how the test works, the following statements may not perform as expected
x = [4,-9,25];if x < 0
disp(’Some elements of x are negative.’)else
y = sqrt(x)end
Because the test if x < 0 is false, when this program is run it gives the resulty = 2.0000 0 + 3.0000i 5.0000
[email protected] • ENGR-25_Programming-3.ppt7
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Logical Array Example cont
Now Fine-Tune to Test for Positive xx = [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 if x < 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
[email protected] • ENGR-25_Programming-3.ppt8
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Combine Decisions & Logicals
The following statementsif logical expression 1 if logical expression 2
statementsend
end
Can be written more Concisely with
if logical expression 1 & logical expression 2statements
end
[email protected] • ENGR-25_Programming-3.ppt9
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Example: Decisions & Logicals
The Coder = 7; s = -11;% Double ifif r < 13 if s > -17 t = log(abs(r)+abs(s)) endend% if-n-&if (r < 13)&(s > -17) q = log(abs(r)+abs(s))end
The OutPut t = 2.8904q = 2.8904
[email protected] • ENGR-25_Programming-3.ppt10
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The elseif Statement The elseif Statement Basic
Formif logical expression 1 statement group 1elseif logical expression 2 statement group 2else statement group 3end
• The else and elseif statements may be omitted if not required. However, if both are used, the else statement must come AFTER the elseif statement to since the else yields the DEFAULT result
[email protected] • ENGR-25_Programming-3.ppt11
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
elseif FlowChartStart
if LogicalExpression
Statements-1
end
Statements
elseif Logical Expr
True
else
Statements-2
True
Statements-3
else
[email protected] • ENGR-25_Programming-3.ppt12
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
elseif Example
Given• y = log(x) for
x > 10 • y = sqrt(x)
for 0 <= x <= 10,
• y = exp(x) - 1 for x < 0
The Following finds y for given user input for x
x = input('enter scalar: x = ')if x > 10; z = log(x);elseif x >= 0 z = sqrt(x);else z = exp(x) - 1;end% output to usery = z
The InPut & OutPut x = -3.7000y = -0.9753
[email protected] • ENGR-25_Programming-3.ppt13
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Nested if FlowChartStart
x < 13?
y = sinh(x)
Yes
y, z out
y > 43?
y = 7.3xz = -19x + 29
y 17?
z = √y z = y1.89
z = 0
No
No No
Yes Yes
Nesting if statements is a alternative to the elseif form
[email protected] • ENGR-25_Programming-3.ppt14
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Strings
A STRING is a variable that contains Characters. • 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
[email protected] • ENGR-25_Programming-3.ppt15
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Strings cont
The following string, digits, is not the same as the variable number created by typing digits = 987.>> digits = '987'digits =987
>> p = log(digits)??? Function 'log' is not defined for values of class 'char'.
No Indent for String
[email protected] • ENGR-25_Programming-3.ppt16
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Strings & the input Statement
The prompt program on the next slide uses the 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 string variable x.• The function returns an empty matrix if you
press the Enter key without typing anything.
x = input(’prompt’, ’string’)
[email protected] • ENGR-25_Programming-3.ppt17
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Strings and Decisions
The following prompt program is a script file that allows the user to answer Yes by typing either Y or y or by pressing the Enter key. • 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 = ’Y’else
response = ’N’end
[email protected] • ENGR-25_Programming-3.ppt18
Bruce Mayer, PE Engineering/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 “Spring Constant” in N/m
– x the Compression Distance in m
dxdxkxkW
dxxkW
:2
:
21
1
[email protected] • ENGR-25_Programming-3.ppt19
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Problem 4-20 cont
Find or Createa. A Function to find x given W, k1, k2, and db. A Plot of x vs. W for 0 W 3000 N given
– k1 = 10 kN/m = 10 N/mm– k2 = 15 kN/m = 15 N/mm– d = 100 mm
Solve Force-Balance Eqns for x dx
kk
dkWx
dxkWx
:2
2
:
21
2
1
[email protected] • ENGR-25_Programming-3.ppt20
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The Prob 4-20 Plots
Before Brk-Pt After Brk-Pt
111
1
kdW
dxm
2122 2
1
kkdW
dxm
[email protected] • ENGR-25_Programming-3.ppt21
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
All Done for Today
A NiceSpring
Platform
[email protected] • ENGR-25_Programming-3.ppt22
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Appendix 6972 23 xxxxf
[email protected] • ENGR-25_Programming-3.ppt23
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Prob 4-20: spring_plat.m
[email protected] • ENGR-25_Programming-3.ppt24
Bruce Mayer, PE Engineering/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
[email protected] • ENGR-25_Programming-3.ppt25
Bruce Mayer, PE Engineering/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 SECTIONWmax = 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 = ');
[email protected] • ENGR-25_Programming-3.ppt26
Bruce Mayer, PE Engineering/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 <= Wmax % All 3 springs engaged W=[0, Wcrit, Wmax]; % 3element row vector % make corresponding 3-pt output vector x = [spring_plat(W(1),k1,k2,d),spring_plat(W(2),k1,k2,d),... spring_plat(W(3),k1,k2,d)]; plot(W,1000*x,'-s'), xlabel('Wt (N)'), ylabel('Compression (mm)'),... title('Above-Critical Load') % Plot 2pts for one Spring Linear Rangeelse W = [0,Wmax] x = [spring_plat(W(1),k1,k2,d), spring_plat(W(2),k1,k2,d)]; plot(W,1000*x,'-s'), xlabel('Wt (N)'), ylabel('Compression (mm)'),... title('Below-Critical Load')end