Chp1 MATLAB OverView: Part-2 Engr/Math/Physics 25 Bruce Mayer, PE

advertisement
Engr/Math/Physics 25
Chp1 MATLAB
OverView: Part-2
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_MATLAB_OverView-2.ppt
Learning 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
Engineering/Math/Physics 25: Computational Methods
2
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Systems of Linear Equations
 Consider an
Electrical Circuit for
Which we need to
Find the OutPut
Electrical Potential
 Using the ENGR43
Method of Nodal
Analysis we find
1Vo  0V1  1V2  2000 I x  0
0Vo  1V1  0V2  0 I x  12 V
2kIx
1 k
V1
1Vo  1V1  1V2  0 I x  0
1 k
V2
+
1 k
12 V
1 k
Ix
Engineering/Math/Physics 25: Computational Methods
3
Vo

0Vo  0V1  1V2  1000 I x  0
 MTH6 Provides
Methods to solve
this System of Eqns
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt

A  x b
Systems of Linear Equations cont
 We Will Use
MATLAB’s “Left”
Division Solver
 Write the System of
Eqns (below) in
Matrix/Array Form
1 0  1  2000 Vo   0 
0 1 0
 V  12
0

 1    
1  1 1
0  V2   0 

   
0 0 1  1000   I x   0 
 In MATLAB need to
InPut the
1Vo  0V1  1V2  2000 I x  0
0Vo  1V1  0V2  0 I x  12 V
1Vo  1V1  1V2  0 I x  0
0Vo  0V1  1V2  1000 I x  0
Engineering/Math/Physics 25: Computational Methods
4
• 4x4 COEFFICIENT
Matrix, A
• 4x1 CONSTRAINT
Vector, b
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Systems of Linear Equations cont
 Use MATLAB to Solve 4Eqns in 4Unkowns
>> A = [1,0,-1,-2000; 0,1,0,0; 1,-1,1,0;
0,0,1,-1000];
Row Separator
>> b = [0;12;0;0];
Left Division
>> Soln = A\b
Soln =
9.0000
12.0000
V1  12 Volts
3.0000
V2  3 Volts
0.0030
I x  0.003 Amps
Engineering/Math/Physics 25: Computational Methods
5
 Thus the Solution by
MATLAB
Vo  9 Volts
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Left Division Syntax
 “Normal”; i.e., RIGHT Division:
P/Q
READ
• Read as “P divided by Q”
 LEFT (a.k.a. “Back”), RIGHT Division:
READ
S\R
• Read as “R divided by S”
Engineering/Math/Physics 25: Computational Methods
6
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Left 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];
 By MTH6
>> b = [0;12;0;0];
>> Soln = A\b
The Matrix “Inverse”
(More on This Later)
 
Ax  b 
1 
1
A Ax  A b 
 
1
1
A A x  x  A b
x  A\b
Engineering/Math/Physics 25: Computational Methods
7
• x is the Solution Vector
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Performing 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
Engineering/Math/Physics 25: Computational Methods
8
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
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
% 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.
 Note that the portion
of the line before
>>x = 2+3 % So is this.
the % sign is
x =
executed to
5
compute x.
>>% This is a comment.
Engineering/Math/Physics 25: Computational Methods
9
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
MATLAB Editor/Debugger
Engineering/Math/Physics 25: Computational Methods
10
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Script 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
Engineering/Math/Physics 25: Computational Methods
11
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
DeBugging 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
Engineering/Math/Physics 25: Computational Methods
12
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Locating Program Errors

To locate program errors, try:
1. Use a simple version of the problem
which can be checked by hand to
Test your program
2. Display any intermediate calculations
by removing semicolons at the end of
statements.
3. Use the debugging features of the
Editor/Debugger.
Engineering/Math/Physics 25: Computational Methods
13
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Programming Style
1. Comments section
a. 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.
Engineering/Math/Physics 25: Computational Methods
14
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Programming Style cont
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
Engineering/Math/Physics 25: Computational Methods
15
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
InPut/OutPut Commands
Command
disp(A)
disp(’text’)
Description
Displays the contents, but not
the name, of the array A.
Displays the text string enclosed
within quotes.
Displays the text in quotes, waits
x = input(’text’) for user input from the keyboard,
and stores the value in x.
Displays the text in quotes, waits
for user input from the keyboard,
x = input(’text’,’s’)
and stores the input
as a TEXT STRING in x.
Engineering/Math/Physics 25: Computational Methods
16
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Script 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.
Engineering/Math/Physics 25: Computational Methods
17
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
FallSpeed Plot for tf = 7 sec
Engineering/Math/Physics 25: Computational Methods
18
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Exploit 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.
Engineering/Math/Physics 25: Computational Methods
19
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
MATLAB Help → Hidden Tab
Engineering/Math/Physics 25: Computational Methods
20
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Command 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.
Engineering/Math/Physics 25: Computational Methods
21
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Relational Operators
>> x = [6,3,9];
y = [14,2,9];
>> z = (x < y)
z =
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
Engineering/Math/Physics 25: Computational Methods
22
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
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
The Find Function
 find(x) computes an array containing
