Download Understanding Logical Decisions and Arrays in MATLAB Programming and more Slides Calculus for Engineers in PDF only on Docsity!
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
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 Logical Expression
Statements
end
Statements
True
False
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
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 5.
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
Example: Decisions & Logicals
r = 7; s = -11; % Double if if r < 13 if s > - t = log(abs(r)+abs(s)) end end % if-n-& if (r < 13)&(s > -17) q = log(abs(r)+abs(s)) end
The OutPut **t =
q = 2.**
Start elseif FlowChart
if Logical Expression
Statements-
end
Statements
elseif Logical Expr True
else
Statements-
True Statements-
else
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 user y = z The InPut & OutPut x = -3. y = -0.
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
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
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
Example: Prob 4- 20
- Given Spring-Loaded Platform
In This Case the ENGR36 Force- Balance Model
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
W k x k ( x d ) x d
W k x x d = + − ≥
= < 2 :
: 1 2
1
The Prob 4-20 Plots
After Brk-Pt
(^111) 1 dW k m = dx =
(^2 )
1 dW k k m dx = = +
Prob 4-20: spring_plat.m