Download Lecture 2 Mathcad basics and Matrix Operations - essie-uf

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Euclidean vector wikipedia , lookup

Quadratic form wikipedia , lookup

System of linear equations wikipedia , lookup

Basis (linear algebra) wikipedia , lookup

Linear algebra wikipedia , lookup

Tensor operator wikipedia , lookup

Eigenvalues and eigenvectors wikipedia , lookup

Jordan normal form wikipedia , lookup

Determinant wikipedia , lookup

Bra–ket notation wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Cartesian tensor wikipedia , lookup

Matrix (mathematics) wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Four-vector wikipedia , lookup

Matrix multiplication wikipedia , lookup

Matrix calculus wikipedia , lookup

Transcript
CGN 3421 - Computer Methods
Gurley
Lecture 2 Mathcad basics and Matrix Operations
Announcements
No class or lab Wednesday, 8/29/01
I will be posting a lab worksheet on the web site on Tuesday for you to work through on your own.
Operators
+ Addition, Subtraction, *
( ) Specify evaluation order
Multiplication,
/
Division,
^
Power
Order of Operations
() ‘
^
highest level, first priority
* /
next priority level
+ -
last operations to be performed
y := 2
x := 3 * y^2
x = 12
y := 2
x := (3*y)^2
x = 36
y := 2
x := 3 * y + 2
x = 8
z := 3*6+6*2/4
x := 5^2/2
z = 21
x = 12.5
Matrix operations:
Mathcad is designed to be a tool for quick and easy manipulation of matrix forms of data. We’ve seen the
matrix before in Lecture 1 as a 2-D array. That is, many pieces of information are stored under a single
name. Different pieces of information are then retrieved by pointing to different parts of the matrix by
row and column. Here we will learn some basic matrix operations: Adding and Subtracting, Transpose,
Multiplication.
Adding matrices
Add two matrices together is just the addition of each of their respective elements.
If A and B are both matrices of the same dimensions (size), then
Lecture 2 Mathcad basics and Matrix Operations
page 11 of 18
CGN 3421 - Computer Methods
Gurley
C := A + B
produces C, where the ith row and jth column are just the addition of the elements (numbers) in the ith row
and jth column of A and B
Given: ! & ! " # , and " & ' ( )
$ % !!
* !+ !'
" $ !!
!# !% '"
The Mathcad commands to perform these matrix assignments and the addition are:
so that the addition is :
#,- & ! . " &
A := Ctrl-M (choose 2 x 3) 1 3 5 7 9 11
B := Ctrl-M (choose 2 x 3) 2 4 6 8 10 12
C := A + B
C =
Rule:
A, B, and C must all have the same dimensions
Transpose
Transposing a matrix means swapping rows and columns of a matrix. No matrix dimension restrictions
Some examples:
! & # ' % ,
1-D
2-D
" & */! 0 (/# 0 $/) ,
"/' "/! "/%
!
$
#
& '
%
"
$
1x3 becomes ==> 3x1
*/! "/'
& 0 (/# "/!
0 $/) "/%
2x3 becomes ==> 3x2
In general
" ( %, & ) & " $ ( &, % )
In Mathcad, The transpose is can be keystroked by Ctrl - 1 (the number one)
" & #")'
%*($
#
B Ctrl-1 = '() & "
)
'
Lecture 2 Mathcad basics and Matrix Operations
%
*
(
$
page 12 of 18
CGN 3421 - Computer Methods
Gurley
Multiplication
Multiplication of matrices is not as simple as addition or subtraction. It is not an element by element multiplication as you might suspect it would be. Rather, matrix multiplication is the result of the dot products
of rows in one matrix with columns of another. Consider:
C := A * B
matrix multiplication gives the ith row and kth column spot in C as the scalar results of the dot product of
the ith row in A with the kth column in B. In equation form this looks like:
2,34,536789:,;9,<
# %, * &
∑
! %, & ,1," &, *
&&!
Let’s break this down in a step-by-step example:
Step 1: Dot Product (a 1-row matrix times a 1-column matrix)
The Dot product is the scalar result of multiplying one row by one column
)
DOT PRODUCT OF ROW AND COLUMN
' # " 1 * & '1) . #1* . "1$ & $"1x1
1x3
$
3x1
Rule:
1) # of elements in the row and column must be the same
2) must be a row times a column, not a column times a row
Step 2: general matrix multiplication is taking a series of dot products
each row in pre-matrix by each column in post-matrix
# )
!(' 1
!1#.(1*.'1!+ ,,,,!1).(1!'.'1!! & #$ $)
* !' &
%"$
%1#."1*.$1!+ ,,,,%1)."1!'.$1!!
!"% !)$
!+ !!
2x2
2x3
3x2
C(i,k) is the result of the dot product of row i in A with column k in B
Matrix Multiplication Rules:
1) The # of columns in the pre-matrix must equal # of rows in post-matrix
inner matrix dimensions must agree
2) The result of the multiplication will have the outer dimensions
# rows in pre-matrix by # columns in post-matrix
Lecture 2 Mathcad basics and Matrix Operations
page 13 of 18
CGN 3421 - Computer Methods
Gurley
For this example, apply rules
C := A * B
A is nra x nca (# rows in a by # columns in a)
B is nrb x ncb
Rule 1 says:
nca = nrb or else we can’t multiply (can’t take dot products with
different number of terms in row and column)
Rule 2 says:
C will be of size nra x ncb
result C has outer dimensions
(+',,,(-',1,(+.,,,(-.
inner dimensions must agree
How to perform matrix multiplication in Mathcad???
Easy
A := [4 5; 2 1]
B := [9 1; 6 12]
C := A*B
Note: Now that we know how to enter a matrix into Mathcad, I’ll use a shortcut notation for these notes
as applied above, where:
A := [4 5; 2 1]
means a 2 x 2 array where the ; indicates the start of the next row. The ; is not actually used in Mathcad
for this purpose, its just a shorthand notation that I’ll use for these notes.
D := [12
34
56; 45
89
9]
is a 2 x 3 matrix using this notation
Note: If inner matrix dimensions don’t match, Mathcad can’t perform the operation since it violates the
rules of matrix multiplication, and you’ll get an error that says:
“the number of rows and or columns in these arrays do not match”
Lecture 2 Mathcad basics and Matrix Operations
page 14 of 18
CGN 3421 - Computer Methods
Gurley
example: Let’s try to multiply a 2x3 by another 2x3 (rules say we can’t do this)
A := [3 4 1 ; 0 4 9];
B := [2 9 5 ; 9 4 5];
C := A * B
Mathcad will tell you:
“the number of rows and or columns in these arrays do not match”
and won’t provide an answer
Since the # of columns in A was not equal to # of rows in B, we can’t multiply A * B
IMPORTANT: Another example: Say we create a 1-D vector x with the following:
x := [2 4 9 5 2];
Now say we want to square each number in x. It would seem natural to do this:
y := x^2
But Mathcad tells us:
“This Matrix must be square. It should have the same number of rows as
columns”
Note that y : = x^2 is the same as saying y := x*x
Mathcad by default will always interpret any multiplication as a standard dot product type matrix multiplication, thus we can’t take a dot product of two row vectors, since rules of matrix multiplication are violated in this case. The exception to this default assumption in Mathcad is if the vector is a column instead
of a row. In that case, Mathcad will assume you want to square each element in the vector rather that
apply standard matrix multiplication.
If we just want to square the numbers in x, we can do this:
y = x Ctrl-1 shift ^2
This first transposes the row vector into a column vector, then squares the elements in the vector
Try this out
a := ( 2 5 4 )
Lecture 2 Mathcad basics and Matrix Operations
(a )
T
2
4
=  25 
 
 16 
page 15 of 18
CGN 3421 - Computer Methods
Gurley
Practice matrix operations on the following examples.
List the size of the resulting matrix first. then perform the operations by hands. Use Mathcad to confirm
each of your answers.
'
% !' # 1 * &
"
% $
' ( ) &
" )
* !+ !'
$ !
(
% 1 '!* &
$
( " 1 * $ &
' ! % )
"+
! ( % 1
%" &
* ) (
($
$
(%* 1 ' $ ! &
$
 ( $

. # "  1 !+ ' $ &

 * %
+ !
) " #
We will consider the use of matrices to solve a number of different problems in the numerical methods
portion of the course.
Lecture 2 Mathcad basics and Matrix Operations
page 16 of 18
CGN 3421 - Computer Methods
Gurley
Fundamental Program Structure
Labeling the program using comments
program title
student information
program summary
executable statements
program input (load data from external files, assignment statements, etc.)
perform operations needed (sequential execution, loops, etc.)
display program output (graphs, numbers, output files, etc.)
intersperse comments to explain program
Example program #1
K. Gurley, CGN 3421, August 27, 2001
This program demonstrates basic program structure
INPUT SECTION
 2 
15
x :=  
8
4
 
CALCULATION SECTION
(1
y := x + x
)2
CREATE scalar y as a function
of x1 and x4
4
 40 
261 
z=
 100 
 52 
 
2
z := x + y
CREATE z vector from x and y
NOTE that x^2 operates on each value in x
OUTPUT SECTION
300
200
graph of z vs. x
z
100
0
NOTE that the plot plots the
x values in order of appearance
it does not reorder and sort
0
5
10
15
x
Lecture 2 Mathcad basics and Matrix Operations
page 17 of 18
CGN 3421 - Computer Methods
Gurley
Vector operations
Note in the previous example that z was created from the vector x and the scalar y. Mathcad knew how to
handle the combination based on its default assumptions, and the resultant variable z is a vector. This is
an example of a vector operation. That is, we did not have to write an algorithm to explicitly calculate
each of the values in z one at a time. However, we will see later that it is important to know how to operate on one element at a time within a vector or matrix.
In the previous example, we could have used a for loop to explicitly calculate each element of z, one at a
time. Simply replace the Calculation section with the following
CALCULATION SECTION
(
)2
y := x1 + x4
z :=
CREATE scalar y as a function
of x1 and x4
 40 
 261 
z=
 100 
 52 
 
for i ∈ 1 .. length ( x)
()
zzi ← xi
2
+y
zz
CREATE z vector from x and y
Another example of vector operation
Plot the following function over the range , & 0 # !+
'
/ & ' . ", 0 ', . +/',
"
2
3
y ( x) := 2 + 3⋅ x − 2⋅ x + 0.2x
CREATE FUNCTION
CREATE Range Vector
x := −5 , −4.5.. 10
50
0
PLOT of function
y ( x)
50
100
5
0
5
10
x
Lecture 2 Mathcad basics and Matrix Operations
page 18 of 18