Вы находитесь на странице: 1из 49

Announcements

our final exam is the last week of class, in your


lab period
Lab 10 will be our final and third-graded lab

m files
type commands into MATLABs "notepad"
load/save as usual
two ways to run:
hit the PLAY button
Type the filename at the command prompt
(without the .m)
rand
I = rand(1,3) % rand(m,n) gives m*n matrix
of uniformly distributed random numbers
from 0 - .9999

>> I = rand(1,3)

I =
0.8147 0.9058 0.1270
>> A = [rand(1,3); rand(1,3)*10; rand(1,3)*100 ]

A =
0.8235 0.6948 0.3171
9.5022 0.3445 4.3874
38.1558 76.5517 79.5200

Create a 3 by 3 matrix with each element a random
value between 0 and 9
rand( 1 ) create a 1 by 1 matrix with random values(0, 1)
rand(3, 3) create a 3 by 3 matrix with random values(0,
1)
Create a 3 by 3 matrix with each element a random
value between 0 and 9
rand( 1 ) create a 1 by 1 matrix with random values(0, 1)
rand(3, 3) create a 3 by 3 matrix with random values(0,
1)
rand(3,3)*10 create a 3 by 3 matrix with random
values(0, 10)
the single dimension
z = rand( 10 ) gives a 10 x 10 matrix

q = ones( 10 ) gives a 10 x 10 matrix
transposing - swap rows and columns
A = [ 16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1 ]
>> B = transpose(A)
B = [16 5 9 4
2 11 7 14
3 10 6 15
13 8 12 1 ]

transposing a row makes it a
column
>> x = [0:6]
x =
0 1 2 3 4 5 6
>> y = transpose(x)
y =
0
1
2
3
4
5
6

>>
transposing a column makes it a row
y =
0
1
2
3
4
5
6
>> z = transpose(y)
z =
0 1 2 3 4 5 6

>>
transposing a 2 x 5 matrix
>> A = [ 5 7 9 0 12
16 3 44 1 8 ]
A =
5 7 9 0 12
16 3 44 1 8
>> B = transpose(A)
B =
5 16
7 3
9 44
0 1
12 8

>>
2 x 5
5 x 2
Individual Matrix elements
Let's start with the simple case of a vector and
a single subscript. The vector is
v = [16 5 9 4 2 11 7 14]
The subscript can be a single value.
v(3) % Extract the third element
ans =
9
Colons work:
v(1:4)
ans =
16 5 9 4
Now consider indexing into a matrix.
A = [ 16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1 ]
indexing in matrices is done using two subscripts -
one for the rows and one for the columns.
A(2,4) % Extract the element in row 2, column 4
ans
= 8
who and whos
who shows your variables
whos lists your variables and their
characteristics

clc and clear
clc clears the command window
clear erases all variables
e.g. surface plot
x=[1:10]
y=transpose(x)
%matrix mult:
z= y * x
figure(1)
surf(x,y,z)
figure(2)
z = rand(10)
surf(x,y,z)


matrix multiplication?
it's an odd operation
matrices can be multiplied if their inside
dimensions match:
(m x n) * (n x q)
e.g. a (5 x 4) CANNOT multiply a (2 x 3)
the resulting matrix has the outside
dimensions:
(m x n) * (n x q) = (m x q) matrix
We'll learn how

.mat files and
the SAVE command
http://www.mathworks.com/help/matlab/ref/sa
ve.html

saves all the variables and current values in a
.mat file
different than saving a script file
more like saving your desktop, or workspace,
not your commands

the SAVE command
http://www.mathworks.com/help/matlab/ref/sa
ve.html

saves all the variables and current values in a
.mat file
different than saving a script file
more like saving your desktop, or workspace,
not your commands

file extension .mat is automatic
save myStuff % saves all variables to myStuff.mat

load myStuff % loads variables from myVars.mat

Matrix Math, MATLAB, and data
tables
Reading
Chapter 9 & 10 for Matrix Math,
Chapter 11 for data interpolation

3 equations in 3 unknowns
1 equation: 3 x + 4y + 16z = 12
many solutions (x=0, y=3, z=0 / x=4, y=0, z=0 / etc. )

2 equations: 3 x + 4y + 16z = 12
6x - 17y - 23z = -168
solutions become limited...

3 equations: 3 x + 4y + 16z = 12
6x - 17y - 23z = -168
x + 42y + 101 z = 140
one solution only: x = 62.0129 y = 70.2452 z = -28.4387

matrices
usually represent a table, or a data relationship
or - referring to C++ - correlated arrays

3 x + 4y + 16z = 12 3 4 16 12
6x - 17y - 23z = -168 6 -17 -23 = -168
x + 42y + 101 z = 140 1 42 101 140

3 equations in 3 unknowns, represented by matrices
row and column vectors
a row vector: a b c d e f g h
usually reflects different variables

a column vector: a
b
c
d
e
f
g

usually reflects the SAME
variable
matrix dimensions: R x C
given as Rows x Columns
1 56 98 3 0 1 x 5
45
67 3 x 1
94

101 46 2 x 2
3 17

matrix multiplication?
it's an odd operation, not just multiplying
corresponding elements
matrices can be multiplied if their inside
dimensions match:
(m x n) * (n x q)
e.g. a (5 x 4) CANNOT multiply a (2 x 3)
the resulting matrix has the outside
dimensions:
(m x n) * (n x q) = (m x q) matrix


matrix multiplication
simple
A * B = C

each Row in A x each Col in B =
(Row,Col) item in C

a
11
b
12
+ a
12
b
22
= c
12



1 2 3 * 1 2 = 22 28
4 5 6 3 4 49 64
5 6