the INDICES of the NONzero elements
of the numeric array x. For example
>>x = [-2, 0, 4];
>> M = [4 -9 23; 0 78 -11; 32
0 0 ]
>>y = find(x)
M =
y =
1
3
4
-9
23
0
78
-11
32
0
0
 The resulting array y>>=NonZeroM
[1, 3] indicates
= find(M)
that the first and third elements of
array x are nonzero.NonZeroM =
Engineering/Math/Physics 25: Computational Methods
23
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
>> M = [4 -9 23; 0 78 -11; 32 0 0 ]
Another M
find
Example
=
4
-9
23
0
78
-11
32
0
0
>> NonZeroM = find(M)
NonZeroM =
1
3
4
5
7
8
Engineering/Math/Physics 25: Computational Methods
24
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
The Find 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)
Engineering/Math/Physics 25: Computational Methods
25
how_many =
2
>>indices =
find(x<y)
indices =
1
4
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
The if Statement
 The general form of the if statement
if
expression
 The elseif
statement can be
commands
REPEATED if
elseif expression
Needed
commands
 The else and
elseif
else
statements may
commands
be OMITTED if
end
not required
Engineering/Math/Physics 25: Computational Methods
26
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
if Example
 Use MATLAB to
Evaluate the
Piecewise Function
15 4 x  10

y  10 x  10
10

x9
0 x9
x0
 The Script File
Engineering/Math/Physics 25: Computational Methods
27
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
>> x = -11;
if Example OutPut
15 4 x  10

y  10 x  10
10

x9
0 x9
x0
>> if_Test_0506
y =
10
>> x = 7.3;
>> if_Test_0506
y =
83
>> x = 13;
>> if_Test_0506
y =
Engineering/Math/Physics 25: Computational Methods
28
118.1665
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Loops

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
Engineering/Math/Physics 25: Computational Methods
29
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
for Loop Example
m = 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.
Engineering/Math/Physics 25: Computational Methods
30
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
while Loop Example
 The loop variable x is
initially assigned the value
while x < 25
5, and it keeps this value
k = k + 1;
until the statement x =
y(k) = 3*x;
2*x - 1 is encountered
the first time. Its value then
x = 2*x-1;
changes to 9. Before each
end
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.
x = 5;k = 0;
Engineering/Math/Physics 25: Computational Methods
31
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
CAVEAT
 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).
Engineering/Math/Physics 25: Computational Methods
32
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Demos: for & while
 Prob 1-39 → Evaluate with for
sum 
k 10
 5k
• Also list the value of
the individual Terms
3
k 1
 Prob 1-40 → Use while
to find the number of
terms, qmax, such that
Total 
k  qmax
1.73
k
 9999
k 1
Engineering/Math/Physics 25: Computational Methods
33
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Engr/Math/Physics 25
Appendix
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
34
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
>> run Prob1dash39
term =
5
term =
40
term =
135
term =
320
term =
625
term =
1080
term =
1715
term =
2560
term =
3645
term =
5000
sum =
15125
Engineering/Math/Physics 25: Computational Methods
35
Problem 1-39
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Problem 1-40
>> run
Prob1dash40
min no. terms
=
15
Sum-Total for
max-terms =
8.8165e+003
Engineering/Math/Physics 25: Computational Methods
36
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
Engineering/Math/Physics 25: Computational Methods
37
Bruce Mayer, PE
BMayer@ChabotCollege.edu • ENGR-25_MATLAB_OverView-2.ppt
FallSpeed Code
% 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
Download