Title: Chapters 4-3-5 MATLAB Basics Matrices Built-In Functions Plotting
1Chapters 4-3-5MATLAB BasicsMatricesBuilt-In
FunctionsPlotting
2Arrays Vector Matrix
3Array Fundamentals Create A2, 4, 10 16, 3,
7 ones(n) gt nxn matrix eye(n) gt nxn
Address A(2,3) A(,3) A(2,) a(25)
A() Edit A(2,3)6
4(No Transcript)
5Initializing Variables/Arrays 1. Assignment
statement A1 2 3 4 A1,2 3,4 2.
Keyboard (Colon Operator) vfirstincrlast
Transpose gtgt v14 input functiongtgtVinput(d
ata)3. Read data file load command
(MAT-file)
6(No Transcript)
7Data File Commandsload gt loads data from a file
into the current workspace.
Form load filenamesave gt saves data from
current workspace into a file
Form save filename var1 var2 var3Data shared
with other programs use ascii option eg. save
arr.dat arr ascii produces arr.dat file
8(No Transcript)
9Initializing Variables Functionszeros(n) gt n
x n matrix of zeroszeros(n,m) gt n x m matrix of
zerosones(n) gt n x n matrix of
onesones(n,m) gt n x m matrix of onessize(arr)
gt returns number of rows
columns in arreye(n) gt n x n identity
matrixeye(n,m) gt n x m identity
matrixlength(arr) gt returns the length of a
vector or longest dimension
10MATLAB Initializing ExampleFor vector v, create
6 elements in decreasing value of 2 where the
first element is 10 and the last is 0.10, 8, 6,
4, 2, 0gtgt
11Appending Example BA r gtgt appends vector r
to matrix A increases columns BAr gtgt
appends vector r to matrix A increases rows
12MATLAB Initializing ExampleAppend vector a to
vector b to create vector c.Append vector a to
vector b to create matrix D. a elements 1
3 5 b elements 7 9 11
13MATLAB Initializing ExampleAssume the following
matrix X is stored in the current
workspace 2 4 8 6X 1 7 3 4 3 5 6 9 1 2 4 5
Create a new matrix Y from X which will produce
the following matrix Y 1 7 3 3 5 6
14Array (element-by-element) OperationsSymbol
Operation Form Example
Scalar-array addition A b
6,328,5 - Scalar-array subtraction
A b 8,3-53,-2 . Scalar-array
multiplication A.b 3,5.26,10 ./
Scalar-array right division A./b
4,8./44/4,8/4 Array addition
A B 6,54,810,13 -
Array subtraction A B 6,5 4,82,-3
. Array multiplication A.B
3,5.4,812,40 ./ Array right
division A./B 2,5./4,82/4,5/8
.\ Array left division A.\B
2,5.\4,82\4,5\8 . Array
Exponentiation A.B 3,5.29,25
2.3,58,32
3,5.2,49,625
15Array Operation ExampleThe following table gives
data for the distance traveled along (5) truck
routes and the corresponding time required to
traverse each route. Use the data to compute the
average speed required to drive each route. Find
the route that has the highest average speed.
(Hint x,kmax(a) returns the largest element
in a to x the row index for that value to
k.)Route gtgt 1 2 3 4 5
Distancegtgt 560 440 490 530
370Time(hrs)gtgt 10.3 8.2 9.1 10.1 7.5
16Array Operation ExampleThe current i passing
through an electrical resistor having a voltage v
across it is given by iv/r, where r is the
resistance. The power dissipated in the resistor
is given by v2/r. The following table gives data
for the resistance and voltage for (5) resistors.
Use the data to compute the current in each
resistor and the power dissipated. 1 2
3 4 5 r(ohms) 104
2x104 3.5x104 105 2x105 v(volts)
120 80 110 200 350
17Matrix OperationC A x B (n x n)
n ci,j ? ai,k bk,j
k1
18Matrix Operations Symbol Operation
Form Example Scalar-matrix addition
A b 6,328,5 - Scalar-matrix
subtraction A b 8,3-53,-2
Scalar-matrix multiplication Ab
3,526,10 / Scalar-matrix right division
A/b 4,8/44/4,8/4 Matrix
addition AB 6,5
4,810,13 - Matrix subtraction AB
6,5 4,82,-3 Matrix
multiplication AB
1,23,41,31,13,57,13 /
Matrix right division A/B A
inv(B) 1,23,41,31,1.5,.5.5,2.5
\ Matrix left division A\B
inv(A) B
1,23,41,31,1-1,-51,4
19(No Transcript)
20Product Cost Analysis ExampleThe following 2
tables show the costs production volume for 4
quarters associated with 4 products. Find the (1)
total quarterly costs the (2) total costs for
the year for each of the following categories
materials, labor, transportation. (Use 4 lines
of code) Unit costs (x103) Qtr.
Production VolumeProduct Mat Labor Trans
Q1 Q2 Q3 Q4 (1) 6
2 1 10 12 13 15
(2) 2 5 4
8 7 6 4 (3) 4
3 2 12 10 13
9 (4) 9 7 3
6 4 11 5
21Hierarchy of Arithmetic Operations1. Parentheses
(innermost to outward)2. Exponentials (L to
R)3. Multiplication/Division (L to R)4.
Addition/Subtraction (L to R)
22Predefined Special Operators pi ? i, j
i(?-1) eg. 2i 2 ?-1 Inf Infinity
NaN Not-a-Number clock Vector
y,m,d,hour,min,sec date Current date eg.
13-Feb-2004 ans Stores the result of an
expression
23Mathematical Functions abs(x) absolute
value cos(x) cosine (rad) sin(x) sine
(rad) tan(x) tangent (rad) acos(x) arc
cosine (rad) asin (x) arc sine (rad)
atan(x) arc tangent (rad) angle(x) Phase angle
(rad) exp(x) ex log(x) Natural Log (ln x)
log10(x) Base 10 Log sqrt(x) Square Root
mod(x,y) Remainder of x/y
24(No Transcript)
25Rounding Functionsround(x) Rounds to nearest
integer ceil(x) Rounds up to nearest
integerfloor(x) Rounds down to nearest
int.fix(x) Rounds to nearest int. towards 0
26Relational Operators lt Less than lt Less
than or equal to gt Greater than gt Greater
than or equal to Equal to Not equal to
27Polynomial Functionsroots(x) Roots of a
polynomial (column array form) poly(x)
Coefficients of the polynomial whose roots
are specified by the array x. polyval(a,x)
Evaluates a polynomial at specified values
of its independent variable x. Conv(a,b)
Computes the product of two polynomials
described by the coefficient arrays a and b.
28Characters in fprintf Display Function d
Integer format e Exponential format
f Floating point format g Floating
point or exponential, whichever is
shorter \n Skip to a new line
29Format Commandformat short gt 4 digits after
decimalformat long gt 14 digits after
decimalformat short e gt 5 digits plus
exponentformat long e gt 16 digits plus
exponentformat bank gt Two decimal digitsformat
rat gt approximate ratio
30x-y plotting plot(x,y) gt plots any pair of
vectors x y versus each
other gt both vectors must have
the same length gt single
curve gt auto-scale spacing for
each axis
31(No Transcript)
32(No Transcript)
33x-y plotting with fplot fplot(y,xmin xmax) gt
x must be used for the independent
variable. gt automatically analyzes the
function sets the plotting points so
all features of the function are shown.
gt y must be a string describing the
function.
34(No Transcript)
35Multiple x-y plotsplot(x,y,u,v) gt plots
4-arrays y versus x v versus
u
36(No Transcript)
37Plotting Polynomialsplot(x,polyval(a,x)) gt
plots the polynomial at specified values
of the independent variable.
38(No Transcript)
39Logarithmic Scale Plottingplot(x,y) gt Both
xy on linear axessemilogx(x,y) gt x on
logarithmic axessemilogy(x,y) gt y on logarithmic
axesloglog(x,y) gt Both xy on log axes
40(No Transcript)
41(No Transcript)
42Specialized Plot Commandsbar(x,y) gt
Bar Chartpolar(theta,r) gt Polar plot
stairs(x,y) gt Stairs plotstem(x,y) gt
Stem plot
43(No Transcript)
44(No Transcript)
45(No Transcript)
46(No Transcript)
47Subplotssubplot(m,n,p) gt displays several
plots in the same window figure gt m
number of rows gt n number of columns gt p
pane position for next plot
48(No Transcript)
49Three Dimensional Line Plotsplot3(x,y,z) gt
plots function of two independent
variables x y
50(No Transcript)
51Label, Title, Legend Commands for Plotting
gtgtxlabel(string) gtgtylabel(string)
gtgttitle(string) gtgtlegend(str1,str2,,0)
gtgtgrid on
52Colors, Marker Styles, Line Styles
gtgtplot(x,y,attributestring) Up to 3
components allowed gtgt Line Color
gtgt Marker Style gtgt Line
StyleColor Markers Lines r (red) .
(point) - (solid) b (blue) o (circle)
(dotted) g (green) (star) -- (dashed)
53(No Transcript)
54Plot Commandsgtext(string) gt Place the
string at a point specified by the mouse
hold on gt Freezes the current plot for
subsequent graphics command
55(No Transcript)
56Stream (Text) Modifiers for Plotting \bf gt
Boldface \it gt Italics \fontnamefontname
\fontsizefontsize _xxx gt Subscripts
xxx gt Superscripts \Gamma gt ? \lamda gt
? \rm gt Restore normal font
57(No Transcript)