Sei sulla pagina 1di 22
2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 MAPLE TUTORIAL for the Second Course in Applied Differential Equations. Part 2.1 Vladimir Dobrushkin Matrix Algebra -- with(linalg) This tutorial surves as an introduction to the computer algebra system Maple, created by MapleSoft ©. It is made solely for the purpose of education. This tutorial assumes that you have a working knowledge of basic Maple functionality. This includes knowing how to define variables and carry out algebraic manipulations. If you are completely new to Maple, then please go to the introductory tutorial for APMAQ330. Usually, the same algorithm can be implemented in many different ways to accomplish the same task. As a result, this, tutorial may introduce several different commands (or sets of commands) to do the same thing. Itis always good to be aware of these alternate pathways; however, you are welcome to pick up any approach whatever you like the best Finally, the commands in this tutorial are all written in red text (as Maple input), while Maple output is in blue, which means that the output is in 2D output style. You can copy and paste all commands into Maple, change the parameters and run them. You, as the user, are free to use the mw files and scripts to your needs, and have the rights to distribute this tutorial and refer to this tutorial as long as this tutorial is accredited appropriately. The tutorial accompanies the textbook Applied Differential Equations. The Primary Course by Vladimir Dobrushkin, CRC Press, 2015; hittp://www.crcpress.com/productlisbn/978 1439851043. Contents hide} Return to computing page for the first course APMA0330 Return to computing page for the second course APMA0340 Preface Return to Maple tutorial for the first course (APMA0330) 1. Part |: Matrix Algebra Return to Maple tutorial for the second course (APMA0340) 2. Part Il: Linear Systems of Ordinary Return to the main page for the course APMAQ340 Differential Equations Return to the main page for the course APMA0330 3, Part Ill: Non-linear Systems of Ordinary Differential Equations 4, Part IV: Numerical Methods 5, Part V: Fourier Series 6. Part VI: Partial Differential Equations Return to Matrix Algebra with(LinearAlgebra) Now we will explore topics from Linear Algebra. To begin, we will need to load the linear algebra package, Actually, Maple has two such packages: linalg and LinearAlgebra. The linalg package is older and considered obsolete; it was replaced by LinearAlgebra in Maple version 6 (in 1999). The LinearAlgebra package is more powerful and it is recommended to use. However, for our needs, linalg package is sufficient. This web page deals only with linalg package. The command to invoke a linear algebra package type: with(linalg): If the package is not loaded, then either a typed command will not be recognized, or a different command with the same name will be used. Note that the older Package linalg uses lower case letters. 2.1.1. How to Define and Operate on Vectors “Matrix” is the Latin word for womb. The origin of mathematical matrices has a long history. The term "matrix" in combinatorics was introduced in 1850 by the British mathematician James Joseph Sylvester, who also coined many mathematical terms or used them in a "new or unusual ways" mathematically such as graph, discreminant, annihilator, canonical form, minor, nullity, and many others, We start with a particular example of matrices--vectors, Avector is a quantity that has magnitude and direction and that is commonly represented by a directed line segment whose length represents the magnitude and whose orientation in space represents the direction. In mathematics, itis always assumed that vectors can be added or subtracted, and multiplied by a scalar (real or complex numbers). It is, also assumed that there exists a unique zero vector (of zero magnitude and no direction), which can be added/subtructed from any vector without changing the outcome. The zero vector is nat zero. Wind, for example, had hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl 122 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 both a speed and a direction and, hence, is conveniently expressed as a vector. The same can be said of moving objects, momentum, forces, electromagnetic fields, and weight. (Weight is the force produced by the acceleration of gravity acting on a mass.) The first thing we need to know is how to define a vector so it will be clear to everyone. One of the common ways to do this is to introduce a system of coordinates, either Cartesian or any other, which includes unit vectors in each direction, usually referred to as an ordered basis. In engineering, traditionally these unit vectors are denoted by i (abscissa), j (ordinate), and k (applicat), called the basis. Once rectangular coordinates (or a basis) are set up, any vector can be expanded through these unit vectors. In three dimensional case, every vector can be expanded as v= ni + mj + ak, where 1,2, are called the coordinates of the vector v. Coordinates are always specified relative to an ordered basis. Therefore, every vector can be identified with an ordered n-tupple of n real numbers or coordinates. The number of components in the vector is called its dimension. Coordinate vectors of finite-dimensional vector spaces can be represented by either a column vector (which is usually the case) or a row vector, Generally speaking, Maple does not distinguish column vectors from row vectors. It does when the user specifies it. A vector in Maple can be defined by different ways, Typically, a vector is defined by special command: v:= veetor([3, 11) larly, we can define another vector: vector({-2, 1) 2 1 Ifyou want a symbolic vector of dimension three you do: w := vector(3) w= array(1.. 3) Compare with veetor(3, 0) If you want to see the entries try: evalm(w) w/t}, w/2), w'3)] Notice that just typing "u;" doesn't work the way you expect. You should use either evalm or print. A vector with pseudorandom entries, use the command randvector(n) where nis the dimension of the vector. ‘The number of components in a vector is called its dimension. We can find the dimension in at least three different ways: vectdim( v ) or nops({3,2]) with(Statistics): Counts({3,2]) Entries of a vector could be functions or other objects, including vectors and arrays. We will show examples in the next section. To get access to particular entry of the vector, use brackets: uz] Now we turn our attention to basic operations with vectors. For example, hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl 212 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 uPlusy : uPlusy =u tv does not evaluate. To see the entries you need to use "eval" which stands for EVALuate as a Matrix. So now evaim(uPlusy); 12 and you get the components of the sum of the two vectors “u" and "v", There is still one more way to use "vector" This is the function form of the "vector" command, w i= vector(6, 142 -i); w= (026122030) Notice that here, the function is a function of only one variable. Multiplication by a scalar is simply done with asterisk: evalm(atu); (-20a] The inner or dot product of two vectors u = (ui,tusy-+-y2a) and v = (v1,02,-.-,0) of the same length, which is the number w-v = (uv) = ut + uate +---tnt, are Computed with the command innerprod{v , u) innerprod(y , u) The angle (in radians) between these two vectors: angle(v, u) * arccos( 4 yt0 4) To “convert” to degrees just do: convert(%,degrees); 180 (m — arecos(z V10 v5) degrees evalt(%); 135.0000000 degrees Two vectors are called orthogonal if the angle between them is 1/2. In n-dimensional vector space, it is possible to introduce many definitions of their lengths. We will mostly use two of them: either Euclidian Ila = fae eR or Manhatten: I vfoe = max{lesy ooh ooo eal Maple has dedicated commands for them. So to find the Euclidean norm, we type norm( v, 2) vio and the Manhatten norm is just norm(v) pst. cim brown edupooplalécbrushiam 34Maplelopartt. him! am 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 For the vector u, we have norm(u) 2 Avector can be reduced to a vector with unit (Euclidean) norm: normalize( u ) To define a column vector, we use array command: vos array({{3}, (11) (i 1 To find a cross product, use the following Maple command: crossprod([3, 1, 1], [-4, 1, 211) [1-74] In engineering applications, it is a custom to identify a vector with a column-vector, When a vector entered as a list, you can access each of its components with v(1], vi2], vi3]. So for example, a,b, cis vit+v[s}*2; ate which is the sum of the first entry of the vector "v" plus the square of its third entry. 2.1.2. How to define Matrices First thing we need to know how to define a matrix. (One way is to list each row as a vector, For example to enter the matrix A A matrix({{t, 2, 3], [4 8, 6), (7, 8, 91) Another way is to give the raw-column size of the matrix and then list all the elements. For example to enter the matrix B r= matrix(3,3,[2, -1, 3, 0, 2,1, -2, 1, ADs Just typing A will not show the matrix A on the screen; Instead you need to use either evalm(A) or print(A) Armatrix can be defined also with the aid of array command, array(1.. 2, 1.. 2, [[1, 2} [3, 411) hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl a2 ssa WARE TUTONAL tena Aenea Pt [4] 34 You can also enter a matrix interactively using the command entermatrix(M) after having defined the matrix M: Mi=array(1..2,1..2); M = array(1..2,1.2, []) entermatrix(M); enter element 1,1 > 1; enter element 1,2 > 2; center element 2,1 > 3; enter element 2,2 > 4; (4) 3 x 4 matrix with random entries can be defined as follows: M:= randmatrix(3,4) then Maple will generate a 3-by-4 matrix where all entries are between -99 and 99. You can define what function will be used to generate the entries, but this is sometimes difficult. (For more information type ?randmatrix..) M:= diag(3,2) (3) s In a similar way, one can define a diagonal matrix of arbitrary dimensions. Once the matrix is entered, the elements. can be extracted. Foe example, M[2,1] gives the entry of the matrix M at the second row and first column. We can define the 3 x 3 identity matrix using the array command: array(identity, 1. 3, 1... 3) array(fdentity, 1.3, 1.2, {]) print(ii) 100 v10 004 diag(1, 1, 1) or directly: = matrix(3, 3, [1, 0, 0, 0, 1, 0, 0, 0, 41) To define a zero matrix, type: zero := matrix(3, 3, 0) 000 000 000, Now we check by adding zero matrix with the identity matrix: evalm(zero+ld) To enter a matrix of the same values, type matrix(3,4,1); hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl 522 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 where 3 is the number of rows, 4 is the number of columns, and 1 is the entry, To define the general form matrix and then enter some values, use matrix(2, 4, a) [eon (1,2) a(1,3) a(1,4) (2,1) a(2,2) (2,3) a(2,4) a(t, 2) a(2, 2) := +2; a(2, 4) matrix(2,4,a); [eon IT a(1,3) (1,4) (2,1) -2 a(2,3) ow There's lots of stuff here, let's hope we won't have to use it al. Recall that we work with linalg package. Now suppose we want to add two matrices. M:=matrix({(1,0,1],{2,1,0],(0,1,11); Ne=matrix(3,3,[2, -1, 3, 0, 2, 1, -2, 4, 1s Now we add two matrices together using evalm command: evalm(M+N); To subtract the matrices one from another, use evaim(N-M); To multiply a matrix by a scalar, type evalm(3*M); We don't know exactly who and when invented the mutiplication of matrices. At least we know that the work of 1812 by a French mathematician, physicist and astronomer Jacques Philippe Marie Binet (1786--1856) contains the definition of the product of matrices. Let A be a m x n matrix and B be an x k matrix. is product, C = AB isa mx k matrix, in which the n entries across the rows of A are multiplied with the n entries down the columns of B: Cela), where 6 = Srouhy To multiply a matrix by another matrix, type in evalm(A &* B); hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl 622 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 4 62 412 418 20 Remember the order matters with matrices! As expected, matrix multiplication is not commutative, evalm(B &* A); 19 23. 27 15 18 2h 5-7-9 You can even square matrices. For example, evalm((A &* B)*2); 98 344 590, This is the same thing as (AB)*(AB) Ifyou need to mutiply the same matrix a number of times, a cleaner way to do it is to design a do..od loop. Let's say | want to multiply a matrix A by itself 10 times. You will lear later how to determine A” for a square matrix A and any power p (not necessarily positive integer) without actual muttiplivcation when construction of a function of a square matrix willbe covered. | also want to be able to call each product and check the entries. In the following program, | Use the concatenation operator (.) to keep track of all the matrices that this loop creates. Also | define A1 by typing ‘A so I can mutiply A by itself within the loop. The print statement allows me to specify exactly which output Maple should print out since I have used a colon throughout to prevent Maple from prettyprinting an answer lke it normally does when one uses a semicolon, with(linalg): array({L-5, .5], [2 By): At for i to 9 do et: multiply(A., A) print( op(A.j) od: Now when I run this, the matrices A2 to A10 will show up on the screen in order. if you just want to see one of these matrices, you cannot type AS [return] because Maple will respond by prettyprinting AS. To see the entries in the matrix AS, you can type: op(AS); [enter] or print(AS); [enter] and you wil see the matrix. (op stands for operand. To see how this works, type 2op; for information.) To find a linear combination of matrices, Maple has a dedicated command matadd(M, N, 2, 3) which is equivalent to evalm(2*M+3"N) The call addrow(A,r1,r2,t); returns a copy of the matrix A in which row r2 is replaced by t row(A, r1) + row(A, £2). If your input ends with a colon, the result is not appeared on the screen. Assarray(1..4,1..5,[[12, :saddrow(A,1,2,9/12) addrow(A1,1,3,1/2) =addrow(A2,1,4,-1/3); 14,-9,5],1-9,4,-8,7,-3],{-6,11,-7,3,-9],[4,-6,10,-5,12]]); A similar operation is available for columns: addcol. Use the command mulrow(A,R1,m); to multiply row R1 of the matrix A by m: Ad:=mulrow(A3,1, 1/12) AS:=mulrow(Ad,2,-4/5); Use the command pivot(A,ji,j) to row-manipulate the matrix so that the entries above and below the pivot position (ij) are equal to 0 A6:=pivot(AS, 2,2 hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl 12 zvvo017 MAPLE TUTORIAL fr the Second Course in Applied Dera Equation, Part 21 Use the command swaprow(A,R1,R2); to swap rows R1 and R2 of the matrix A AT:=swaprow(A6,3,4); AB:=mulrow(A7,3,5/28): A9:=pivot(A8,3,3); A10:=mulrow(A8,4,-1/2}; We finally obtain the row-reduced form of the matrix A. A1t:=pivot(A10,4,5); Maple has a routine which performs row-reduction for you: use the command rref(A) R:=rref(A); You can obtain vectors which span the column space of the matrix with the command eolspan(A): colspan(A); The span of the columns of Ais the span of the pivot columns. If we remove the fourth column of A (using the command delcols(A,4..4)), the span does not change. B:=delcols(A,4..4); colspan(8) 2.1.3. Basic Operations with Matrices There is a special operation that transfers columns into row and vice versa: itis called transposition. The transpose of ‘a matrix was introduced in 1858 by the British mathematitian and lawyer Arthur Cayley (1821--1895). The transpose of a m x n matrix Ais another n x m matrix A” (also denoted as A’ or A ) created by any one of the following equivalent actions: reflect A over its main diagonal (which runs from top-left to bottom-right) to obtain A” write the rows of A as the columns of A write the columns of A as the rows of A™ Formally, the ith row, jth column element of AT is the j th row, /th column element of A’ AT), = [Aly A square matrix A is called symmetric if A = A7.Let A and B be m x n matrices and c be a scalar, Then we have the following properties for transpose matrices: (ATT =A 2.(A+B)7 =a" 3.(AB)" =BTAT 4, (cB)" =cB" 5. AAT is a symmetric matrix. Br To make a transpose A: matrix({{0, 1, 0], (0, 0, 1], [-24, 19, 3]]) o 10 oo -M4 10 3 transpose(A) hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl ame 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 When entries of a matrix A are complex numbers or functions, then there exists another useful operation, called adjoint. Namely, the adjoint matrix to m x n matrix A is the n x m matrix, denoted by A° that is defined as ae (ay where A is complex conjugate of the matrix A. If A = A*, the matrix A is called self-adjoint or Hermitian. Maple has a dedicated command to calculate the adjoint matrix: B:=matrix(2,2,(1,1,2°1,3]) (tr 3) ar 3 htranspose(B) ( Tr 31) 13 transpose(A) ¢ 2 ") 13 When a matrix has only real entries, the commands transpose(A); and htranspose(A); give the same output transpose matrix. The trace of a matrix is the sum of its diagonal elements: where nis the dimension of a square n x n matrix A = [as,! trace(A) Applying Functions to Vector and Matrix Elements Maple can be used to apply a function to each element of a vector or matrix, This is done using the map( ) command. The calling sequence of this command makes use of function notation. This may be unfamiliar to you, but itis easy to understand. The command is inputted as follows: map(u > f(u), Matrix or Vector). This will become much clearer after an example with(linalg): 9:= <1, t42, 2, th3> e & map(u > u’2, 9) hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl 922 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 The map( ) command takes each element u of the vector and applies the function f(u) to it, In this example, each element of the vector g is squared. u is a dummy variable, Any other letter or name could be used in its place, This command is particularly useful for taking derivatives and integrals of vectors and matrices. Matrix Derivative and Integral Example: with(linalg): B:= l> map(q > diff a, t), B) map(q -> int(a, t), B) The integral command does not include constants of integration. Definite integral map(q -> int(q, t= 0.5), B) [Fee | 2.1.4. Determinants and Inverse Matrices The determinant of a square n x n matrix A is the value that is calculated as the sum of n! terms, half of them are taken with sign plus, and another half has oposite sign. The concept of a determinant first appears, nearly two mmillonnia before its supposed invention by the Japanese mathematician Seki Kowa (1642-1708) in 1683 or his German contemporary Gottfried Leibniz (1646--1716). Traditionally, the determinant of a square matrix is denoted by det(A), det A, or [Al In case of a 2 x 2 matrix A, the determinant is det A ~ det |° ‘|-|2 | ad —be le alle @ Similarly, for a 3 x 3 matrix, we have a specific formula: {io |4 Ae elt jleace oe dh — cog — bdi — afh. a il lg Each determinant of a 2 x 2 matrix in this equation is called a *minor” of the matrix A. The same sort of procedure can be used to find the determinant of a 4 x 4 matrix, the determinant of a 5 x 5 matrix, and so forth First, we define a matrix: A := matrix({[0, 1, 0], [0, 0, 1], [-24, 10, 31)) Then we obtain its determinant: det(A) hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl 1022 2erta2017 MAPLE TUTORIAL forte Second Course in Applied Diferertal Equations, Part 21 We list the main properties of determinat: 4. det(I) = 1, where | is the identity matrix (all entries are zeroes except diagonal terms, which all are ones). 2. det (AT) = det (A) 3, det (AT!) = 1/det (A) = (det A)? 4, det (A detB) = det A det B 5, det (eA) = o* det (A) for n x n matrix A and a scalar c. 6. IF A = [a,,] is a triangular matrix, ic. a, = 0 whenever i > jor, alternatively, whenever i 1 is in the interior of the domain, with f being (mj — 4) times differentiable at 4; We build a function f(A) of diagonalizable square matrix A according to James Sylvester, who was an English lawyer and music tutor before his appointment as a professor of mathematics in 1885. To define a function of a square matrix, we need to construct k Sylvester's auxiliary matrices for each distinct eigenvalue Dy F128 wD) (A= Aa) (A= AnD) (A= AD) (As = Aad A = Aas = Angad = As = An) Now we define the function f(A) according to the formula f(A) = OHA) B Each Sylvester's matrix is a projection matrix on eigenspace of the corresponding eigenvalue. 2.1.9. The Resolvent Method |. with(linalg) 2.1.10. Spectral Decomposition Method |. with(linalg) 2.1.6. Positive Matrices and Square Roots |. with(linalg) 2.1.11. Miscellaneous |. with(linalg) hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl uae 2erta2017 [MAPLE TUTORIAL forthe Second Course Applied Differential Equations, Part 21 hp. cim brown edupooplalécbrushiam 34Maplelopartt.himl ne

Potrebbero piacerti anche