Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
EPSII
59:006
Spring 2004
Outline
Managing Your Session
File Usage
More on Matrices
Review of Matrix Algebra
Dot Product
Matrix Multiplication
Matrix Inverse
Solutions to Systems of Linear Equations
Saving Workspace
Loading Data Files
Creating M-files
Solutions Using Matrix Inverse
Solutions Using Matrix Left Division
Plotting
Managing Your Session
Command
Description
casesen
“Casesen off” turns off case sensitivity
clc
clear
exist(‘name’)
help name
Clears the command window
Removes variables from memory
Does a file or variable have that name?
Help on a topic
lookfor name
quit
who
Keyword search in help
Terminates your session
Lists variables in memory
whos
Lists variables and their sizes
Space Workspace File named
myspace.mat
Step 1 – Type
save
myspace at prompt or
Step 2 – choose File and the
Step 3 – Save Workspace As
To restore a workspace use load
load
myspace
Matlab M-Files
Can also create a Matlab file that contains a
program or script of Matlab commands
Useful for using functions (Discuss later)
Step 1 – create a M-file defining function
Step 2 – make sure the M-file is in your MATLAB
path (Use Current Directory menu or Use View
and check Current DIerectory)
Step 3 – call function in your code
Matlab M-Files Example
Calculate the integral
an M-file called g.m is created and contains the
following code
function f=g(x)
f = x.^2 - x + 2
at the Matlab prompt write
>> quad('g',0,10)
ans=
303.3333
help g - Displays the comments in the file g.m
Data File Handling in MATLAB
Uses MAT-files or ASCII files
MAT-files Useful if Only Used by Matlab
save <fname> <vlist> -ascii (for 8-digit text format)
save <fname> <vlist> -ascii –double (for 16-digit text format)
save <fname> <vlist> -ascii –double –tabs (for tab delimited
format)
Matlab can import Data Files
To read in a text file of numbers named file.dat:
A = load(‘file.dat’); (dat recommended extension)
The file ‘file.dat’ should have array data in row, col format
Each row on a separate line
More on Matrices
An m x n matrix has m rows and n columns
Aij refers to the element in the ith row and jth
column.
A = [1 2 3; 4 5 6]
Instead of using numbers as element in the
matrix, you can use other vectors or matrices
A’ is the transpose of A (its rows and columns
are interchanged.
Matrix Operations
r*A a scalar times an array
A+B array addition (arrays must be the
same size)
A.*B element by element multiplication of
arrays
A*B dot product multiplication
A./B element by element right division
Matrix Transposition
The transpose operator (‘) switches rows
and columns
Row vectors become column vectors and
vice versa
1
4
1 2 3
2 5
4 5 6 3 6
A(2,3)
A(3,2)
Array Addressing
V(:) means all the elements of V
V(2:5) means elements 2 through 5
A(:,3) means all the elements of the 3rd
column
A(:,2:5) means all the elements of the 2
through 5th columns
Example
5
8
A
6
9
Given:
What is A(2:3,1:2)?
9
2
2
2
3
3
1
1
2
4
3
2
Review of Matrix Algebra
Dot Product
Matrix Multiplication
Matrix Inverse
Solutions to Systems of Linear Equations
Solutions Using Matrix Inverse
Solutions Using Matrix Left Division
Dot Products
The dot product is the sum of the
products of corresponding elements in
two vectors.
n
Dot product = A*B = ai bi
i 1
Matlab Commands (either will work)
sum(A.*B)
dot(A,B)
Matrix Multiplication
N
ci , j aik bkj
k 1
A = [1 2 3; 0 1 0], B = [2 1; 0 0; 1 2]
C = A*B
c(1,1) = 1(2) + 2(0) + 3(1) = 5
1
2
3
2
1
0
1
0 *
0
0
1
2
=
5
7
0
0
What Size Matrices Multiply?
Write the matrix dimensions in normal form:
(A_rows, A_columns) (B_rows, B_columns)
If the inside values are
the same, the matrices
can be multiplied. They
are conformable for
multiplication.
(3,4)(4,16) -> (3,16)
(2,12)(12,19)->(2,19)
columns
The result will
have this size.
rows
The Identity Matrix and Powers
Let I bet the identity matrix
For any matrix X, X*I = X = I*X
I = [1 0 0; 0 1 0; 0 0 1], for a 3x3 matrix
In general I can be any square size with
the diagonal elements = 1, all others = 0
To multiply a matrix by itself, use X^2, or,
in general, X^n
Inverse Matrices
Let A-1 be the inverse matrix of A
Then, A-1A = AA-1 = I
2 1 1.5 0.5 1 0
1 0 1
4 3 2
Before computers, finding inverses was a pain in
the neck
Not all matrices have inverses
Determinants
Most of the techniques to find inverses,
and many other techniques that use
matrices, at some point or other require
determinants. These are special functions
performed on matrices
If A is a 2x2 matrix, then the determinant
is:
Det(A) = a(1,1)*a(2,2) – a(1,2)*a(2,1)
Determinants for 3x3 Matrices
A [ 1 2 3; 4 5 6; 7 8 9]
Det(A) =
+ a(1,1)*(a(2,2)*a(3,3) – a(2,3)*a(3,2))
- a(1,2)*(a(2,1)*a(3,3) – a(2,3)*a(3,2))
+ a(1,3)*(a(2,1)*a(3,2) - a(2,2)*a(3,1))
1
0
2
1
2
1
2
0
1
=2 -0 -8=-6
Solution of Linear Equations
Perhaps the best thing about matrices is
how well they deal with systems of
simultaneous linear equations
3x
-x
x
+2y
+3y
-y
3
2 -1
-1 3 2 *
1 -1 -1
-z
+2z
-z
x
y
z
=
= 10
=5
= -1
10
5
-1
A*X = B
We Can Solve This In 2 Ways
A*X = B
One way is with A inverse
A-1A*X = A-1B => I*X = A-1B => X = A-1B
Another way is with Left Multiplication
(which is similar, but uses a better
numerical technique)
X = A\B’
Why Do Some Matrices Not
Have Inverses?
X
X
+2y
+2y
=0
=0
X
2X
+2y
+4y
=0
=0
1
2
1
2
1
2
2
4
A=
A=
X = -2y,
No solution
X = -2y,
No solution
An Example
x 1 – x2 – x3 – x4 = 5
x1 + 2x2 + 3x3 + x4 = -2
2x1 + 2x3 + 3x4 = 3
3x1 + x2 + 2x4 = 1
A = [1 –1 –1 –1;1 2 3 1; 2 0 2 3; 3 1 0 2]
B = [5 –2 3 1]
X = inv(A)*B’ or
X = A\B’
Some Useful Matrix Commands
Command
Description
mean(b)
Finds average of each column of b
max(b)
Finds max of each column of b
min(b)
Finds min of each column of b
sort(b)
Sorts each column in b, ascending
sum(b)
Sums each column in b
If A =
5
8
6
9
9
2
2
2
3
3
1
1
2
4
3
2
B = mean(A) = 7.0 3.75 2.0 3.75
C = max(A) = 9 9 3 4
D = min(A) = 5 2 1 2
E = sum(A) = 28 15 8 11
If A =
5
8
6
9
9
2
2
2
3
3
1
1
2
4
3
2
S = sort(A) = 5
6
8
9
2
2
2
9
1
1
3
3
2
2
3
4
Plotting
Plotting Basics
2D Plots (x,y)
y=f(x)
plot(x,y)
3D Plots (x,y,z): a.k.a. “surface plots”
plot3(x,y,z)
Basic Commands
plot(x,y)
xlabel(‘Distance (miles)’)
ylabel(‘Height (miles)’)
title(‘Rocket Height as a Function of
Downrange distance’)
grid
axis([0 10 0 100])
clf
% clear current figure
Subplots
Subplots
subplot(m,n,p)
m = the number of figure rows
n = the number of figure columns
Divides
figure window into an array of
rectangular frames
Comparing data plotted with different axis
types
Plot Customization
Data Markers/Line Types/Colors
Labeling Curves/Data
Hold Function
hold
on
when a plot requires 2+ plot() commands
Special Plot Types
Logarithmic Plots
Tick Mark Spacing and Labels
loglog(x,y)
semilogx(x,y)
semilogy(x,y)
set(gca,’XTick’,[xmin:dx:xmax], ’YTick’,[ymin:dy:ymax])
set(gca,’Xticklabel’,[‘Jan’,’Feb’,’Mar’])
Axis
axis( [ xmin, xmax, ymin, ymax ] )
Stem, Stairs, Bar Plots
Polar Plots
Plot bells & whistles
Various line types, plot symbols and colors may be obtained with PLOT(X,Y,S)
where S is a character string from any or all the following 3 columns:
S = ‘ linetype color shape of data annotation ‘
Colors
Data symbol
Line type
b blue
g green
r red
c cyan
m magenta
y yellow
k black
.
o
x
+
*
s
d
v
^
<
>
p
h
:
-.
--
point
circle
x-mark
plus
star
square
diamond
triangle (down)
triangle (up)
triangle (left)
triangle (right)
pentagram
hexagram
solid
dotted
dashdot
dashed
Plot(X,Y,S) examples
PLOT(X,Y,'c+:') plots a cyan dotted line with a plus at each data point
PLOT(X,Y,'bd') plots blue diamond at each data point but does not draw
any line.
PLOT(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by
the (X,Y,S) triples, where the X's and Y's are vectors or matrices
and the S's are strings.
For example, PLOT(X,Y,'y-',X,Y,'go') plots the data twice, with a
solid yellow line interpolating green circles at the data points.
Function Discovery
The process of determining a function that
can describe a particular set of data
LINEAR
POWER
EXPONENTIAL
polyfit() command
3D Plots
3D Line Plots
Surface Mesh Plots
plot3(x,y,z)
meshgrid()
mesh(), meshc()
surf(), surfc()
Contour Plots
meshgrid()
contour()
Data m-file
y = 1:5;
x = 1:12;
for i = 1:5
for j = 1:12
z(i,j) = i^1.25 * j ;
end;
end;
data
x = 1
y = 1
z=
1.0000
2.3784
3.9482
5.6569
7.4767
2
2
3
3
4
4
5
5
6
7
8
2.0000 3.0000 4.0000
4.7568 7.1352 9.5137
7.8964 11.844 15.792
11.313 16.970 22.627
14.953 22.430 29.907
9
5.0000
11.892
19.741
28.284
37.383
Columns 8 through 12
8.000
19.02
31.58
45.25
59.81
9.000
21.405
35.534
50.911
67.290
10.000
23.784
39.482
56.568
74.767
11.000
26.162
43.430
62.225
82.244
10
12.000
28.541
47.378
67.882
89.720
11
12
6.0000
14.270
23.689
33.941
44.860
7.0000
16.6489
27.6376
39.5980
52.3372
3-D demo m-file
surf(x,y,z)
pause
mesh(x,y,z)
pause
waterfall(x,y,z)
pause
waterfall(y,x,z’)
pause
bar3(y,z)
pause
bar3h(y,z)
Reduced mesh
waterfall(x,y,z)
Title:
Wat_fallA.eps
Creator:
MATLAB, The Mathw orks, Inc.
Prev iew :
This EPS picture w as not s av ed
w ith a preview inc luded in it.
Comment:
This EPS picture w ill print to a
Pos tSc ript printer, but not to
other ty pes of printers.
waterfall(y,x,z')