Upload
catherine-quinn
View
214
Download
0
Embed Size (px)
Citation preview
[email protected] • ENGR-25_MATLAB_OverView-2.ppt1
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Chp1 MATLABChp1 MATLABOverView: OverView:
Part-2Part-2
[email protected] • ENGR-25_MATLAB_OverView-2.ppt2
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Learning GoalsLearning Goals
Turn On MATLAB and use as a calculator
Create Basic Cartesian Plots Write and Save simple “Script”
Program-files Execute Conditional Statements
• IF, THEN, ELSE, >, <, >=, etc.
Execute Loop Statements• FOR & WHILE
[email protected] • ENGR-25_MATLAB_OverView-2.ppt3
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Systems of Linear EquationsSystems of Linear Equations Consider an
Electrical Circuit for Which we need to Find the OutPut Electrical Potential
Ix
1 k
1 k 1 k
1 k12 V
2kIx
+
Vo
V1V2
Ix
1 k
1 k 1 k
1 k12 V
2kIx
+
Vo
V1V2
Using the ENGR43 Method of Nodal Analysis we find
01000100
00111
V 120010
02000101
21
21
21
21
xo
xo
xo
xo
IVVV
IVVV
IVVV
IVVV
MTH6 Provides Methods to solve this System of Eqns
[email protected] • ENGR-25_MATLAB_OverView-2.ppt4
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Systems of Linear Equations Systems of Linear Equations contcont
We Will Use MATLAB’s “Left” Division Solver
Write the System of Eqns (below) in Matrix/Array Form In MATLAB need to
InPut the • 4x4 COEFFICIENT
Matrix, A
• 4x1 CONSTRAINT Vector, b
0
0
12
0
1000100
0111
0010
2000101
2
1
x
o
I
V
V
V
01000100
00111
V 120010
02000101
21
21
21
21
xo
xo
xo
xo
IVVV
IVVV
IVVV
IVVV
A b
x
[email protected] • ENGR-25_MATLAB_OverView-2.ppt5
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Systems of Linear Equations Systems of Linear Equations contcont
Use MATLAB to Solve 4Eqns in 4Unkowns>> A = [1,0,-1,-2000; 0,1,0,0; 1,-1,1,0; 0,0,1,-1000];
>> b = [0;12;0;0];
>> Soln = A\b
Soln =
9.0000
12.0000
3.0000
0.0030
Row Separator
Left Division
Thus the Solution by MATLAB
Amps 003.0
Volts 3
Volts 12
Volts 9
2
1
x
o
I
V
V
V
[email protected] • ENGR-25_MATLAB_OverView-2.ppt6
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Left Division SyntaxLeft Division Syntax
“Normal”; i.e., RIGHT Division:
P/Q• Read as “P divided by Q”
LEFT (a.k.a. “Back”), RIGHT Division:
S\R• Read as “R divided by S”
READ
READ
[email protected] • ENGR-25_MATLAB_OverView-2.ppt7
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Left Div & Matrix InverseLeft Div & Matrix Inverse Use MATLAB to Solve 4Eqns in 4Unkowns>> A = [1,0,-1,-2000; 0,1,0,0; 1,-1,1,0; 0,0,1,-1000];
>> b = [0;12;0;0];
>> Soln = A\b
The Matrix “Inverse”(More on This Later)
By MTH6• x is the Solution Vector
bx
Abx 11 AAA
bxx 11 AAA
bx
\A
[email protected] • ENGR-25_MATLAB_OverView-2.ppt8
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Performing MATLAB OpsPerforming MATLAB Ops You can perform
operations in MATLAB in two ways:
1. In the interactive mode, in which all commands are entered directly in the Command window
2. By running a MATLAB program stored in script “m” file.– A Script file contains
MATLAB commands, so running it is equivalent to typing all the commands - one at a time - at the Command window prompt.
– Run the file by typing its name at the Command window prompt
[email protected] • ENGR-25_MATLAB_OverView-2.ppt9
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
% Comments% Comments COMMENTS are NONexecutable Statements
that help Document or Explain Script Files The comment symbol (%) may be put
anywhere in the line. MATLAB ignores everything to the right of the % symbol.
>>% This is a comment.
>>x = 2+3 % So is this.
x =
5
Note that the portion of the line before the % sign is executed to compute x.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt10
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
MATLAB Editor/DebuggerMATLAB Editor/Debugger
[email protected] • ENGR-25_MATLAB_OverView-2.ppt11
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Script File UsageScript File Usage The name of a script file must begin with a
letter, and may include digits and the underscore character, up to 63 characters.
Do not give a script file the same name as a variable
Do not give a script file the same name as a MATLAB command or function. • You can check to see if a
command, function or file name already exists by using the exist command
[email protected] • ENGR-25_MATLAB_OverView-2.ppt12
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
DeBugging Script FilesDeBugging Script Files Program errors usually fall into
one of the following categories.1. Syntax errors such as omitting a parenthesis or
comma, or spelling a command name incorrectly. MATLAB usually detects the more obvious errors and displays a message describing the error and its location.
2. Errors due to an incorrect mathematical procedure are called runtime errors. Their occurrence often depends on the particular input data. – A common example is division by zero
[email protected] • ENGR-25_MATLAB_OverView-2.ppt13
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Locating Program ErrorsLocating Program Errors
To locate program errors, try:1. Use a simple version of the problem
which can be checked by hand toTest your program
2. Display any intermediate calculations by removing semicolons at the end of statements.
3. Use the debugging features of the Editor/Debugger.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt14
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Programming StyleProgramming Style
1. Comments sectiona. The name of the program and any key words
in the first line.
b. The date created, and the creators' names in the second line.
c. The definitions of the variable names for every input and output variable. • Include definitions of variables used in the calculations
and units of measurement for all input and all output variables!
d. The (file)name of every user-defined function called by the program.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt15
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Programming Style Programming Style contcont
1. Input section • Include input data and/or the input functions and
comments for documentation.
2. Calculation section
3. Output section • This section might contain functions for displaying
the output on the screen or creating a plot
[email protected] • ENGR-25_MATLAB_OverView-2.ppt16
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
InPut/OutPut CommandsInPut/OutPut Commands
Command Description
disp(A)Displays the contents, but not the name, of the array A.
disp(’text’)Displays the text string enclosed within quotes.
x = input(’text’)Displays the text in quotes, waits for user input from the keyboard, and stores the value in x.
x = input(’text’,’s’)
Displays the text in quotes, waits for user input from the keyboard, and stores the input as a TEXT STRING in x.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt17
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Script File ExampleScript File Example
Problem: The speed v of a falling object dropped with no initial velocity is given as a function of time t by v = gt.• Where g is the Acceleration of Gravity; a
CONSTANT = 32.2 ft/s2
Use MATLAB to Plot v as a function of t for 0 ≤ t ≤ tf
• Where tf is the final time entered by the user.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt18
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
FallSpeed Plot for tFallSpeed Plot for tff = 7 sec = 7 sec
[email protected] • ENGR-25_MATLAB_OverView-2.ppt19
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Exploit the TextBookExploit the TextBook Throughout each chapter margin notes identify
where key terms are introduced. Each chapter contains tables summarizing the
MATLAB commands introduced in that chapter. At the end of each chapter is a summary guide to
the commands covered in that chapter. Appendix A contains tables of MATLAB
commands, grouped by category, with the appropriate page references.
There are three indexes. 1. lists MATLAB commands and symbols, 2. lists SimuLink blocks 3. lists topics.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt20
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
MATLAB Help → Hidden TabMATLAB Help → Hidden Tab
[email protected] • ENGR-25_MATLAB_OverView-2.ppt21
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Command Window Help-FcnsCommand Window Help-Fcns help funcname: Displays in the Command
window a description of the specified function funcname.
lookfor topic: Displays in the Command window a brief description for all functions whose description includes the specified key word topic.
doc funcname: Opens the Help Browser to the reference page for the specified function funcname, providing a description, additional remarks, and examples.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt22
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Relational OperatorsRelational Operators
Symbol Meaning
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
~= Not equal to
0 if FALSE 1 if TRUE
>> x = [6,3,9]; y = [14,2,9];
>> z = (x < y)
z =
1 0 0
>>z = (x > y)
z =
0 1 0
>>z = (x ~= y)
z =
1 1 0
>>z = ( x == y)
z =
0 0 1
>>z = (x > 8)
z =
0 0 1
[email protected] • ENGR-25_MATLAB_OverView-2.ppt23
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The The FindFind Function Function
find(x) computes an array containing the INDICES of the NONzero elements of the numeric array x. For example
The resulting array y = [1, 3] indicates that the first and third elements of array x are nonzero.
>>x = [-2, 0, 4];
>>y = find(x)
y =
1 3
>> M = [4 -9 23; 0 78 -11; 32 0 0 ]
M = 4 -9 23
0 78 -11
32 0 0
>> NonZeroM = find(M)
NonZeroM =
1
3
4
5
7
8
[email protected] • ENGR-25_MATLAB_OverView-2.ppt24
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
AnotherAnotherfindfind ExampleExample
>> M = [4 -9 23; 0 78 -11; 32 0 0 ]
M = 4 -9 23
0 78 -11
32 0 0
>> NonZeroM = find(M)
NonZeroM =
1
3
4
5
7
8
[email protected] • ENGR-25_MATLAB_OverView-2.ppt25
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The The FindFind Distinction Distinction
Note the difference between the result obtained by x(x<y) and the result obtained by find(x<y).>>x = [6,3,9,11];y = [14,2,9,13];
>>values = x(x<y)
values =
6 11
>>how_many = length(values)
how_many =
2
>>indices = find(x<y)
indices =
1 4
[email protected] • ENGR-25_MATLAB_OverView-2.ppt26
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
The The ifif Statement Statement The general form of the if statement
The elseif statement can be REPEATED if Needed
The else and elseif statements may be OMITTED if not required
if expression
commands
elseif expression
commands
else
commands
end
[email protected] • ENGR-25_MATLAB_OverView-2.ppt27
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
ifif Example Example Use MATLAB to
Evaluate the Piecewise Function
010
901010
910415
x
xx
xx
y
The Script File
[email protected] • ENGR-25_MATLAB_OverView-2.ppt28
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
ifif Example OutPut Example OutPut
010
901010
910415
x
xx
xx
y
>> x = -11;
>> if_Test_0506
y =
10
>> x = 7.3;
>> if_Test_0506
y =
83
>> x = 13;
>> if_Test_0506
y =
118.1665
[email protected] • ENGR-25_MATLAB_OverView-2.ppt29
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
LoopsLoops
There are two types of EXPLICIT Loops in MATLAB
1. The for loop, used when the number of passes is known ahead of time
2. The while loop, used when the looping process must terminate when a specified condition is satisfied
– In this case the the number of passes is not known in advance.
i.e., WHILEs use Dynamic Termination
[email protected] • ENGR-25_MATLAB_OverView-2.ppt30
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
forfor Loop Example Loop Examplem = 1; % Array-Building index
x(1) = 10; % 1st element of x = 10
for k = 2:3:11
m = m+1;
x(m+1) = x(m) + k^2;
end k takes on the values 2, 5, 8, 11. The variable
m indicates the index of the array x. When the loop is finished the array x will have the values x(1)=10 , x(2)=14, x(3)=39, x(4)=103, x(5)=224.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt31
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
whilewhile Loop Example Loop Examplex = 5;k = 0;
while x < 25
k = k + 1;
y(k) = 3*x;
x = 2*x-1;
end
The loop variable x is initially assigned the value 5, and it keeps this value until the statement x = 2*x - 1 is encountered the first time. Its value then changes to 9. Before each pass through the loop, x is checked to see if its value is less than 25. If so, the pass is made. If not, the loop is skipped.
[email protected] • ENGR-25_MATLAB_OverView-2.ppt32
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
CAVEATCAVEAT
Beware MATLAB Script file (“.m file) naming conventions• The name of a script file MUST begin with
a letter, and May include digits and the underscore character, up to 63 chars.
• May NOT include the DASH (-)
ca·ve·at ( P ) Pronunciation Key (kv-t, kv-, käv-ät), n.
A WARNING or CAUTION: “A final caveat: Most experts feel that clients get unsatisfactory results when they don't specify clearly what they want” (Savvy).
[email protected] • ENGR-25_MATLAB_OverView-2.ppt33
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Demos: Demos: forfor & & whilewhile
Prob 1-39 → Evaluate with for
10
1
35k
k
ksum • Also list the value of the individual Terms
Prob 1-40 → Use while to find the number of terms, qmax, such that
max
1
999973.1qk
k
kTotal
[email protected] • ENGR-25_MATLAB_OverView-2.ppt34
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
AppendiAppendixx
[email protected] • ENGR-25_MATLAB_OverView-2.ppt35
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Problem 1-39Problem 1-39>> run Prob1dash39term = 5term = 40term = 135term = 320term = 625term = 1080term = 1715term = 2560term = 3645term = 5000sum = 15125
[email protected] • ENGR-25_MATLAB_OverView-2.ppt36
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Problem 1-40Problem 1-40
>> run Prob1dash40min no. terms = 15
Sum-Total for max-terms = 8.8165e+003
[email protected] • ENGR-25_MATLAB_OverView-2.ppt37
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
FallS
peed
Co
de
FallS
peed
Co
de
% Bruce Mayer, PE% ENGR25 * 26Jan10% Program fall_speed_1001.m% Plots speed of a falling object in USA units%% Input Var:% tf = final time (in seconds)%% Output Var:% t = array of times at which speed is computed (in sec)% s = array of speeds (feet/sec converted to mph)%% Parameter Value:g = 32.2; % Acceleration in USA customary units%% Input section:tf = input('Enter final time in seconds = ');%% Calculation section:dt = tf/500; % time step for 501 values% Create an array of 501 time values.t = [0:dt:tf];% Compute speed values in mph.s = (g*t)*(60/88); % 88fps = 60mph%% Output section:disp('fall speed at t-final'); disp(s(length(s))); disp('mph')plot(t,s), xlabel('t (sec)'),ylabel('s (mph)'), title('Falling Speed vs Time'), grid