2 X 3 * 3 X 2 results in a 2 x 2
inner dimensions must be the same
out dimensions reveal size
row 1 column 1 item (1, 1)
1*1 + 2*3 + 3*5 = 22
Watch the video 5.4
Harvey explains how to multiply matrices
3 equations in 3 unknowns
x + y z = 4 has many solutions
(e.g. 0,0,-4 or 0,4,0 or 2,2,0 etc...)
two equations together....
x + y z = 4 have fewer solutions
x 2y + 3z = 6

but three equations in three unknowns
x + y z = 4
x 2y + 3z = 6 has exactly one solution
2x + 3y + z = 7

represent N equations in N unknowns
1) x + y z = 4
2) x 2y + 3z = 6
3) 2x + 3y + z = 7

1 1 -1 x 4
1 -2 3 * y = -6
2 3 1 z 7

A = [1 1 -1; 1 -2 3; 2 3 1] % called coefficient matrix
[ x; y; z ] % called the variables matrix
C = [ 4; -6; 7 ] % called the constants matrix


the identity matrix
a square matrix with 1s in the diagonal and 0s
everywhere else
I = eye(3) % yields a 3x3 Identity Matrix

the Identity Matrix
(1s in the diagonal)
Any matrix times an appropriately sized identity
matrix yields itself
3x2 2x2 3x2
23 45 1 0 23 45
17 22 * 0 1 = 17 22
1 32 1 32
Size of ID matrix: SQUARE, dictated by
COLUMNS of the multiplying matrix

what is a matrix inverse?
A matrix multiplied by it's Inverse yields the
identity matrix
A
inv *
A = Identity
"Singular" matrices have no Inverse


Why?
1) x + y z = 4
2) x 2y + 3z = 6
3) 2x + 3y + z = 7


multiply both sides by
A
Inv
:



1 1 -1 x 4
1 -2 3 * y = -6
2 3 1 z 7

x 4
A
I
* A * y = A
I
* -6
z 7

find A
Inv,
and you can solve
for x, y, z
A
watch Harvey explain Matrix Math
in Video 5.1
let's try one
The admission fee at a small fair is $1.50 for children and $4.00 for
adults. On a certain day, 2200 people enter the fair and $5050 is
collected. How many children and how many adults?

Lets call adults x and children y
2200 people attend the fair x + y = 2200
Child admission fee is $1.50, Adult admission fee is $4.00
The total amount collected is $5050.00: 4x + 1.5y = 5050
non-matrix way:
Take the first equation and set it equal to x(subtract y from both sides x + y - y = 2200 - y
x = 2200 - y
Now since we have shown that x equals 2200-y we can substitute that for x in the
second equation and solve for y
4x + 1.5y = 5050
4(2200-y) + 1.5y = 5050

8800 - 4y + 1.5y = 5050
combine like terms: 8800 - 2.5y = 5050
subtract 8800 from both sides: 8800 - 8800 - 2.5y = 5050 - 8800
we have: -2.5y = -3750
divide both sides by -2.5: -2.5y/-2.5 = -3750/-2.5
y = 1500
Answer: 1500 children attended the fair
Now use this answer to find x
Take the first equation and substitute y with 1500
x + y = 2200
x + 1500 = 2200
x + 1500 - 1500 = 2200 - 1500
x = 700
Answer: 700 adults attended the fair

or.
x + y = 2200 1 1 x = 2200
4x + 1.5y = 5050 4 1.5 y 5050

find the Inverse of A: -.6 .4
1.6 -.4

-.6 .4 1 1 x = -.6 .4 2200
1.6 -.4 4 1.5 y 1.6 -.4 5050
x 700
y = 1500

m file

summary
matrix multiplication (and its vector equivalent, the
"dot product") is essentially a transformation which
combines properties.
"dividing by a matrix" is only possible by multiplying
by its inverse (i.e. dividing by 5, is the same as
multiplying by .20, or 1/5, which is the inverse of 5).
element-by-element multiplication is called the
Hadamard product, (in MATLAB " .* ") and is used in
compressing JPEGs, where display properties are
represented by a matrix, .
Use matrices in MATLAB to solve the following problem. You
must display all matrices and the resultant 3-digit number as
output.
Consider a three-digit number given as xyz. For example, if
the number were 123, x would be 1, y would be 2, and z
would be 3. Remember that the number represented by xyz is
actually (x*100) + (y*10) + z ...
If you add up the digits of a 3-digit number, the sum is 11.
If the digits are all reversed, the new number is 46 more than
5x the old number.
The hundreds digit plus twice the tens digit is equal to the
units digit.
What is the number?

numbers...

If you add up the digits of a 3-digit number, the
sum is 11.

x + y + z = 11
If the digits are reversed, the new number is 46 more
than five times the old number.

note: xyz is really 100x + 10y + z

100z + 10y + x = 5(100x + 10y + z) + 46
100z + 10y + x = 500x + 50y + 5z +46
(100z - 5z) + (10y -50y) + (x - 500x) = 46
95z -40y -499x = 46

-499x -40y +95z = 46




The hundreds digit plus twice the tens digit is
equal to the units digit.

x + 2y = z

1x + 2y -1z = 0


x + y + z = 11
-499x -40y +95z = 46
1x + 2y -1z = 0


1 1 1 x 11
-499 -40 95 * y = 46
1 2 -1 z 0

Answer is 137
A = [ 1 1 1 ; -499 -40 95; 1 2 -1]
Z = [11; 46; 0]

A_I = inv (A)
Ans = A_I * Z
x = Ans(1,:)
y = Ans(2,:)
z = Ans(3, :)

Вам также может понравиться