Chp4 MATLAB Programming-3 Engr/Math/Physics 25 Bruce Mayer, PE

advertisement
Engr/Math/Physics 25
Chp4 MATLAB
Programming-3
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
1
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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
Engineering/Math/Physics 25: Computational Methods
2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
if Decision Statement

The if Statement Basic Form
statements
end
True
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.
Engineering/Math/Physics 25: Computational Methods
3
Statements
end
Statements
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
False
if Logical
Expression
if logical expression

Start
else Decision Statement

else Statement
Basic Form
if logical expression
Statement Group-1
else
Statement Group-2
end
Start
if Logical
Expression
False
True
Statements-1
Statements-2
end
Statements
Engineering/Math/Physics 25: Computational Methods
4
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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
Engineering/Math/Physics 25: Computational Methods
5
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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 result
y =
2.0000
0 + 3.0000i
Engineering/Math/Physics 25: Computational Methods
6
5.0000
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Logical Array Example cont

Now Fine-Tune to Test for Positive x
x = [4,-9,25];
ENTIRE Statement is FALSE
if x >= 0
since ONE element is False
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].
Engineering/Math/Physics 25: Computational Methods
7
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Combine Decisions & Logicals

The following statements
if logical expression 1
if logical expression 2
statements
end
end

Can be written more Concisely with
if logical expression 1 & logical expression 2
statements
end
Engineering/Math/Physics 25: Computational Methods
8
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Example: Decisions & Logicals

The Code

The OutPut
t =
r = 7; s = -11;
2.8904
% Double if
q =
if r < 13
2.8904
if s > -17
t = log(abs(r)+abs(s))
end
end
% if-n-&
if (r < 13)&(s > -17)
q = log(abs(r)+abs(s))
end
Engineering/Math/Physics 25: Computational Methods
9
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
The elseif Statement
 The elseif Statement Basic Form
if logical expression 1
• The else statement
statement group 1
must come AFTER
the elseif
elseif logical expression 2
statement to since the
statement group 2
else yields the
else
DEFAULT result
statement group 3
end
Default
Engineering/Math/Physics 25: Computational Methods
10
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Start
if Logical
Expression
elseif FlowChart
else
True
Statements-1
elseif
Logical Expr
else
True
Statements-2
Statements-3
end
Statements
Engineering/Math/Physics 25: Computational Methods
11
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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 user
y = z
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
Engineering/Math/Physics 25: Computational Methods
12

The InPut & OutPut
x =
-3.7000
y =
-0.9753
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Start
Yes
Nested if FlowChart
x < 13?

No
y = 7.3x
z = -19x + 29
y = sinh(x)
y > 43?
Yes
z = √y
No
Nesting if statements
is a alternative to the
elseif form
y  17?
Yes
No
z=0
z = y1.89
y, z out
Engineering/Math/Physics 25: Computational Methods
13
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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
Engineering/Math/Physics 25: Computational Methods
14
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Strings cont

The following string, digits, is not the
same as the variable number created by
typing digits = 987.
>> digits = '987'
digits =
No Indent for String
987
>> p = log(digits)
??? Function 'log' is not defined for
values of class 'char'.
Engineering/Math/Physics 25: Computational Methods
15
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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 x = input(’prompt’, ’string’)

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.
Engineering/Math/Physics 25: Computational Methods
16
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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
Engineering/Math/Physics 25: Computational Methods
17
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Display Str&Double on SAME Line
 Use disp(X), Along with the [ ]
Operator
 Example Find: 𝑒
>> disp(['the SqRt of "e" is ', num2str(exp(1/2), 7), '; a nice result
the SqRt of "e" is 1.648721; a nice result
;-)'])
;-)
 Example Find 𝑒 3.7
>> Eto3_7_Ltrs = num2str(exp(3.7), 11)
Eto3_7_Ltrs =40.44730436
>> disp(['e to 3.7 pwr = ', Eto3_7_Ltrs])
e to 3.7 pwr = 40.44730436
Engineering/Math/Physics 25: Computational Methods
18
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Example: Prob 4-20

Given SpringLoaded Platform

In ENGR36 we
Learn that the
Spring Force
Fs  k  x
•
Where
– k  the “Spring
Constant” in N/m
– x  the Compression
Distance in m
Engineering/Math/Physics 25: Computational Methods
19

In This Case the
ENGR36 ForceBalance Model
W  k1 x
:xd
W  k1 x  2k 2 x  d  : x  d
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Problem 4-20 cont

Find or Create
a. A Function to find x given W, k1, k2, and d
b. 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
Engineering/Math/Physics 25: Computational Methods
20
x  W k1
:xd
W  2k 2 d
x
k1  2k 2
:xd
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
The Prob 4-20 Plots
 Before Brk-Pt
 After Brk-Pt
b2 
m1 
m2 
dx
dW

2
dx
1

dW 1 k1
Engineering/Math/Physics 25: Computational Methods
21
2k 2 d
k1  2k 2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
1
k1  2k 2
All Done for Today
A Nice
Spring
Platform
Engineering/Math/Physics 25: Computational Methods
22
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Engr/Math/Physics 25
Appendix
f x   2 x  7 x  9 x  6
3
2
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
23
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Prob 4-20: spring_plat.m
Engineering/Math/Physics 25: Computational Methods
24
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
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
Engineering/Math/Physics 25: Computational Methods
25
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
.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 = ');
Engineering/Math/Physics 25: Computational Methods
26
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
.m-file = Prob4_20_plot.m cont
%CALCULATION SECTION
% calc Critical-Load for Curve Break-Point
Wcrit = k1*d
% Plot 3 pts for PieceWise Linear range
if 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 Range
else
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
Engineering/Math/Physics 25: Computational Methods
27
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_Programming-3.ppt
Download