Maple Hand Boken

Embed Size (px)

Citation preview

  • 8/13/2019 Maple Hand Boken

    1/28

    Maple Handbook

    Carl-Gustav Werner

    Centre for Mathematical Science

    Lund University

  • 8/13/2019 Maple Hand Boken

    2/28

  • 8/13/2019 Maple Hand Boken

    3/28

    Contents

    1 Introduction to Maple 5

    1.1 Input of Mathematical Expressions . . . . . . . . . . . . . . . . . 51.2 Arithmetical Expressions . . . . . . . . . . . . . . . . . . . . . . 51.3 Defining Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 71.4 Working with Algebraic Expressions . . . . . . . . . . . . . . . . 81.5 Defining Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 91.6 Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    1.6.1 General Equations . . . . . . . . . . . . . . . . . . . . . . 111.6.2 Inequalities . . . . . . . . . . . . . . . . . . . . . . . . . . 111.6.3 Systems of Equations . . . . . . . . . . . . . . . . . . . . 121.6.4 Numerical Solutions . . . . . . . . . . . . . . . . . . . . . 121.6.5 Integer Equations . . . . . . . . . . . . . . . . . . . . . . . 131.6.6 Differential Equations . . . . . . . . . . . . . . . . . . . . 131.6.7 Difference Equations . . . . . . . . . . . . . . . . . . . . . 14

    2 Linear Algebra 15

    2.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.1.1 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.1.2 Vector Commands in Maple . . . . . . . . . . . . . . . . . 162.1.3 A Geometrical Example . . . . . . . . . . . . . . . . . . . 162.1.4 Examples with Orthogonal Vectors . . . . . . . . . . . . . 172.1.5 Examples with Lines in R3 . . . . . . . . . . . . . . . . . 18

    2.2 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.2.1 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.2.2 Matrix Commands in Maple . . . . . . . . . . . . . . . . . 202.2.3 Orthogonal Matrices . . . . . . . . . . . . . . . . . . . . . 212.2.4 Determinants . . . . . . . . . . . . . . . . . . . . . . . . . 222.2.5 Systems of Equations . . . . . . . . . . . . . . . . . . . . 222.2.6 Functions of Matrices and Vectors . . . . . . . . . . . . . 232.2.7 Linear Maps . . . . . . . . . . . . . . . . . . . . . . . . . 242.2.8 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . 252.2.9 Diagonalization . . . . . . . . . . . . . . . . . . . . . . . . 27

    3

  • 8/13/2019 Maple Hand Boken

    4/28

    CONTENTS

    4

  • 8/13/2019 Maple Hand Boken

    5/28

    Chapter 1

    Introduction to Maple

    1.1 Input of Mathematical Expressions

    When Maple is ready to receive input, a so called prompt becomes visible:

    >

    Right after this> one can write the mathematical expression and Maple com-

    mands, which should always be followed by ; (or in some cases :). This iseasily forgotten, so once again: All input must finish with ;. An easyexample:

    > 3+12;

    15

    If one would like to modify something in a previous input, one could use thearrow-tangent (or the mouse) to mark and modify the text. The new commandis executed again if you press Return/Enter.

    1.2 Arithmetical Expressions

    The basic arithmetic operators such as plus, minus, and equal to are written inMaple using their symbols+,, =. More is to be found in the following table:

    Mathematics Maple Explanationx + y x+y additionx y x-y subtractionxy x*y multiplicationx

    y x/y division

    xy xy or x**y exponentiatex= y x=y equal tox=y xy not equal tox < y xy greater thanx y x>=y greater than or equal to

    5

  • 8/13/2019 Maple Hand Boken

    6/28

  • 8/13/2019 Maple Hand Boken

    7/28

    1.3. DEFINING VARIABLES

    > evalf(8/12+sqrt(2));

    2.080880229

    > 8/12+sqrt(2.0);

    2.080880229

    Maple answers normally with 10 digits. If one wants a computation to be exe-cuted using for instance 25 digits instead, one could specify this in the commandevalf:

    > evalf(8/12+sqrt(2), 25);

    2.080880229039761715468356

    1.3 Defining Variables

    Sometimes a result to some computation is to be used several times in other

    calculations. It is therefore important to be able to access such a result in Maple.This can be done in several ways:

    1. One can refer to the last and second last result using%, and%%respec-tively:

    > 60*60*24;

    86400

    > %*365;

    31536000

    2. Save the answer as a constants:

    > s:=60*60*24;

    s:= 86400

    > s*365;

    31536000

    If the result is to be used several times, the second method is to prefer. Nev-ertheless, it is always easier to define a variable and assign a value to it. The

    7

  • 8/13/2019 Maple Hand Boken

    8/28

    CHAPTER 1. INTRODUCTION TO MAPLE

    notation used for the variable may contain both capital and small letters, dig-its and underscore. In the previous example one could have used the notationSecondsPerDay or seconds_per_day instead ofs.

    One can also assign values to several variables at a time:

    > a:=3; b:=6.75; c:=2.83;

    a:= 3b:= 6.75

    c:= 2.83

    Once a value is assigned to some variable, this value remains unchanged untilone assigns a new value to it, or until Maple is shut down. To get rid of a valueassigned to some variable, one could "erase" the value by writing:

    > s:=s;

    s:= s

    1.4 Working with Algebraic Expressions

    If one writes an algebraic expression that contains "free" variables, Maple re-sponds by writing the same expression, in a slightly simpler way:

    > 2*(x+y)*(x+y)/(x-y);

    2(x + y)2

    x yIf one wants to control what Maple does to some algebraic expression, the fol-lowing commands are available:

    Maple Explanationexpand(u) expands the expression ufactor(u) factorizes unormal(u) simplifies usimplify(u) simplifies ucollect(u,x) adds together terms in u containing the variable xcombine(u,f) combines all functions f in u to one function

    To illustrate the differences between these commands consider the followingexpression:

    > a:=2*(x+y)*(x+y)/(x-y)+(x+y)/(x-y)2;

    a:= 2(x + y)

    2

    x

    y

    + x + y

    (x

    y)

    2

    > simplify(a);

    8

  • 8/13/2019 Maple Hand Boken

    9/28

    1.5. DEFINING FUNCTIONS

    2 x3 + 2 x2y 2 xy2 2 y3 + x + y(x y)2

    > factor(a);

    (x + y)

    2 x2 + 1 2 y2(x y)2

    > expand(a);

    2 x2

    x y + 4 xy

    x y + 2 y2

    x y + x

    (x y)2 + y

    (x y)2

    Some of these commands prove useful in simplifying expressions containing num-bers:

    > (sqrt(5)-1)2/(sqrt(5)-3);

    5 1

    2

    5

    3

    One can use% to refer to the previous result:a

    > simplify(%); 5 125 3

    Nothing seems to have happened. One could try factor instead. To this endone can use%. (In fact, it is the second last result that is to be factorized, andit can be refered to using %%)

    > factor(%);

    2It turns out that factor is often better than simplify on this type of simplifica-tions.

    1.5 Defining Functions

    One can define functions in the same manner that one can assign a value tosome variable. This can be useful since in many situations, one may have anexpression that is to be used several times. There are two ways to "save" acertain expression. One can assign the expression as "value" to some variable:

    > f:=x+2/(x*x+2*x);

    f :=x + 2

    x2 + 2 x

    9

  • 8/13/2019 Maple Hand Boken

    10/28

    CHAPTER 1. INTRODUCTION TO MAPLE

    but it is often more useful to define it as a proper function in Maple:

    > g:=x->x+2/(x*x+2*x);

    g:= xx + 2x2 + 2 x

    Note the differences in the syntax. The arrow is written as a minus signfollowed by the symbol >.

    Most situations can be handled using both ways, but in some cases it turnsout that the second method is far more advantageous. Below we compare howthe value of the expression is computed given a certain value of the variable x:

    > eval(f, x=1);

    5

    3

    > g(1);

    5

    3

    It is of course possible to define a function out of an expression:

    > unapply(f, x);

    xx + 2x2 + 2 x

    and vice-versa:

    > g(x);

    x + 2

    x2 + 2 x

    Note though that it is notpossible to write in the following manner in order todefine a function:

    > h(x):=x+2/(x*x+2*x);

    h(x) :=x + 2

    x2 + 2 x

    Unfortunately, Maple does not return any error message, but "h" is not definedas a function.

    1.6 Equations

    Maple can solve many types of equations and each such type requires a specialcommand:

    10

  • 8/13/2019 Maple Hand Boken

    11/28

    1.6. EQUATIONS

    Maple Explanationsolve solves general equations (polynomial)fsolve solves equations numericallyisolve solves integer equationsdsolve solves differential equationsrsolve solves difference equations

    1.6.1 General Equations

    One of the most common types of equations are polynomial equations. To solvefor instance the second-degree equation x2 4x + 3 = 0, write:

    > solve(x2-4*x+3=0, x);

    1, 3

    thus, Maple returns the two roots 1 and 3. If the right hand side is equal tozero, one does not have to write it:

    > solve(x2-4*x+3, x);

    1, 3

    If the equation one wants to solve using Maple contains only one variable (as inthe above example) one does not need to specify this fact either. On the otherhand, the variable must be specified when using the command diff:

    > solve(x2-4*x+3);

    1, 3

    If one wants the solution returned by Maple to appear more clearly, one couldwritex using set-brackets:

    > solve(x2-4*x+3=0, {x});

    {x= 1

    },{

    x= 3}

    If the equation contains several variables, the variable that one wants to equatemust be specified. To obtainx from yx + x

    y = 5 write:

    > solve(y*x+x/y=5, x);

    5 y

    y2 + 1

    1.6.2 Inequalities

    To solve an inequality one can again use the command solve:

    > solve(x2-4*x+3

  • 8/13/2019 Maple Hand Boken

    12/28

    CHAPTER 1. INTRODUCTION TO MAPLE

    RealRange(Open(1), Open(3))

    The answer might look a little bit weird, but it means real interval from 1 till3, open at both endpoints (i.e. end-points excluded). To obtain the answer onthe form 1 < x solve(x2-4*x+3 solve({3*x+2*y=16, 2*x-y=-1});

    {y= 5, x= 2}Systems of inequalities can be solved in a similar manner.

    1.6.4 Numerical Solutions

    If one attempts to solve an equation that cannot be solved exactly, Maplesresponse is either none whatsoever or a meaningless reformulation of the originalequation:

    > solve(3*sin(x)=log(x));

    RootOf(_Z e3 sin(_Z))One could try to obtain a numerical solution to such an equation instead, using

    > fsolve(3*sin(x)=log(x));

    2.792247796

    Often, Maple answers to the commandfsolve with the first numerical solutionthat is computed. If one has reason to believe that there might be a largersolutionx, for instance in the interval 3 < x < 10 one could specify this intervalinfsolve:

    > fsolve(3*sin(x)=log(x), x=3..10);

    6.988241068

    So there is another solution! This procedure makes it hard to find, numerically,all the solutions to such equations. In practice, in such cases it is to prefer todraw the graphs of the functions involved.

    12

  • 8/13/2019 Maple Hand Boken

    13/28

    1.6. EQUATIONS

    1.6.5 Integer Equations

    Integer or Diophantine equations can be solved in Maple with isolve. To findinteger solutions to x2 + y2 = 8 write:

    > isolve(x2+y2=8);

    {y = 2, x= 2} , {x=2, y=2} , {x=2, y= 2} , {y=2, x= 2}

    Here is one equation with no integer solution:

    > isolve(2*x+2*y=5);

    So Maple does not give any answer whatsoever! A small modification gives in-finitely many solutions:

    > isolve(2*x+2*y=6);

    {x= 3 _Z1 , y= _Z1}

    The answer looks more peculiar than what it is. Since the equation has in-finitely many solutions, these can be written using a parameter. In the exampleabove, Maple used the parameter _Z1. One could change the notation for the

    parameter_Z1 tok by writing:

    > isolve(2*x+2*y=6, k);

    {x= 3 k, y= k}

    1.6.6 Differential Equations

    Differential equations and systems of such are solved in Maple using dsolve.The first order linear differential equation, y + 2y= 5, without initial values issolved by:

    > dsolve(D(y)(x)+2*y(x)=5);

    y(x) = 5/2 + e2 x_C1

    Instead ofD(y)(x)one can writediff(y(x),x)to expressy . The undeterminedconstant usually denoted by Cis denoted here by _C1.

    One can of course solve initial value problems as well. Add to the equationabove the initial condition y(1) = 3. Unfortunately, the syntax becomes morecomplicated since one must specify which function is to be determined (in thiscasey(x)) even if there are no other alternatives:

    > dsolve({D(y)(x)+2*y(x)=5, y(1)=3}, y(x));

    y(x) = 52+ 12 e

    2 x

    e2

    13

  • 8/13/2019 Maple Hand Boken

    14/28

    CHAPTER 1. INTRODUCTION TO MAPLE

    1.6.7 Difference Equations

    Difference or recursive equations can also be solved in Maple. The command isrsolveand the syntax is similar to the one used for dsolve.

    Below it is shown how to obtain a (non-recursive) formula for the Fibonaccisequence(1, 1, 2, 3, 5, 8, 13, 21, . . .)that is defined by:

    F(k) = F(k 1) + F(k 2), F(0) = 1, F(1) = 1

    The initial values are written inside brackets together with the difference equa-tion:

    > rsolve({F(k)=F(k-1)+F(k-2), F(0)=1, F(1)=1}, F(k));

    2

    5

    5

    2

    1

    1 +

    5

    k1 +

    5

    +

    2

    5

    5

    2 1

    5 + 1

    k

    5 + 1.

    14

  • 8/13/2019 Maple Hand Boken

    15/28

    Chapter 2

    Linear Algebra

    Maple has only a couple of functionalities available when working with matricesand vectors. Nevertheless, there are two additional packages that contain more:linalg(older) and the newer LinearAlgebra package. In what follows we give adescription of the latter.

    In order to have access to the contents of this package one has to write first:

    > with(LinearAlgebra):

    Instead of ":" at the end of this line one could write ";" in order to obtain a listof all the commands available in this package.

    2.1 Vectors

    2.1.1 Input

    Vectors are created with the command Vector.In what follows we give an account of the most useful ways to create vectors.

    (In Maples help-section one finds several other ways that can be useful in somespecial cases, but for our purposes, the examples below are enough.)

    So one can create a vector with 3 entries, all of them equal to 1 and savethis vector as the value of the variable v1:

    > v1:=Vector(3, 1);

    v1 :=

    1

    1

    1

    Note that the vector is represented by a (column) 3 1 matrix. If you wouldlike to have a (row) vector represented by a 13matrix instead, this is possible.The following command creates a (row) vector with 5 entries, all equal to 2:

    > v2:=Vector[row](5, 2);

    v2 := [2, 2, 2, 2, 2]

    15

  • 8/13/2019 Maple Hand Boken

    16/28

    CHAPTER 2. LINEAR ALGEBRA

    If the entries of the vectors are different, they should be enumerated like in theexample below:

    > v3:=Vector[row]([2, 3, 5, 7, 11]);

    v3 := [2, 3, 5, 7, 11]

    Once a vector is defined, one can access its entries by giving the index for theentries in question :

    > v4:=v3[2]*v3[3..5];

    v4 := [15, 21, 33]

    2.1.2 Vector Commands in Maple

    The operators (and operations) that usually are defined on vectors are of courseavailable in Maple:

    Mathematics Maple Explanationu+ v u+v; additionu v u-v; subtractionsv s*v; multiplication by scalars1u v or (u | v) DotProduct(u,v); scalar productu v CrossProduct(u,v); vector product|v| Norm(v,2); length of a vectorvt Transpose(v); transpose (changes between

    row- and column-vectors)map(f,v); applies the functionfon all

    the entries of the vector v.

    The operations+ andare of course defined only if the vectors have the samenumber of entries. Also, to add or subtract vectors, Maple requires that theseshould be either row- or column- vectors.

    2.1.3 A Geometrical Example

    1. Compute the distance between the lines L1andL2in R3, whereL1is the linethrough the points (1, 1, 1)and (4, 5, 3), and L2 the line through (1,10,1)and (8, 2, 2). (The points are given with their coordinates with respect to apositively oriented ON coordinate system.)

    Solution: Begin by definingv1and v2, the orientation vectors forL1 and L2,respectively:

    > v1:=Vector[row]([4, 5, 3] - [1, 1, 1]);

    v1 := [3, 4, 2]

    1If the scalar, s, is a variable (with no assigned value) then Maple does not perform themultiplication. One should instead use the commandScalarMultiply(v,s);

    16

  • 8/13/2019 Maple Hand Boken

    17/28

    2.1. VECTORS

    > v2:=Vector[row]([8, 2, 2] - [-1, -10, -1]);

    v2 := [9, 12, 3]

    Determine a vector that is orthogonal to v1 and v2:

    > ort:=CrossProduct(v1, v2);

    ort:= [12, 9, 0]

    Divide this vector with its length in order to obtain a vector e of length 1 or-thogonal to bothv1 and v2. 2:

    > e:=1/Norm(ort, 2)*ort;

    e:=

    4

    5,3

    5, 0

    Create a vector P Qassociated to the directed segment from an arbitrary pointPon the line L1 to an arbitrary point Q on the line L2:

    > PQ:=Vector[row]([8, 2, 2] - [1, 1, 1]);

    P Q:= [7, 1, 1]

    The distance between the two lines equals the absolute value of the scalar prod-uct between e and P Q:

    > abs(DotProduct(PQ, e));

    5

    2.1.4 Examples with Orthogonal Vectors

    Consider the vectors e1 = (2, 1, 0, 1), e2 = (0, 2, 1,2)and w = (1, 2, 1, 2). Showthat e1 and e2 are orthogonal and determine numbers s1 and s2 such that thevector s1e1+ s2e2+ w becomes orthogonal to both e1 and e2 3.

    Solution: We begin by defining the vectors:

    > e1:=Vector[row]([2, 1, 0, 1]);

    e1 := [2, 1, 0, 1]

    > e2:=Vector[row]([0, 2, 1, -2]);

    e2 := [0, 2, 1,2]

    2There is a commandNormalizethat can be used to normalize vectors. Using this commandwe could have written e:=Normalize(ort, Euclidean);

    3Example 4, Chapter 7, Linear Algebra, K. G. Andersson, 2000

    17

  • 8/13/2019 Maple Hand Boken

    18/28

    CHAPTER 2. LINEAR ALGEBRA

    > w:=Vector[row]([1, 2, 1, 2]);

    w:= [1, 2, 1, 2]

    If e1 and e2 are orthogonal then their scalar product is equal to zero:

    > DotProduct(e1, e2);

    0

    So these vectors are indeed orthogonal. Let now u= s1e1+ s2e2+ w:

    > u:=ScalarMultiply(e1,s1)+ScalarMultiply(e2,s2)+w;

    u:= [2 s1 + 1, s1 + 2 s2 + 2, s2 + 1, s1 2 s2 + 2]

    Note thatScalarMultiply(e1,s1)is used here instead ofs1*e1since Maple doesnot know whether s1 is a scalar.

    We can now set up a system of equations with the two conditions on thescalar products and let Maple solve it:

    > solve({DotProduct(u, e1)=0, DotProduct(u, e2)=0});

    s2 =19 , s1 =1

    2.1.5 Examples with Lines in R3

    Do the lines

    L1:

    x=2 + 3ty = 5 tz = 35t

    L2 :

    x= 11 ty =2 + tz =42t

    intersect each other? And, if this is the case, what is their point of intersection?In Maple, lines given by parametric equations are handled by letting the

    right-hand sides be the entries of a Vector:> L1:=Vector([-2+3*t,5-t,3-5*t]);

    L1 :=

    2 + 3t5 t

    3 5t

    > L2:=Vector([11-s,-2+s,-4-2*s]);

    L2 :=

    11 s2 + s

    4

    2s

    Note that the parameters involved in the parametric equations of the two linesmust be carried in different variables.

    18

  • 8/13/2019 Maple Hand Boken

    19/28

    2.2. MATRICES

    The fact that the lines intersect each other results in the solvability of thesystem of equations given by L1 =L2. However, it turns out that In Maple itis easier to write this system in the form L1 L2 = 0and consider only the lefthand side of this:

    > L1-L2; 13 + 3t + s

    7 t s7 5t + 2s

    Since the commandsolverequires that the solution to such a system is a set, wecan not solve the system directly, but we have to convert the right-hand side to aset (that is, the set of all points in R3 of the form(13+3t+s, 7ts, 75t+2s)):

    > convert(%, set);

    {13 + 3t + s, 7 t s, 7 5t + 2s}and finally solve the system L1 L2 = 0:

    > solve(%);

    {t= 3, s= 4}The point of intersection is determined by letting t = 3 in the equation for L1,ors = 4in the equation for L2; this can be done witheval

    > eval(L1, %); 7212

    2.2 Matrices

    2.2.1 Input

    Matrices are created using the command Matrix. The syntax is similar to thecommandVector.

    A square3 3 matrix with all entries equal to zero is created via:> Matrix(3);

    0 0 0

    0 0 0

    0 0 0

    En2 3 matrix whose entries are all equal to 8 can be created by:> Matrix(2, 3, 8);

    19

  • 8/13/2019 Maple Hand Boken

    20/28

    CHAPTER 2. LINEAR ALGEBRA

    8 8 8

    8 8 8

    However, to create a matrix with different entries, one has to list the entries oneach row like in the example below. Notice the double square-brackets used inthe syntax:

    > A:=Matrix([[1, 2], [3, 4]]);

    A:=

    1 2

    3 4

    When a matrix is defined, parts of it can be accessed via the indices of theelements in question:

    > A[2, 2]*A[2, 1..2];

    [12, 16]

    2.2.2 Matrix Commands in Maple

    Most of the operations that are defined for matrices are available in Maple:

    Mathematics Maple ExplanationA + B A+B; additionA B A-B; subtractionsA s*A; multiplication by a scalar4

    AB A.B; matrix productAt Transpose(A); transposeA1 A(-1); matrix inverseI orE IdentityMatrix(n); identity matrix (of order n)

    NullSpace(A); null spaceRank(A); rangLinearSolve(A, b); solve the system ofAx=bQRDecomposition(A); QR-faktorization

    detA or D(A) Determinant(A); determinanttrA Trace(A); trace

    Eigenvalues(A); eigenvaluesEigenvectors(A); eigenvectors and eigenvaluesmap(f, A); Apply the functionfon all

    the entries of the matrix A.

    There are many functions and operations defined on matrices that are avail-able in Maple. To find more information, see the Help section available in the

    LinearAlgebra package.4If the scalar, s, is a free variable, then Maple does not perform the multiplication. One

    should use the command ScalarMultiply(A,s);

    20

  • 8/13/2019 Maple Hand Boken

    21/28

    2.2. MATRICES

    2.2.3 Orthogonal Matrices

    Determine the numbers a, b and c such that the matrix

    1

    7

    2 6 b3 2 c

    6 a 2

    becomes orthogonal5.Solution: Begin by defining the matrix:

    > Q:=1/7*Matrix([[2, 6, b], [3, 2, c], [6, a, 2]]);

    Q:=

    2

    7

    6

    7

    1

    7b

    3

    7

    2

    7

    1

    7c

    6

    7

    1

    7a

    2

    7

    To check whether a matrixQis orthogonal one has to check the equality QtQ=E (orQQt =E). The equality can be checked in Maple, but it turns out that

    it is easier to proceed as in section 2.1.5, that is:- consider the right-hand side of the equality QtQ E= 0 :> Q.Transpose(Q)-IdentityMatrix(3);

    949

    + 1

    49b2

    18

    49+

    1

    49bc

    12

    49+

    6

    49a+

    2

    49b

    18

    49+

    1

    49bc 36

    49+

    1

    49c2

    18

    49+

    2

    49a +

    2

    49c

    12

    49+

    6

    49a +

    2

    49b

    18

    49+

    2

    49a+

    2

    49c 9

    49+

    1

    49a2

    - convert it into a set:

    > convert(%, set);18

    49+

    1

    49bc,

    12

    49+

    6

    49a +

    2

    49b,

    18

    49+

    2

    49a +

    2

    49c, 9

    49+

    1

    49b2,36

    49+

    1

    49c2, 9

    49+

    1

    49a2

    - solve the system QtQ E= 0

    > solve(%);

    {a=3, c=6, b= 3}- and finally, to get Q, write:

    > eval(Q, %);

    5Exercise 7, chapter 7, Linear Algebra, K. G. Andersson, 2000

    21

  • 8/13/2019 Maple Hand Boken

    22/28

    CHAPTER 2. LINEAR ALGEBRA

    2

    7

    6

    7

    3

    7

    3

    7

    2

    7

    67

    6

    7

    37

    2

    7

    2.2.4 Determinants

    Determinants are computed using the command Determinant. By definingsome matrices:

    > A:=Matrix([[1, 2], [3, 4]]);

    A:=

    1 2

    3 4

    > B:=Matrix([[2, 4], [6, 8]]);

    B :=

    2 4

    6 8

    and compute their determinants, one can check (not prove) the product rule fordeterminants:

    > Determinant(A); Determinant(B); Determinant(A.B);

    2816

    2.2.5 Systems of Equations

    Systems of equations of the type Ax= b are solved A1b, provided that (A) isinvertible. (Let A be the matrix from the previous example.):

    > b:=Vector([5, 7]);

    b:=

    5

    7

    > A(-1).b; 34

    22

  • 8/13/2019 Maple Hand Boken

    23/28

    2.2. MATRICES

    One could also use one of Maples special commands (there are several) to solvesuch systems of equations:

    > LinearSolve(A, b); 34

    In easier cases it does not matter which method one chooses to use. LinearSolve

    is faster, and this method is to prefer if the matrix involved is very large. Also,this command manages to determine the structure of the solutions to systems ofequationsAx= b having infinitely many solutions (detA= 0and the solutionsdepend on one or more parameters).

    2.2.6 Functions of Matrices and Vectors

    Certain commands and functions that are defined for real numbers are evendefined for vectors and matrices. One such command is evalf:

    > C:=Pi*Matrix([[-1/2, 5/6], [5/6, -1/2]]);

    1

    2

    5

    6

    5

    6 1

    2

    > evalf(C); 1.570796327 2.6179938782.617993878 1.570796327

    If one would like to compute the sine of each entry of some matrix or vector,one could use the commandmap:

    > map(sin, C); 1

    1

    21

    2 1

    Moreover, there are commands that enables one to compute functions of matri-ces:

    Mathematics Maple

    eA MatrixExponential(A);Ax MatrixPower(A, x);f(A) MatrixFunction(A, f(x), x);

    With the same matrix Cas above one could for instance compute sin C ( to

    spare some time one could simplify the answer directly as well):

    > simplify(MatrixFunction(C, sin(x), x));

    23

  • 8/13/2019 Maple Hand Boken

    24/28

    CHAPTER 2. LINEAR ALGEBRA

    1

    2

    3 0

    0 1

    2

    3

    2.2.7 Linear Maps

    Using Maple one can illustrate geometrically the action of certain linear maps.

    In what follows we describe how one can illustrate what a rotation around theorigin does to some object. To this end it is appropriate to consider some non-symmetric object, like for instance, an L with corner (1, 1)and endpoints(1, 3)and (2, 1). To be able to work with this geometric object we store the coordi-nates of the three points as the columns of a matrix:

    > L:=Matrix([[1, 1, 2], [3, 1, 1]]);

    L:=

    1 1 2

    3 1 1

    On the other hand, the command plot requires that the coordinates are givenin a list of lists, so the matrix above must be converted into such a list:

    > l:=convert(Transpose(L), listlist);

    l:= [[1, 3], [1, 1], [2, 1]]

    before we can draw our L:

    > plot(l, x=-4..4, y=-4..4, scaling=constrained, color=black);

    4

    3

    2

    1

    0

    1

    2

    3

    4

    y

    4 3 2 1 1 2 3 4x

    To see what a rotation around the origin does to the geometric object above,define first a generalmatrix for rotation around the origin in a plane as a func-

    tion (rather than considering a rotation by a certain angle):

    > Rot:=v->Matrix([[cos(v), -sin(v)], [sin(v), cos(v)]]);

    24

  • 8/13/2019 Maple Hand Boken

    25/28

    2.2. MATRICES

    Rot:= v> Matrix([[cos(v),sin(v)], [sin(v),cos(v)]])

    It is now easy to consider a rotation by a certain angle (in this case 6 ):

    > R:=Rot(Pi/6);

    R:=

    1

    2

    3 1

    2

    1

    2

    1

    23

    The action of the rotation on the L above is represented by:

    Lr:=R.L;

    Lr :=

    1

    2

    3 3

    2

    1

    2

    3 1

    2

    3 1

    2

    1

    2+

    3

    2

    3

    1

    2+

    1

    2

    3 1 +

    1

    2

    3

    Finally, the result (the matrix above) can be converted to a list and representedas a geometrical object on the plane:

    lr:=convert(Transpose(Lr), listlist);

    lr :=

    1

    2

    3 3

    2,1

    2+

    3

    2

    3

    ,

    1

    2

    3 1

    2,1

    2+

    1

    2

    3

    ,

    3 1

    2, 1 +

    1

    2

    3

    > plot(lr, x=-4..4, y=-4..4, scaling=constrained, color=black);

    4

    3

    2

    1

    0

    1

    2

    3

    4

    y

    4 3 2 1 1 2 3 4x

    2.2.8 Eigenvalues and Eigenvectors

    LetA be the matrix given by:

    25

  • 8/13/2019 Maple Hand Boken

    26/28

    CHAPTER 2. LINEAR ALGEBRA

    > A:=Matrix([[3, -1, 0], [-1, 2, -1], [0, -1, 3]]);

    3 1 01 2 1

    0 1 3

    To compute the eigenvalues ofA write:

    > Eigenvalues(A);

    3

    1

    4

    To compute the eigenvectors ofAone uses the commandEigenvectors(A). Thiscommand produces both the eigenvalues and the eigenvectors ofA. In order tobe able to work with the computed eigenvalues and the corresponding eigenvec-tors in Maple, one could save their values in two different variables, and Trespectively:

    > (lambda, T):=Eigenvectors(A);

    , T :=

    314

    , 1 1 10 2 1

    1 1 1

    Notice that the eigenvectors are given as columns of a matrix. By definition,e = Ae where e is the eigenvector corresponding to . This can easily bechecked; here is the first eigenvalue and its corresponding eigenvector:

    > e1:=T[1..3, 1];

    e1 :=

    1

    0

    1

    > lambda[1]*e1;

    3

    0

    3

    > A.e1;

    3

    03

    26

  • 8/13/2019 Maple Hand Boken

    27/28

    2.2. MATRICES

    2.2.9 Diagonalization

    For a matrix A that can be diagonalized, the diagonal matrix T is the matrixwhose columns are the eigenvectors ofA, which is exactly the output of thecommand Eigenvectors(A). With A and Tas in the previous example, thematrixA is diagonalized as follows:

    > R:=T(-1).A.T;

    R:=

    3 0 0

    0 1 0

    0 0 4

    so the eigenvalues are the entries on the main diagonal in the diagonal matrix. (Note that the diagonal matrix cannot be denoted by D in Maple sinceD meansthe differentiation operator.)

    The diagonalized matrix can be used to compute more complicated functionsof matrices. An example that shows how one can determine a matrix B thatsatisfiesB2 =A (i.e. a matrix which in some sense is

    A), is given below.

    First, define a matrix S that satisfies S2 = R. Such a matrix is easily

    computed by taking the square root of the diagonal entries of the (diagonal)matrixR:

    > S:=map(sqrt, R);

    S:=

    3 0 0

    0 1 0

    0 0 2

    Actually, it is easier (from Maples point of view) to compute the square rootof all the entries ofR, which has of course no effect on the zero entries.

    The matrixBis then given by B=T ST1

    :

    > B:=T.S.T(-1);

    B:=

    1

    2

    3 +

    5

    6 1

    3 1

    2

    3 +

    5

    6

    13

    4

    3 1

    3

    12

    3 +

    5

    6 1

    3

    1

    2

    3 +

    5

    6

    One can compute B2 to check whether the computations carried out so far are

    correct:

    > B2;

    27

  • 8/13/2019 Maple Hand Boken

    28/28

    CHAPTER 2. LINEAR ALGEBRA

    1

    2

    3 +

    5

    6

    2+

    1

    9+

    1

    2

    3 +

    5

    6

    21 2

    1

    2

    3 +

    5

    6

    1

    2

    3 +

    5

    6

    +

    1

    9

    1 2 1

    2

    1

    2

    3 +

    5

    6

    1

    2

    3 +

    5

    6

    +

    1

    9 1

    1

    2

    3 +

    5

    6

    2+

    1

    9+

    1

    2

    3 +

    5

    6

    2

    The result above does not really look like as the original matrix A, but if wesimplify6:

    > simplify(%);

    3 1 01 2 1

    0 1 3

    we see that B2 =A.One could also compare this result to the result given by MatrixPower or

    MatrixFunction:

    > simplify(MatrixPower(A, 1/2));

    1

    2

    3 +

    5

    6 1

    3

    5

    6 1

    2

    3

    13

    4

    3 1

    3

    5

    6 1

    2

    3 1

    3

    1

    2

    3 +

    5

    6

    6In older versions of Maple one would have to write map(simplify,%); instead.

    28