Академический Документы
Профессиональный Документы
Культура Документы
CHAPTER 1
INTRODUCTION
Goal of Course
For you to be able to generate readable, compact,
and verifiably correct MATLAB programs that
obtain numerical solutions to a wide range of
physical and empirical models, and to display the
results with fully annotated graphics.
Chapter 1 Objective
To introduce the fundamental characteristics of the
MATLAB environment and the languages basic
syntax
MATLAB
A computing language devoted to processing data in
the form of arrays of numbers (called matrices).
Integrates computation and visualization into a
flexible computer environment, and provides a diverse
family of built-in functions that can be used to obtain
numerical solutions to a wide range of engineering
problems.
Derives its name from MATrix LABoratory.
Notation Conventions
Variable/Function Name Font Example
User-created variable Times Roman ExitPressure
a2, sig
MATLAB function Courier cosh(x), pi
User-created function Times Roman BeamRoots(a, x, k)
Bold
Numerical Value Font Example
Provided in program Times Roman 5.672
Output to command window Helvetica 5.672
or to a graphical display
and with
format compact
format long e
format short
break global
case if
catch otherwise
continue persistent
else return
elseif switch
end try
for while
function
System response
p=
7.1000 Using semicolon
x= instead of commas
4.9200 suppresses this output
k=
-1.7000
Arithmetic Operators
Hierarchy
Level
( ) Parentheses 1
Prime (Apostrophe) 1
^ Exponentiation 2
* Multiplication 3
/ Division 3
+ Addition 4
Subtraction 4
Parentheses
Needed so that the mathematical operations are
performed on the proper collections of quantities
and in their proper order.
Within each set of parentheses the mathematical
operation are performed from left to right on
quantities at the same hierarchical level.
These rules can help minimize the number of
parentheses.
Some Examples
Mathematical
expression MATLAB expression
1 dcx+2 1-d*c^(x+2)
dcx + 2 d*c^x+2 or 2+d*c^x
(2/d)cx+2 (2/d)*c^(x+2) or 2/d*c^(x+2) or 2*c^(x+2)/d
(dcx + 2)/g2.7 (d*c^x+2)/g^2.7
dc x 2 sqrt(d*c^x+2) or (d*c^x+2)^0.5
Another Example
Consider
k
1
t
1 + px
The MATLAB script is
p = 7.1; x = 4.92; k = 1.7; % Numerical values
% must be assigned first
t = (1/(1+p*x))^k
which results in
t=
440.8779
Copyright Edward B. Magrab 2009 26
An Engineers Guide to MATLAB Chapter 1
Two Exceptions
(1) Complex Numbers: z = a +1jb or z = a +1ib (i = j = -1)
Example
a = 2; b = 3;
z = (a+1j*b)*(4-7j)
Note:
Real and complex numbers can be mixed without any
special concerns.
Example 1 Example 2
z = 4 + sqrt(-4) z = 1i^1i
System displays System displays
z= z=
4.0000 + 2.0000i 0.2079
Copyright Edward B. Magrab 2009 28
An Engineers Guide to MATLAB Chapter 1
x = 4.56e-2
or
x = 0.0456
or
x = 4.56*10^-2
Note:
Maximum number of digits that can follow the e is 3.
Trigonometric Hyperbolic
Function (radians) (degrees) Inverse Inverse
sine sin(x) sind(x) asin(x) sinh(x) asinh(x)
cosine cos(x) cosd(x) acos(x) cosh(x) acosh(x)
tangent tan(x) tand(x) atan(x) tanh(x) atanh(x)
secant sec(x) secd(x) asec(x) sech(x) asech(x)
cosecant csc(x) cscd(x) acsc(x) csch(x) acsch(x)
cotangent cot(x) cotd(x) acot(x) coth(x) acoth(x)
atan2(y, x) is the four quadrant version.
Example
For x = 0.1 and a = 0.5, determine the value of y when
e
- x
y= - sin( x )/cosh(a ) - lne ( x + a )
The script is
x = 0.1; a = 0.5;
y = sqrt(abs(exp(-pi*x)-sin(x)/cosh(a)-log(x+a)))
MATLAB x y Description
function
2.7 2.0000
y = fix(x) 1.9 1.0000 Round toward
2.49-2.51j 2.0000 2.0000i zero
2.7 3.0000
y = round(x) 1.9 2.0000 Round to nearest
2.492.51j 2.0000 3.0000i integer
2.7 3.0000
y = ceil(x) 1.9 1.0000 Round toward
2.49 2.51j 3.0000 - 2.0000i infinity
2.7 2.0000
y = floor(x) 1.9 2.0000 Round toward
2.49 2.51j 2.0000 3.0000i minus infinity
Additional Reasons -
Required when creating functions
Editor has many features
Commenting/Un-commenting
lines
Smart Indenting
Parentheses checking
Comments
in green
Keywords
in blue
Smart
indent Strings
in violet
Additional Reasons -
Click one icon to save and run a program
(Must use Save As first time)
Additional Reasons -
Enabling M-Lint from the Preferences menu
Illustration of M-Lint
Red
square
Orange
bar
Red bar
With cursor placed on red bar,
this error message is displayed.
Additional Reasons
Enabling the cell feature of the Editor
Run the
highlighted
cell
Third cell
1. Documentation:
Purpose and operations performed
Programmer's Name
Date originated
Date(s) revised
Description of the input(s): number, meaning, and
type
Description of the output(s): number, meaning, and
type
2. Input -
which includes numerous checks to ensure that all
input values have the qualities required for the
script/function to work properly.
3. Initialization -
where the appropriate variables are assigned their
numerical values.
4. Computation -
where the numerical evaluations are performed.
5. Output -
where the results are presented as graphical and/or
annotated numerical quantities.
Clicking on this
icon brings up the
Browser
d/2 2 d/2
23/2 Dc5/2 g - 0.5sin 2
3/2
Q=
8 sin 1 - cos
5/2
Dc
d
Dc 1 cos
2
The various
MATLAB windows
after executing the
program
On-Line Help:
Command Window
Symbolic Toolbox
The Symbolic Math Toolbox provides the capability
of manipulating symbols to perform algebraic,
matrix, and calculus operations symbolically
When one couples the results obtained from
symbolic operations with MATLABs ability to create
functions, one has a very effective means of
numerically evaluating symbolically obtained
expressions. This is introduced in Chapter 5.
-26881171155030517550432725348582123713611118.773742
If variable precision arithmetic had not been used, then
y = 2.6881171155030521043
Example
We shall take the derivative of bcos(bt), first with
respect to t and then with respect to b.
The script is
syms b t
dt = diff(b*cos(b*t), t, 1)
db = diff(b*cos(b*t), b, 1)
Upon execution, we obtain
dt =
-b^2*sin(b*t)
db =
cos(b*t)-b*sin(b*t)*t
Example
We shall integrate the results of the differentiation
performed previously. Thus,
syms b t
f = b*cos(b*t);
dt = diff(f, t, 1);
db = diff(f, b, 1);
it = int(dt, t)
ib = int(db, b)
The execution results in
it =
b*cos(b*t)
ib =
b*t*cos(b*t))
Limits
One can take the limit of a symbolic expression using
limit(f, x, z)
where
f = f(x) is the symbolic function
x = symbolic variable that is to assume the limiting
value z
Example
Determine the limit of
2a b
Lim
a
3a 4
The script is 2a b
Lim
a
3a 4
syms a b
Lim = limit((2*a+b)/(3*a-4), a, inf)
where inf =
Example
Determine the limit of
x
y
Lim 1
x
x
The script is x
y
Lim 1
syms y x x
x
Lim = limit((1+y/x)^x, x, inf)
Upon execution, we obtain
Lim =
exp(y)
In other words, the limit is ey.
k!
The function that performs this operation is
taylor(f, n, a, x)
Example
Obtain a four-term expansion of cos about o. The
script is
syms x tho
Tay = taylor(cos(x), 4, tho, x)
Upon execution, we obtain
Tay =
cos(tho)-sin(tho)*(x-tho)-1/2*cos(tho)*(x-tho)^2
+1/6*sin(tho)*(x-tho)^3
That is,
1 1
cos o sin o x o cos o x o sin o x o
2 3
2 6
Substitution
To substitute one expression b for another
expression a, the following function is used
subs(f, a, b)
where f = f(a)
Example
Consider the expression
1
F ( s) 2
s 2 s 1
where 0 < < 1.
The inverse Laplace transform is obtained from
syms s t z
f = ilaplace(1/(s^2+2*z*s+1), s, t)
The execution of this script gives
f=
exp(-t*z)*sinh(t*(z^2-1)^(1/2))/(z^2-1)^(1/2)
Example
Determine the curvature of the parametric curves
x 2b cos t b cos 2t
y 2b sin t b sin 2t
syms t a b
x = 2*b*cos(t) +b*cos(2*t);
y = 2*b*sin(t)-b*sin(2*t);
xp = diff(x, t, 1);
xpp = diff(x, t, 2);
yp = diff(y, t, 1);
ypp = diff(y, t, 2);
kn = xp*ypp-yp*xpp;
kd = xp^2+yp^2;
kn = factor(simple(kn));
kd = factor(simple(kd));
k = simple(kn/kd^(3/2))
The execution of this script gives
k= 1
-1/4/(2-2*cos(3*t))^(1/2)/b 4b 2 2cos3t
Copyright Edward B. Magrab 2009 83
An Engineers Guide to MATLAB Chapter 1
Then
2-2*cos(3*t) 2 1 cos3t 4sin 2 3t / 2