HMC Homepage      CS Home

Using Maple on Turing

Contents


Introduction

Maple VI is a powerful mathematical problem-solving and visualization system which is used just about everywhere. Its biggest strength is symbolic manipulation, which makes it ideal when you want symbolic answers rather than numbers (if you are looking for number crunching, try Matlab instead).


Getting Started

To start Maple type maple or xmaple in an open Turing window. maple starts a text console session while xmaple gives you a really spiffy graphical interface. I recommend the latter because it makes things a lot easier, so from now on I'm going to be referring to the graphical interface.

If Maple isn't broken (no guarantees on that), you should be presented with a gui with lots of buttons and menus and the like. There should also be an "untitled" window with the Maple command prompt. From here you can start entering maple command. You can access the very excellent online help system at any time through the "help" pulldown menu. If you have never used Maple before I'd recommend trying help->contents->introduction, which gives you a good introduction to maple syntax and the like. You can also access help on the command line by typing ? < your function here >.


Basic Command Line Syntax

The Maple command line is pretty easy to use in that when you type in something, it generally gives you back what you would expect (just remember to terminate each command with a semicolon). You basically type in expressions to evaluate, and Maple gives you the answer. These can be algebraic expressions, function calls, matrix definitions, or any one of a number of other things. For example:
* - " refers to last result, "" refers to 2nd to last result, and """ refers to 3rd to last result
Expression Returns Explanation
3 + 4 + 5; 12 Evaluate 3 + 4 + 5
e1 := expand( x*(x+1)*(x-1) ); e1 := x3-x Expand and collect like terms, and assign result to e1
factor( " ); x*(x+1)*(x-1) Factor previous result*
evalf( sqrt(2) ); 1.414213562 Return numeric solution to 21/2
solve(e1=0, x); 0, 1, -1 Return all solutions to e1 = 0
int(x*exp(x), x); x*ex-ex Symbolically integrate xex with respect to x


Useful Stuff

These are just some tricks and useful bits of information that make it easier to bend Maple to your will.

@

@ is Maple's composition operator. This lets you compose functions in a slightly less messy and more readable fashion. There is also a second version of this function, @@, which is the repeated composition operator. This second operator applies a given function an arbitrary number of times, so is useful when you need to repeatedly apply a single function.

Examples:

  • (sin@cos)(x) returns sin(cos(x))
  • (sin@arcsin)(x) returns x
  • (D@@2)(ln) returns a -> -1/a^2
$

$ is Maple's sequencing operator (see ?sequence for more about the use of sequences in Maple). There are three ways to use the operator: expr $ i = m..n, expr $n, and $m..n. The first usage results in a sequence of expressions for expr evaluated at +1 increments i between m and n inclusive. The second usage is similar to the first, but produces the sequence of expressions for expr evaluated at +1 increments between 1 and n inclusive. The last usage is a shorthand for the sequence of integers between n and m inclusive.

Examples:

  • $2..5 returns 2, 3, 4, 5
  • i^2 $ i = 2/3 .. 8/3 returns 4/9, 25/9, 64/9
  • x$4 returns x,x,x,x
Functions

Maple wouldn't be very useful if you couldn't define your own functions. Defining a function makes it easier to perform operations with/on that function, so I suggest that if you are going to be working with a specific mathematical function for any period of time you should define a corresponding Maple function.

Functions in Maple are written in the form vars -> result where vars is a sequence of variable names and result is an expression of some kind. The most general form of a maple function is (x1,x2,...xn) -> (expr1,expr2,...,exprn). Once you've defined your function you can use it by incanting function_name(x1,x2 ,...,xn) on the command line.Thats really all there is to it.

Examples:

  • f := x -> 3*x + 5
  • g := (x,y) -> sin(x)*cos(y) + x*y
  • h := x -> (2*x, x^3)

Algebraic Manipulation

As noted previously, one of maple's strong suits is symbolic manipulation. This makes it ideal for performing operations on messy, complicated algebraic expressions. Some commonly used function follow.

collect(a,x)

The collect function collects coefficients of like powers of x in the polynomial a. Note that x can be a single variable, a list or set of variables, or an algebraic expression.

Examples:

  • collect (a*ln(x)-ln(x)*x-x,ln(x)) returns (a - x)ln(x)-x
  • collect (x^2*exp(x)-2*x*exp(x)+2*exp(x)-x^2/exp(x)-2*x/exp(x)-2/exp(x),x) returns (2+x2-2*x)*ex+(-2*x-2-x2)/ex
  • collect (x*exp(1/(R*C)*t)+y*exp(1/(R*L)*t)+t^2*y+t^3*x,{x,y}) returns (et/(RC)+t3)x+(e1/(RL)+t2)y
convert(expr,form,...)

The convert function converts expr to form. Additional arguments required for some conversions are passed after form. This function is very useful for performing operations such as getting the binary representation of a number or performing a partial fraction decomposition on a polynomial. I counted roughly 70 different types of conversions listed in the online help, more than can be enumerated here. To get an exhaustive listing of all the things that convert can do, type ?convert

Examples:

  • convert (9,binary) returns 1001
  • convert (1.23456,fraction) returns 3858/3125
  • convert ((x^3+x)/(x^2-1),parfrac,x) returns x+1/(x-1)+1/(x+1)
expand(expr)

The expand function expands expr Its primary application is to distribute products over sums, but it also knows how to expand most of the mathematical functions built in to Maple such as sin, cos, factorial, etc.

Examples:

  • expand((x+1)*(x+2)) returns x2+3x+2
  • expand(sin(x+y)) returns sin(x)cos(y)+cos(x)sin(y)
  • expand(exp(a+ln(b)) returns eab
factor(a,k)

The factor function factors a multivariate polynomial. It can handle integer, rational, complex, and algebraic coefficients. This function does not factor integers or integer coefficients (use ifactor to factor integers). The optional second argument is a list of factors which the factor function should factor in terms of.

Examples:

  • factor(6*x^2+18*x-24) returns 6(x+4)(x-1)
  • factor((x^3-y^3)/(x^4-y^4))returns (x2+xy+y2)/((y+x)(x2+y2))
  • factor(x^3+5,{5^(1/3),(-3)^(1/2)})returns (-1/4)(-2x+51/3+I sqrt(3)51/3)(2x-51/3+I sqrt(3)51/3)(x+51/3)
simplify(expr)

simplify is perhaps the most useful of all the functions for algebraic manipulation. It applies a set of simplification rules on expr and (hopefully) returns something that is less hairy that what you started out with.

Examples:

  • simplify(4^(1/2)+3) returns 5
  • simplify(exp(a+ln(b*exp(c)))) returns be(a+c)
  • simplify(cos(x)^5+sin(x)^4+2*cos(x)^2-2*sin(x)^2-cos(2*x)) returns cos(x)5+cos(x)4

Fun With Functions

This section talks about the tools available in Maple for working with functions in n-variables. These include methods for symbolic manipulation and symbolic/numeric solutions to functions.

allvalues(expr)

The allvalues function evaluates all possible values of expressions involving RootOf terms. The most common application of this function is to evaluate expressions returned from solve that contain terms with RootOfs. Typically a RootOf represents more than one value, so expressions involving RootOfs will generally evaluate to more than one value or expression. The allvalues function will return a sequence of all values or expressions generated by the combinations of different values of the RootOfs.

Examples:

  • allvalues(RootOf(_Z^2-1)+1/RoofOf(_Z^4-1)^2) returns 2,2,0,0,0,0,-2,-2
  • allvalues(2*RoofOf(_Z^3-1)-1) returns 1,-2+I31/2,-2-I31/2
  • allvalues(sin(RootOf(_Z^2-a^2))*RootOf(_Z^2-1)) returns sin(a),-sin(a),sin(-a),-sin(-a)
evalf(expr,n)

The evalf function evaluates the given expression using floating point arithmetic. If the optional argument n is given, then evalf will evaluate the expression to n significant figures. This function is useful when an expression doesn't evaluate automatically, which is sometimes the case with expressions involving constants such as Pi, exp(1),gamma, and functions such as exp,ln,sin,arctan,cosh,GAMMA, and erf.

Examples:

  • evalf(Pi) returns 3.141592654
  • evalf(5/3*exp(-2+3*I)*sin(Pi/4),15) returns -.157898022493763+.0225078172647505I
  • evalf(3/4*x^2+1/3*x-sqrt(2)) returns .7500000000*x2+.3333333333*x-1.414213562
fsolve(eqns,vars,options)

The fsolve function is probably one of the most useful features in the entire Maple library. It lets you solve the list of equations eqns for the list of variables vars. In addition you can specify options which affect the behavior of fsolve, the most useful of these options being var=x..y. This lets you specify the range over which you want to find values of the variable var. For a full listing of all options, see ?fsolve.

Examples:

  • fsolve( tan(sin(x))=1, x ) returns 2.238253543
  • fsolve(23*x^5+105*x^4-10*x^2 + 17*x,x,-1..1) returns 0,-.6371813185
  • fsolve({sin(x+y) - exp(x)*y = 0,x^2 - y = 2},{x,y},{x=-1..1,y=-2..0}) returns {y=-1.552838698,x=-.6687012050}
invfunc

invfunc isn't really a function, but you can play with it like it is. invfunc is actually a table in memory that relates functions and inverse functions. It is accessed either by directly addressing the table or by using the @@ operator as shown below. There also exists a table by the name of Invfunc, which returns a multiple valued inverse when the inverse is a multiple valued function.

Examples:

  • sin@@(-1) returns arcsin
  • ln@@(-3) returns e3
  • Invfunc[sin](1) returns 1/2 Pi + 2 Pi _Z1~
isolate(eqn,expr)

The isolate function is used to isolate a subexpression to the left side of an equation. It does this by first trying to find expr in eqn and the solving eqn symbolically for expr. If eqn is not an equation, then the equation eqn=0 is assumed. Note: isolate is not loaded automagically. To use isolate, first execute the command readlib(isolate).

Examples:

  • isolate(4*x*sin(x)=3,sin(x)) returns sin(x) = 3/(4x)
  • isolate(x^2-3*x-5,x^2) returns x2 = 3*x+5
isolve(eqns)

The isolve function solves equations for integer solutions. If there are no integer solutions, or if Maple is unable to find any integer solutions, then NULL is returned.

Examples:

  • isolve(3*x-4*y=7) returns {y = 2+3*_N1, x = 5+4*_N1}
  • isolve(x^2=3) returns nothing
RootOf

The RootOf function is a little odd in that it doesn't actively calculate anything. Rather, it is a place holder for representing all the roots of an equation in a single variable. You end up getting RootOfs a lot of the time when you are solving equations, systems of equations, eigenvalues, and integrals of rational functions. To get actual values out of RootOf, use the allvalues functions (see above or ?allvalues for more information on the allvalues function).


Integration and Differentiation

Maple's abilities with symbolic integration and differentiation are really probably the best thing about the program. You can toss any kind of irritating integral at Maple and get the right answer, with the exception of certain really random things like elliptical functions. The integration and differentiation routine are pre-loaded, so you don't have to worry about opening a library.

D[i](f)

D is the differential operator as defined by Maple. It takes an expression f of n arguments which can be applied as a function and computes the derivative of f with respect to its ith argument. In the most general form of this function, i can be a positive integer or expression or a sequence of positive integers or expressions. In this case, the call D[x1,x2,...,xn](f) computes the derivative of f with respect to x1,x2,...,xn respectively. The D function is particularly useful when doing calculations involving differential equations.

Examples:

  • D(sin) returns cos
  • D(exp+cos^2+Pi+tan) returns exp-2*sin*cos+1+tan2
  • D(x -> g(x,y(x))) returns x -> D1(g)(x,y(x))+D2(g)(x,y(x))*D(y)(x)
diff(a,x1,x2,...,xn)

The diff function computes the partial derivative of the expression a with respect to x1..xn respectively. The most common use of this function is diff(f(x),x) which computes the derivative of the function f(x) with respect to x. A useful trick with diff is to use the sequence operator "$" to get higher order derivative.

Examples:

  • diff(sin(x),x) returns cos(x)
  • diff(sin(x),x$3) returns -cos(x)
  • diff(f(x,y),x,y) returns d2/(dx dy) [f(x,y)]
implicitdiff(f,x,y)

The implicitdiff function computes dx/dy implicitly defined by the function f. f should be an equation in x and y... if an algebraic expression is passed instead, it is interpreted as the equation f=0. There are more advanced usages of this function as well, using systems of functions and multiple variables. For instructions on advanced uses of the implicitdiff function see ?implicitdiff.

Examples:

  • implicitdiff(y=x^2/z,y,x) returns 2*x/z
  • implicitdiff(x^2+y^3=1,y,x) returns -2/3*x/y2
  • implicitdiff(x^2+y^3=1,x,y) returns -3/2*y2/x
int(f,x=a..b)

The int function is used for performing symbolic integration of the function f with respect to the variable x. If no range is specified for x then Maple calculates the indefinite integral of f with respect to x. In the unlikely but not impossible event that Maple cannot find a closed form expression for the integral then the function call itself is returned. If you require a numeric answer, then wrap the call to int in a call to evalf.

Examples:

  • int( sin(x), x ) returns -cos(x)
  • int( sin(x), x=0..Pi ) returns 2
  • int( exp(-x^2)*ln(x), x ) returns int(exp(-x2)*ln(x),x)

Linear Algebra

Maple has a plethora of functions for performing operations on matrices. However, these functions aren't loaded by default so you have to load them yourself. You can do this by typing with(linalg)(linalg is the name of the linear algebra packages, and the with function tells maple to load the package you specify). For a complete listing of all the functions available in the linalg package type ?linalg.

angle(u,v)

The angle function computes the angle between the n-dimensional vectors u and v via the formula cos(theta) = sqrt(u.v/|u||v|) using the Euclidean inner product and norm.

Examples:

  • angle (vector ([1,0]),vector ([0,1])) returns 1/2*pi
  • angle (array ([1,1]),array ([0,1])) returns 1/4*pi
  • angle (vector ([1,0]),vector ([1,0])) returns 0
basis(V)

The basis function finds a basis for the vector space V, where V is a vector, a set of vectors or a list of vectors, or a matrix. If the argument V is a single vector, then this vector will be returns as a set {V}. If V is a set of vectors, then basis will return a basis for the vector space spanned by those vectors in terms of the original vectors.

Examples:

  • basis ({vector([0,1,0]),vector([1,0,0]),vector([0,0,1])})returns {[0,1,0], [1,0,0], [0,0,1]}
  • basis ({vector([1,1,1]),vector([2,2,2]),vector([1,-1,1]),vector([2,-2,2]),vector([1,0,1]),vector([0,1,1])} ) returns {[2,2,2],[2,-2,2],[0,1,1]}
  • basis ({vector([1,0,0]),vector([0,1,0]),vector([0,0,1]),vector([1,1,1])}) returns {[1,0,0],[0,1,0],[0,0,1]
crossprod(u,v)

The crossprod function computes the vector cross-product of u and v, where u and v are both 3 dimensional vectors. Maple defines the cross product as vector([u[2]*v[3]-u[3]*v[2],u[3]*v[1]-u[1]*v[3],u[1]*v[2]-u[2]*v[1]]).

Example:

  • crossprod(vector([1,2,3]),vector([2,3,4])) returns [-1,2,-1]
det(A)

The det function computes the determinant of the matrix A.

Examples:

  • det(matrix(3,3,[1,1/2,1/3,1/2,1/3,1/4,1/3,1/4,1/5])) returns 1/2160
  • det(matrix(2,2))returns B1,1*B2,2-B1,2*B2,1
dotprod(u,v)
The dotprod function computes the vector dot (or scalar) product of the vectors u and v.

Example:

  • dotprod (vector([1,x,y]),vector([1,0,0])) returns 1
eigenvals(A)

The eigenvals function returns a sequence of the eigenvalues of the square matrix A (use ?sequence for more information on working with sequences). If the matrix A contains floating point numbers, the system is solved using numerical methods. If the system is entirely symbolic, Maple solves the characteristic polynomial det(lambda*I-A)=0. One suggestion is that you try to supply as many numbers as possible (try eigenvals(matrix(3,3,[A, B,C,D,E,F,G,H,I]) if you don't believe me).

Examples:

  • eigenvals(matrix(3,3,[1.0,2.0,3.0,1.0,2.0,3.0,2.0,5.0,6.0])) returns 9.321825380, .7700622123e-15, -.3218253805
  • eigenvals(matrix(3,3, [1,2,3,1,2,3,2,5,6])) returns 0, 9/2+1/2*931/2, 9/2-1/2*931/2
eigenvects(A)

The eigenvectors function computes the eigenvalues and eigenvectors of A. To do this it solves the linear system (I*lambda-A)X=0 for each eigenvalue lambda. The result of the computation is returned as a sequence of lists of the form [ei,mi,{v[1,i],...,v[ni,i]}], where ei is an eigenvalue, mi is the eigenvalues algebraic multiplicity, and {v[1,i],...,v[ni,i]} is the set of basis vectors for the eigenspace corresponding to ei. As with the eigenvals function, if there are floating point numbers present in the matrix A then the eigenvectors are computed numerically. Otherwise, they are computed symbolically (exactly). As can be seen from the examples, the results returned by a call to this function can be rather hairy, so I'd recommend saving the result as a list and dealing with it piece by piece (see the first example in ?eigenvects for instruction on how to do this).

Examples:

  • eigenvects(matrix(3,3, [1,-3,3,3,-5,3,6,-6,4])) returns [-2,2,{[1, 1, 0],[-1,0,1]}],[4,1,{[1,1,2]}]
  • eigenvects(matrix(3,3,[1.0,2.0,3.0,2.0,1.0,2.0,3.0,2.0,1.0])) returns [-2.000000001, 1, {[.7071067811,.62e-10,-.7071067810]}],[5.701562119,1,{[.6059128004, .5154991348, .6059128001]}], [-.7015621182,1,{[-.3645129330,.8568900996, -.3645129336]}]
GramSchmidt({v1,v2,...,vn})

The GramSchmidt function computes a list or set of orthogonal vectors from a given list or set of linearly independent vectors using the GramSchmidt orthogonalization process. The vectors given must be linearly independent, otherwise the vectors returned will also be dependent. This function is really useful for finding an orthogonal basis for a vector space if you already know a basis.

Examples:

  • GramSchmidt({vector([1,0,0]),vector([1,1,0]),vector([1,1,1])}) returns {[1,0,0],[0,1,0],[0,0,1]}
  • GramSchmidt([vector([2,2,2]),vector([0,2,2]),vector([0,0,2])]) returns [[2,2,2],[-4/3,2/3,2/3],[0,-1,1]]
linsolve(A,b)

The linsolve function finds the vector x which satisfies the matrix equation Ax=b. If A has n rows and m columns, then b must be of dimension n and the solution x will be of dimension m, assuming a solution exists. If Ax=b has no solution, then the null sequence NULL is returns. If Ax=b has many solutions, then the results will use global names to describe the family of solutions parametrically (see ?linsolve for more information on how to use global names).

Examples:

  • linsolve(matrix([[1,2],[1,3]]),vector( [1,-2])) returns [7,-3]
  • linsolve(matrix([[5,7],[0,0]]),vector( [3,0] )) returns [3/5-7/5_t[1],_t[1]]
matadd(A,B,c1,c2)

The matadd function adds two matrices or vectors of equivalent dimension. If optional argument c1 and c2 are provided then the matadd function returns c1A+c2B.

Examples:

  • matadd (matrix(3,3,[1,2,3,2,3,4,3,4,5]),matrix(3,3,[1,0,0,0,1,0,0,0,1])) returns [[11 2 3],[2 13 4],[3 4 15]]
  • matadd (vector(3, [2,3,4]),vector(3, [-3,1,-5])) returns [8,1,14]
  • matadd (matrix(2,2,[1,2,3,4]),matrix(2,2,[5,6,7,8]),2,3) returns [[17,22],[27,32]]
multiply(A,B,...)

The multiply function multiplies an arbitrary number of matrices A,B,... . The dimensions of each matrix must be consistent with the rules of matrix multiplication. The multiply function also works with vectors as long as the vector has the correct dimension.

Examples:

  • multiply (array([[1,2],[3,4]]),array([[0,1,],[1,0]]),array ([[1,2],[4,5]])) returns [[6 9][16 23]]
  • multiply (array([[1,2],[3,4]]),vector([3,4])) returns [11,25]
  • multiply (vector([3,4]),transpose(vector([3,4]))) returns [[9 12][12 16]]
transpose(a)

The transpose function computes the transpose of a matrix. Given an nxm matrix A this function returns the mxn matrix B such that A[i,j] = B[j,i]. This command also works with vectors, so given an nx1 vector it would returns a 1xn vector.

Example:

  • transpose (array([[1,2,3],[4,5]])) returns [[1 4][2 5][3 A2,3]]
wronskian(f,v)

The wronskian function computes the wronskian matrix of f with respect to v, where f is a vector or list of functions and v is a variable. The (i.j)th entry of the result is given by diff(f[j],v$(i-1)), which basically returns the (i-1) th derivative of f[j] with respect to v.

Example:

  • wronskian(vector([exp(x),cosh(x),sinh(x)]),x) returns [[ex cosh(x) sinh(x)][ex sinh(x) cosh(x)][ex cosh(x) sinh(x)]]

Differential Equations

Another thing that Maple is pretty good at is working with ODE's and PDE's. In fact, Maple has an entire toolkit devoted to working with the nasty buggers called DEtools. Most of the stuff available in this toolkit is way beyond the scope of an intro document, so I'm not going to cover it here except for a couple of specific functions. I'd recommend seeing ?DEtools to get an idea of the extent of Maple's DE handling abilities. Anyway, Maple has nice basic functions which should be enough for anyone in Math 82 for plotting and solving ODE's and PDE's.

DEplot(deqns,vars,trange,inits,eqns)

The DEplot function is part of the DEtools package, so in order to use it you have to issue the with (DEtools) command first. This function lets you plot the solutions to the list of first order DE's deqns (or a single DE of any order). In addition to specifying the equation or equations to plot you must also specify the dependent variable(s) vars, the range(s) tranges to plot over, and the initial conditions inits. The last argument eqns is a sequence of optional equations which govern the behavior of the plotting method and the way in which the plot of the ODE is displayed. Incanting this function is fairly complicated, so I'd recommend that you see ?DEplot for a full list of options available. The DEtools package also has the function DEplot3d for plotting ODE's in 3-space. This function behaves in much the same manner as DEplot. For a complete description see ?DEplot3d.

dsolve(deqns,vars,eqns)

The dsolve funtion is used for finding closed-form solutions to many kinds of ODE's. Given ODE or set of ODE's deqns and the variable or set of variables vars to be solved for dsolve will try to come up with an exact solution to the system. The third arguments eqns is a sequence of optional equations which control the behavior of the dsolve function (see ?dsolve for a complete list of options). Maple also provides the pdesolve function for finding solutions to PDE's which behaves in an almost identical manner (for the subtle differences, see ?pdesolve).

Examples:

  • dsolve(diff(y(x),x$2) - y(x) = sin(x)*x, y(x)) returns y(x) = -1/2*cos(x)-1/2*sin(x)*x+_C1*ex+_C2*e-x
  • dsolve(t*diff(y(t),t)-t^2*y(t)=sin(t),y(t)) returns y(t) = e1/2*t^2*(int(e-1/2*t^2*sin(t)/t,t)+_C1)
  • dsolve({diff(v(t),t)+2*t=0, v(1)=5}, v(t)) returns v(t) = -t2+6
rsolve(eqns,fcns)

The rsolve function has nothing to do with DE's but rather with a similar type of equation called a difference or recurrence equation. This function attempts to solve the recurrence relation(s) specified by eqns for the function(s) in fcns, returing an expression for the general term of the function. eqns should be a single recurrence relation or a set of recurrence relations and boundary conditions. And expressions in eqns which are not equations will be interpreted as the equation expr=0. fcns indicates the functions that rsolve should solve for and return solutions in terms of.


Copyright (c) HMC Computer Science Department. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License.''

HMC Computer Science Department
Contact Information
Last Modified Tuesday, 22-May-2001 16:48:27 PDT