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, * Multiplication, / Division, ^ Power ( ) Specify evaluation order 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 z = 21 x := 5^2/2 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
C := A + B produces C, where the i th row and j th column are just the addition of the elements (numbers) in the i th row and j th column of A and B Given:!!" #, and " ' ( ) $%!! *!+!' so that the addition is : #,-!. " " $!!!#!% '" The Mathcad commands to perform these matrix assignments and the addition are: 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! #'%,! $ # ' 1x3 becomes ==> 3x1 % 2-D " */! 0 (/# 0 $/), " $ */! "/' 0 (/# "/! 2x3 becomes ==> 3x2 "/' "/! "/% 0 $/) "/% 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
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 i th row and k th column spot in C as the scalar results of the dot product of the i th row in A with the k th 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 ) '#" 1 * '1).#1*."1$ $" DOT PRODUCT OF ROW AND COLUMN 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!!!"%!)$!+!! 2x3 3x2 2x2 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
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
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) ( ) 2 4 a T = 25 16 Lecture 2 Mathcad basics and Matrix Operations page 15 of 18
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
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., CGN 3421, August 27, 2001 This program demonstrates basic program structure INPUT SECTION x:= 2 15 8 4 CALCULATION SECTION ( ) 2 y := x + x CREATE scalar y as a function 1 4 of x1 and x4 40 z:= x 2 + y 261 CREATE z vector from x and y z = 100 NOTE that x^2 operates on each value in x 52 OUTPUT SECTION 300 z 200 100 0 0 5 10 15 x graph of z vs. x NOTE that the plot plots the x values in order of appearance it does not reorder and sort Lecture 2 Mathcad basics and Matrix Operations page 17 of 18
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 := x + 1 x CREATE scalar y as a function 4 of x1 and x4 z := for i zz zz i 1.. length() x ( x i ) 2 + y z = 40 261 100 52 CREATE z vector from x and y Another example of vector operation Plot the following function over the range / '. ", 0 ', '. +/', ", 0 #!+ yx ():= 2+ 3x 2x 2 + 0.2x 3 CREATE FUNCTION x:= 5, 4.5.. 10 CREATE Range Vector 50 yx ( ) 0 50 PLOT of function 100 5 0 5 10 x Lecture 2 Mathcad basics and Matrix Operations page 18 of 18