Maple Book 2010

Embed Size (px)

Citation preview

  • 8/2/2019 Maple Book 2010

    1/36

    1

    Introduction to Maple

    Autumn 2010

    Mathematics, University of Exeter

  • 8/2/2019 Maple Book 2010

    2/36

    2

    Contents

    1 Using MAPLE 41.1 What is MAPLE? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.2 What can MAPLE do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41.3 How do I access MAPLE from Windows? . . . . . . . . . . . . . . . . . . . . 41.4 How do I get MAPLE to work? . . . . . . . . . . . . . . . . . . . . . . . . . 51.5 Inserting comments in Maple . . . . . . . . . . . . . . . . . . . . . . . . . . 61.6 Execution groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.7 On line help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61.8 Saving your Work on Disk . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71.9 Restarting MAPLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    2 Examples of Uses of MAPLE 72.1 Recalling Previous Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.2 Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.3 Floating Point Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.4 Unevaluated Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92.5 Complex Numbers and Arithmetic . . . . . . . . . . . . . . . . . . . . . . . 92.6 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.7 Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112.8 Elementary Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132.9 Rational Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

    2.10 P lotting Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142.11 Solving Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152.12 Finding Complex Roots of Equations . . . . . . . . . . . . . . . . . . . . . . 17

    3 Basic Calculus 183.1 Functions in Maple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.2 Definition of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193.3 Functions of More than One Variable . . . . . . . . . . . . . . . . . . . . . . 203.4 Differentiation of a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.5 Composite Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    4 Further use of Maple 224.1 Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224.2 Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234.3 Taylor Series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234.4 Vectors and Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.5 Sums and Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

  • 8/2/2019 Maple Book 2010

    3/36

    3

    5 Expression Sequences, Lists and Sets 265.1 Expression Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

    5.2 Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275.3 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275.4 Conversion from lists to sets . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

    6 The Student Package 306.1 Integration by Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306.2 Integration by Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    7 Elementary programming in MAPLE 327.1 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327.2 Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

    A FAQ and Common Mistakes 36

    [email protected]

    September 16, 2010

  • 8/2/2019 Maple Book 2010

    4/36

    4

    1 Using MAPLE

    1.1 What is MAPLE?

    It is well known that computers can perform numerical calculations very quickly but over thelast few years it has also become possible to use computers to perform algebraic calculationsusing symbols as well as numbers. MAPLE is one of the most powerful of these systems andit will be used in several of your courses throughout the next three or four years.

    In this module you will study some of the basic elements of Maple. You will find that allof the other Mathematics courses will use Maple throughout the year. This course will notonly teach you something about Maple but it will also serve as a means of revising some ofthe Mathematics which you have seen at A-level.

    NB If you find any errors or have any comments on this booklet, please emailthem to me at [email protected]. Thanks!

    1.2 What can MAPLE do?

    The following list gives a few of the many features of the MAPLE system.

    It can calculate with up to thousands of digits using exact or floating point arithmetic.

    It can manipulate formulae involving polynomials, trigonometric functions, logarithmsand many so called special functions which you will encounter later in your degreecourse.

    It can solve systems of equations. It can differentiate, integrate and solve differential equations. It can plot graphs in 2 and 3 dimensions. It can manipulate matrices and vectors with symbolic or numeric entries.

    1.3 How do I access MAPLE from Windows?

    (i) Click on Start in the bottom left hand corner of the screen.

    (ii) Move the cursor to the arrow next to Programs and you will find that a menu appears.

  • 8/2/2019 Maple Book 2010

    5/36

    5

    (iii) Move the cursor to Programming, Maths and Stats Programmes and you willfind Maple. Move your cursor on to this and double click the left button of the mouse

    and wait while the MAPLE software is loaded. You now have a worksheet in front ofyou and can begin to type in your commands.

    1.4 How do I get MAPLE to work?

    Note you can use MAPLE either in worksheet mode or in document mode. We assume herethat you use the former; note that you can get a worksheet from File >New >Worksheetmode.

    Now that we have the MAPLE worksheet in front of us we want to get it to do something.

    You will notice that whenever the system is waiting for instructions it prompts you with a> sign and a thin vertical line. The line tells you where the text will appear when you starttyping. In these notes, when you see

    > anything;

    this means that anything is the MAPLE command you type in. The result will be given byMAPLE in the centre of the next line. Here is an example.

    > 2+2;4

    This means that you type in the two, the plus, the second two and the semi-colon (;) andthen press Return and wait for MAPLE to give the result. The semi-colon tells MAPLEwhen you have finished typing. To begin with your most common mistake will be to forgetthe semi-colon. You can spread your expression over as many lines as you wish, ending eachone with an Return. Use the arrow keys or mouse to move around within the expressionor within the worksheet.

    MAPLE will do nothing until it sees the semi-colon followed by Return. You can put morethan one calculation on one line, for example

    > 3 + 4; 4 2;

    7

    2

    If MAPLE says that the statement you have typed is has an error, you can use the cursorto go back to that statement, edit it and press return again to re-execute it.

  • 8/2/2019 Maple Book 2010

    6/36

    6

    1.5 Inserting comments in Maple

    Adding comments to your worksheet can be very helpful. These are lines that start with the# hash symbol, for example:

    > #This line will be ignored!

    > This line will produce an error!

    (note that you dont need a : or ; at the end of a comment). You will find it very helpfulto add comments, especially to sheets that you save and may want to refer to again later.

    1.6 Execution groups

    You may notice that Maple groups statements and their answers together using squarebackets of variable size on the extreme left hand side of the worksheet. These are calledexecution groups. If you press Return anywhere within an execution group, all commandsin the execution group will be executed by MAPLE. You can put more commands in anexecution group by typing SHIFT-Return at the end of the line and Return only whenyou have got to the end of the group.

    Useful commands include being able to join two execution groups or to split them; go to theEDIT menu at the top and select Split or Join to get several options for splitting/joininggroups. If you would like to add an execution group between two that you have alreadytyped in, go to the INSERT menu at the top and then Execution Group. You can addone either before or after your current location (cursor).

    1.7 On line help

    One of the most useful features of MAPLE is the Help facility. You can use this in one oftwo ways. Firstly it is possible to use the pull down menus at the top of the screen whereyou can obtain information on the whole system. To do this, move the cursor up to theHelp menu and hold down the left button on the mouse. Now, while still holding the mousebutton down, move the cursor on to Browser and release the button. You can now select

    any topic on which you want help by clicking on that topic. If you want specific informationon some keyword, say for example the word expand, but are unsure how to use it then youneed not use the Help menu but just type

    > ?expand;

    A window will appear giving information on that subject along with examples of its use.Note that you can copy and paste the examples into your own worksheet to play around

  • 8/2/2019 Maple Book 2010

    7/36

    7

    with them to get a better idea of how they work.

    Be prepared to use this method a lot in your early experiences of MAPLE!

    1.8 Saving your Work on Disk

    When you have finished your session on MAPLE you may wish to save it in order to use iton another occasion. To do this either on permanent disc space or on your pen drive, seethe appendices at the end of this booklet.

    1.9 Restarting MAPLE

    As you execute MAPLE statements in a worksheet, MAPLE will remember the definitionsand results you have generated; if you wish that MAPLE forgets all definitions and startswith its memory empty, type

    > restart;

    This will not remove any text that you have typed in, but all statements will not be executeduntil, for example, you move the cursor to the start of the worksheet and execute each lineby typing Return on each line.

    2 Examples of Uses of MAPLE

    MAPLE uses + and for addition and subtraction. It uses for multiplication, / for divisionand for exponentiation. Round brackets are used to indicate precedence. Hence 4*(2+3)will evaluate to 20. Whenever multiplication is implied you must put the * operator. 4(2+3)is not the same thing and actually evaluates to 4 so be careful. If you make a mistake withyour typing, you do not need to type everything in again. You can click the mouse so thatthe cursor is where you want to add or delete the text and just alter the text. If you wantto copy something similar to a previous expression use the cut and paste facility under theEdit menu.

    2.1 Recalling Previous Answers

    Maple uses the symbol percent, i.e. % which is obtained by using Shift and 2, as a meansof referring to the previous result. Double percent i.e. %% refers to the answer before thatand %%% refers to the one before that. You cannot go back further than that. Hence

  • 8/2/2019 Maple Book 2010

    8/36

    8

    > 2 + 3;5

    >6^2;36

    > %;36

    > %% %%%;31

    2.2 Integer Arithmetic

    The symbol ! is used for factorial. Unlike a calculator, there is virtually no limit to thenumber of digits that can be used.

    > 100!;

    will produce the exact answer.

    Some of the keywords which will be useful in this area are

    > igcd(4,12);4

    which gives the greatest common divisor

    > ilcm(8,12);24

    which gives the least common multiple

    > ifactor(36);(2)2(3)2

    will factorise an integer into its prime factors.

    If n is a specific integer then some other useful keywords are ithprime(n) which returnsthe nth prime number, nextprime(n) which gives the smallest prime number greater than

    n and prevprime(n) which gives the largest prime less than n. You can test whether anumber is prime by using isprime(n) which tests to see whether n is prime and returns ananswer true or false.

  • 8/2/2019 Maple Book 2010

    9/36

    9

    2.3 Floating Point Arithmetic

    Pi stands for = 3.14159......exp(1) stands for e = 2.718.....

    These are two of the most used constants throughout mathematics.

    evalf(x) stands for evaluate x in floating point

    evalf(x,n) evaluates x to n significant figures.

    The default is that the system works to 10 significant figures. To change this type

    >Digits:=18;

    Now all floating point operations are evaluated to 18 significant figures. Note that there is

    a colon before the equals sign. This is vital and you will see it used again later.

    2.4 Unevaluated Expressions

    If you perform a calculation which has an exact answer, MAPLE will not evaluate it unlessyou specifically ask for it. For example

    > Pi^2+exp(1);2 + exp(1)

    2.5 Complex Numbers and Arithmetic

    In MAPLE the letter I is a reserved character and stands for sqrt(-1), the basis of complexnumbers. You can perform complex operations in MAPLE in just the same way as for realarithmetic. As before dont forget to use the * for all multiplications. An example is

    > (3+2*I)*(2-4*I);14 8I

    Complex numbers can be set up as follows.

    > z1:=1+I;

    z1 := 1 + I

    >z2:=3+4*I;

    z2 := 3 + 4I

  • 8/2/2019 Maple Book 2010

    10/36

    10

    The sum, difference, product and quotient can be obtained by use of the standard math-ematical operators. In the remainder of this section, cplex represents a general complex

    number.The most important commands in Maple for use in conjunction with complex numbers are

    Re(cplex), which gives the real part of cplex

    > Re(z2);3

    Im(cplex), which gives the imaginary part of cplex

    > Im(z1*z2);7

    argument(cplex), which gives the argument of cplex

    > argument(z1);1

    4

    > argument(z2);

    arctan(4

    3)

    abs(cplex), which gives the magnitude or modulus of cplex

    >abs(z2);5

    conjugate(cplex), gives the complex conjugate of cplex

    > conjugate(2-3*I);2 + 3I

    polar(r,t), is a complex number with r as magnitude and t as argument

    > z3:=polar(2,Pi/3);

    z3 := polar(2,1

    3)

    convert(z,polar), converts z into its polar form

    > convert(z1,polar);

    polar(

    (2),1

    4)

    evalc(cplex), attempts to find the real and imaginary parts of cplex

  • 8/2/2019 Maple Book 2010

    11/36

    11

    > z4:=evalc(z3);

    z4 := 1 + I

    (3)

    evalf can be used to produce floating point answers

    > evalf(z4);1. + 1.732050808I

    sin(1 + I);

    will not be expanded unless you ask for it to be. In complex arithmetic use evalc in orderto perform complex evaluations. Hence

    > evalc(sin(1+I));sin(1) cosh(1) + Icos(1) sinh(1)

    > evalf(%);

    will evaluate this.

    2.6 Polynomials

    So far everything we have seen has involved a calculation with numbers. MAPLE is perfectlyhappy to manipulate symbols.

    > x^2+3*x+2;

    x2 + 3x + 2

    Manipulations can be performed on this such as factorising by using the keyword factor.

    > factor(%);(x + 2)(x + 1)

    and the reverse process uses the keyword expand

    > expand(%);x2 + 3x + 2

    2.7 Labels

    It is often useful to be able to refer back to expressions, possibly outside the range allowedby the % notation in the previous section. In order to do this we use the := symbol to attacha label to an expression.

  • 8/2/2019 Maple Book 2010

    12/36

    12

    > f:=x^2+3*x;f := x2 + 3x

    > g:=factor(f);g := x(x + 3)

    > expand(g);x2 + 3x

    A value can also be assigned to a variable, as we did above with Digits (which is a systemvariable), and whenever this variable is referred to in an expression the value is used.

    > x:=1;x := 1

    With the definition of g as above this would produce

    > g;4

    In order to unassign a name if you wish to use it again as a variable we use

    > x:=x;

    x is now again the variable x and

    > g;x(x + 3)

    The labelled expression

    > f:=x^2+3*x;f := x2 + 3x

    looks as though f has been defined as a function of z. This is, however, not a true function (itis sometimes called an apparent function) because the parameter x is fixed. That is, as wesaw above, f(y) or f(2) is not allowed, we have to assign a value to x or to do a substitution,using subs. Thus

    > f:=x^2+3*x;

    f := x2 + 3x

    > x:=1;x := 1

    > f;4

  • 8/2/2019 Maple Book 2010

    13/36

    13

    or

    > f:=x^2+3*x;

    f := x2 + 3x

    > subs(x=1,f);4

    The obvious way of defining functions, f(x):=, does not work because when Maple sees sayf(x):= 3*x, Maple stores in a table the information that f, in the special case when theargument is x, gives the result 3x. If you now ask for f(2) or f(y) then Maple does not knowwhat to do with the argument and you get

    > f(x):=x^2+3*x;

    f(x) := x2

    + 3x

    > f(x),f(y),f(2);3x, f(y), f(2)

    We will discuss about ways of defining functions in the next sections, but remember for nowthat

    > f:=x^2+3*x;f := x2 + 3x

    defines an expression and NOT a function.

    2.8 Elementary Functions

    MAPLE recognises all of the standard trigonometric functions. sin(x) will give the valueof the sine of x, with x in radians. You can use the other trigonometric functions in asimilar way. Inverse trigonometric functions are also available. For example arcsin(x) andarctan(x) will return the angle in radians within the range [

    2, 2

    ]. Similar restrictions willapply to the other inverse functions.

    sqrt(x) stands for

    x

    exp(x) represents ex

    log(x) and ln(x) both represent log to base e

    log10(x) is the log to base 10

    Many other functions are also available but those shown above will be the ones which youwill see most of during your first year. MAPLE uses many of the trigonometric identitieswhen dealing with this type of function.

  • 8/2/2019 Maple Book 2010

    14/36

    14

    Three of the MAPLE keywords which are useful in conjunction with functions are simplify,normal and factor. Examples are given.

    > f:=sin(2*x)+cos(3*x);f := sin(2x) + cos(3x)

    > simplify(f);2sin(x) cos(x) + 4 cos(x)3 3cos x

    > factor(%);cos(x)(2 sin(x) + 4 cos(x)2 3)

    > normal((x^2-y^2)/(x-y)^3);(x + y)

    (x

    y)2

    2.9 Rational Functions

    These can be defined as follows

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

    g :=(x 1)

    (x + 1)(x 2)A very useful keyword which will express this in partial fraction form is convert

    > convert(g,parfrac,x);

    23

    1x + 1

    + 13

    1x 2

    2.10 Plotting Graphs

    Another very useful aspect of MAPLE is its ability to plot graphs. The keyword for producinga graph is plot. In order to obtain a graph of, say, y = x2 between x = 2 and x = 3 youmust type

    > plot(x^2,x=-2..3);

    The graph appears drawn on the worksheet. If you click on the graph, a box appears roundit. You can find the coordinates of any point on the graph by moving the cursor to the pointand click the left mouse button. With your graph boxed, investigate the functions of the rowof buttons on the screen immediately above the workspace. To close i.e. remove the box, just click on a command line on the worksheet. For multiple plots i.e. several functions onthe same graph, use plot:

    > plot({y,y^2,y^3},y=-1..1);

  • 8/2/2019 Maple Book 2010

    15/36

    15

    The graphs should appear each in a different colour. The range can also be specified as

    > plot(tan(x) x 2*Pi .2*Pi, 2. .2);

    To plot 3 dimensional pictures, use plot3d:

    > plot3d(x^3*sin(3*y),x=0..2,y=0..4);

    Box this plot by clicking on it, and then again investigate the functions of the row of buttonson the screen immediately above the workspace. Then try clicking on Style, Colour, Axesand Projection above that, and investigate how these menus work.

    2.11 Solving Equations

    The keyword solve enables one or more equations to be solved.> solve(x^2+3*x+2=0);

    1,2

    If you want to solve a system of equations the the following example shows the method.

    > solve({x-2*y=7,3*x-y=6},{x,y});

    {y = 3, x = 1}

    > solve(x=cos(x));

    produces nothing but the prompt for the next input. This is because there is no exactsolution. If you plot a graph of x and cos(x) on the same graph you will see a root between0 and 1. The equation can be solved by using the keyword fsolve which is the floating pointsolve

    > fsolve(x=cos(x),x);0.7390851332

    Examples of using solve Symbolic solving

    > solve(f=m*a,a); f

    m

    Equation solving

    > eq:=x^4-5*x^2+6*x=2;eq := x4 5x2 + 6x = 2;

  • 8/2/2019 Maple Book 2010

    16/36

    16

    > solve(eq,x);1, 1,1 +

    3,1

    3

    > sols:=[solve(eq,x)];

    sols := [1, 1,1 +

    3,1

    3];

    > sols[3];1 +

    3

    > subs( x=sols[3],eq);2 = 2

    Systems of equations

    > eqns:=u+v+w=1,3*u+v=3,u-2*v-w=0;

    eqns := u + v + w = 1, 3 u + v = 3, u 2 v w = 0

    > sols:=solve(eqns);

    w = 1, u = 12

    , v =3

    3

    check solutions:

    > subs(sols,eqns);

    1 = 1, 3 = 3, 0 = 0Pick of one component of solution

    > subs(sols, u);4

    5

    Assign solutions

    > assign(sols);

    > u;4

    5

    Other examples;

    > solve(x^5-3*x^4+2*x^2-x+3,x);

    RootOf( Z5 3 Z4 + 2 Z2 Z+ 3, index = 1), RootOf( Z5 3 Z4 + 2 Z2 Z+ 3, index = 2),RootOf( Z5 3 Z4 + 2 Z2 Z+ 3, index = 3), RootOf( Z5 3 Z4 + 2 Z2 Z+ 3, index = 4),

    RootOf( Z5 3 Z4 + 2 Z2 Z+ 3,index = 5)

  • 8/2/2019 Maple Book 2010

    17/36

    17

    > evalf(%);

    1.327862375, 2.725868853, 0.03687403922 + .8565945569I,

    1.127479307, 0.03687403922 .8565945569I

    > solve( x^2*y^2=0,x-y=1);

    y = 0, x = 1, y = 0, x = 1, y = 1, x = 0, y = 1, x = 0

    > solve( x^2*y^2=0,x-y=1, x0);

    y = 0, x = 1, y = 0, x = 1

    2.12 Finding Complex Roots of Equations

    If you use solve to solve a polynomial equation then if the roots are complex, it will producethe results in the form of a complex number using the standard Maple notation a + Ib

    > solve(x^2+x+1=0,x);

    12

    +1

    2I

    3,12 1

    2I

    3

    If the roots need to be found numerically by using fsolve then you will need to tell Maple

    that you are looking for complex roots.

    > f:=x^4+4*x^3-8*x^2+32*x-5;

    f := x4 + 4x3 8x2 + 32x 5

    > fsolve(f=0,x);6.162277660, .1622776602

    Clearly only the two real roots have been found. If you also require the complex roots thenyou must use

    > fsolve(f=0,x,complex);

    6.162277660, .1622776602, 1. 2.I, 1. + 2.I

  • 8/2/2019 Maple Book 2010

    18/36

    18

    3 Basic Calculus

    The keywords diff and int are used for differentiation and integration respectively.

    > diff(x^2+log(x),x);

    2x +1

    x

    > int(x*exp(x),x);xex ex

    In each case the second argument tells the system which variable you are differentiating orintegrating with respect to. If you need to evaluate a definite integral then use

    > int(x^2,x=0..1); 1

    3

    If you give it an integral which it finds difficulty in evaluating, then it will merely displaythe integral.

    > int(exp(cos(x)),x=0..1); 10

    ecos(x)dx

    You can now use the evalf command to obtain a numerical approximation.

    > evalf(%);

    To set up an integral, but not actually integrate it, use Int.

    > Int(x^3,x); x3 dx

    To evaluate it

    > value(%);1

    4x4

    Note the following:

    > Int(x^2,x=1..3); 31

    x2 dx

    > value(%);26

    3

  • 8/2/2019 Maple Book 2010

    19/36

  • 8/2/2019 Maple Book 2010

    20/36

    20

    An alternative way of defining an operator is to use the unapply command. Examples are:

    > f:=unapply(x^2,x);

    f := (x > x)2

    > f(3);9

    > y:=x^3+2*x+4;y := x3 + 2x + 4

    > g:=unapply(y,x);g := x > x3 + 2x + 4

    3.3 Functions of More than One Variable

    The same method can be used to define functions of more than one variable. For exampleto define f(x, y) = x2 y2 the Maple command is> f:=(x,y)->x^2-y^2;

    f := (x, y) > x2 y2

    > f(u,v);u2

    v2

    3.4 Differentiation of a Function

    We have seen already that we use the command diff(y,x) to differentiate the expression ywith respect to x. In order to differentiate a function we use the operator D

    > f:=x->x^2;f := x > x2

    > g:=D(f);

    g := x > 2xHowever, if we use

    > D(f)(x);2x

    this produces an expression, since it is evaluated at x.

  • 8/2/2019 Maple Book 2010

    21/36

    21

    If we define

    > f:=(x,y)->x^4-y^4;

    (x, y) > x4 y4

    then D[1](f) produces the function based on the derivative of f with respect to the firstvariable, namely x. D[1,2](f) produces the function based on the derivative with respect tox followed by y. The $ symbol can be used as in differentiation of an expression to signifydifferentiation several times with respect to that variable. Hence

    > D[1$4,2$3](f);

    will produce a function based on differentiating the function f, 4 times with repect to xfollowed by 3 times with respect to y.

    > D[2,2](f);(x, y) > 12y2

    > D[2$4](f);(x, y) > 24

    3.5 Composite Functions

    If we have two functions defined as f(x) and g(x) then the composition f(g(x)) is writtenin Maple as (f@g)(x)

    > f:=x->x^2;f := x > x2

    > g:=x->x+3;g := x > x + 3

    > (f@g)(x);(x + 3)2

    > (g@f)(u);u2 + 3

    If the function fcn is repeated n times this can be abreviated to fcn @@ n

    > (sin@@3)(z);sin(3)(z)

    This looks to be a strange notation and we can see exactly what it stands for if we expandit by using

  • 8/2/2019 Maple Book 2010

    22/36

    22

    > expand(%);sin(sin(sin(z)))

    The differentiation operator for functions, D, can also be repeated in the same way. If f isdefined as

    > f:=x->x^4;f := x > x4

    then the second derivative of the function f evaluated at x is given by

    > (D@@2)(f)(x);

    With the above definition of f this gives

    12x2

    4 Further use of Maple

    4.1 Differential Equations

    In MAPLE y(x) means a function y which depends on x. When solving a differential equationyou will be trying to determine what function satisfies a specific differential equation. If you

    need to solve dy

    dx= xy

    then this would be written as

    >de1:=diff(y(x),x)=x*y(x);

    de1 :=

    xy(x) = xy(x)

    In order to solve it you use the keyword dsolve

    > dsolve(de1,y(x)); y(x) = e1

    2x2

    C1

    where C1 is an arbitrary constant.

  • 8/2/2019 Maple Book 2010

    23/36

    23

    4.2 Limits

    Another very useful tool in Maple is the limit command. Again you will spend much moretime on this topic in other courses. The command gives you the limiting value of an expres-sion as the variable tends to a specific value. This is usually used when an expression is ofthe form 0/0 or /. The general form is> limit(expr,x=a);

    where expr is the expression whose value is required as x tends to a. An example is

    > limit(sin(x)/x,x=0);

    This is a well known limit which will produce the answer

    1

    The limit of an expression as x tends to infinity can also be found:

    > limit((4*x^2 + 3*x +7)/(25*x^2 + 9), x=infinity);

    4

    25

    Left and right limits can also be found:

    > limit(l/x^2, x=0);

    but

    > limit(l/x, x=0);undefined

    > limit(1/x, x=0, left);

    > limit(1/x, x=0, right);

    4.3 Taylor Series

    The command taylor calculates a truncated Taylor series. You will study the theory ofTaylor series in more detail in your other courses. However you may have seen some simpleexamples of Taylor series already. The most well used are those for ex, sin x and cos x. The

  • 8/2/2019 Maple Book 2010

    24/36

    24

    Taylor series is a power series and it will always be displayed in Maple up to a certain orderwhich tells you which is the next term in the series which is not explicitly evaluated. The

    default in the system is for the order to be 6. We will see what this means with an example.The exponential series can be looked at by using

    > taylor(exp(x),x);

    and this produces

    1 + x +1

    2x2 +

    1

    6x3 +

    1

    24x4 +

    1

    120x5 + O(x6)

    The general form of the taylor command is

    > taylor(expr,x=a,n);

    where you want the Taylor series for expr about x = a up to terms of order xn. A Maclaurinseries is a Taylor series about x = 0.

    It is often useful to get rid of the order term from a taylor command because you will findthat you cannot plot such an expression. This can be done by using

    > convert(%,polynom);

    where % refers to the Taylor expansion on the previous line. This converts it to a polynomialby getting rid of the order term. This new expression may now be plotted.

    4.4 Vectors and Matrices

    All of the keywords used in this section are contained in a separate package in MAPLE andthis must be loaded before you can use it. The command is very simple. All you have to dois to type in

    > with(linalg);

    The most important keywords in this section are vector and matrix which set up exactlywhat they say. Examples of each are given.

    > v:=vector([1,3,7]);

    This will produce a 3-dimensional vector. If you just type

    > v;

    you will not be given all of the entries of v but merely the name v itself. If you wish to seethe entries in v on the screen you must type

    > print(v); or > evalm(v);

  • 8/2/2019 Maple Book 2010

    25/36

    25

    If a and b are defined as 2 vectors then the keywords dotprod(a,b) and crossprod(a,b)will produce the scalar and vector products respectively of these vectors.

    In a similar way matrices may be defined. In this case you need to specify 3 facts. Theseare the number of rows, the number of columns and the entries row by row of the matrix.Again an example will show how this is done.

    > A:=matrix(2,3,[1,3,5,0,3,1]);

    This sets up a matrix with 2 rows and 3 columns with elements 1 3 5 in the first row and 03 1 in the second row. Calculations involving matrices can be performed using evalm whichmeans evaluate matrix. For addition and subtraction use the normal + and . However,for multiplication you must use &* and this operator must always have spaces around it, i.e.use A & * B and not A&*B. The command to produce a product of two matrices is

    > evalm(A &* B);

    inverse and transpose will perform those operations on a matrix by using

    > inverse(A);

    > transpose(A);

    There are many other operations that can be used after typing with(linalg); for exampledet can be used to find the determinant of a matrix. Other operations include the elementaryrow and column operations and reduction to row echelon form rref(). More details are givenin the Vector and Matrices module.

    4.5 Sums and Products

    Maple is able to calculate finite sums and products and is sometimes able to work out infiniteones. The general form of the sum command is

    > sum(expr,i=a..b);

    where expr is an expression and a and b are integers. A few examples are

    > sum(i^2,i=1..10);385

    > sum(1/n!,n=0..infinity);exp(1)

    If Maple cannot find a closed form for a sum then it is returned unevaluated and just as withintegrals you can use evalf to obtain a numerical answer. The general form for products isvery similar. It is

  • 8/2/2019 Maple Book 2010

    26/36

    26

    > product(expr,i=a..b);

    An example of this is

    >product(n,n=1..5);120

    5 Expression Sequences, Lists and Sets

    Quite often it is necessary to deal with more than one expression at once. For examplewhen solving a quadratic equation we obtain two solutions. Maple has a variety of ways ofbundling expressions together.

    5.1 Expression Sequences

    The simplest of these is the expression sequence. This is entered simply by writing each ofthe expressions separated by a comma.

    > 1,2,3;1, 2, 3

    > x,y;x, y

    > x,(x+y)^2,3^4;x, (x + y)2, 81

    Expression sequences may be assigned to variables.

    > f:=a,b,c;f := a,b,c

    > solve(x^2+3*x+2=0,x);

    1,

    2

    >sol:=%;sol := 1,2

    Expression sequences may be concatenated

    > f,sol;a,b,c,1,2

  • 8/2/2019 Maple Book 2010

    27/36

    27

    The null sequence is generated by the keyword NULL and nothing is shown on the screenas shown below.

    > x:=NULL;x :=

    5.2 Lists

    A list is an expression sequence written in square brackets.

    > x:=[1,2,3];x := [1, 2, 3]

    The main difference between expression sequences is seen when they are concatenated.> y:=[a,b];

    y := [a, b]

    > x,y;[1, 2, 3], [a, b]

    The separation is still maintained unlike in the case of the expression sequence. A numericallist can be sorted by using the sort command.

    > A:=[1,3,-2,0,7];

    A := [1, 3,2, 0, 7]> sort(A);

    [2, 0, 1, 3, 7]

    When using sort on a list of names Maple sorts them into alphabetical order.

    > sort([red,green,blue]);[blue, green, red]

    The empty list is denoted by [ ]

    5.3 Sets

    A set is an expression sequence written in curly brackets.

    > x:={1,2,3};x := {1, 2, 3}

  • 8/2/2019 Maple Book 2010

    28/36

    28

    Sets are more complicated than lists in that there are some automatic simplification rules.Sets are not ordered and they may not contain duplicates.

    > {1,2,3,2,1};{1, 2, 3}

    > {0,2,1};{0, 1, 2}

    There are three operations defined on sets, namely union, intersect and minus which workin the same way as the mathematical definitions.

    > a:={1,2,3};a := {1, 2, 3}

    > b:={3,4,5};b := {3, 4, 5}

    > a union b;{1, 2, 3, 4, 5}

    > a intersect b;{3}

    > a minus b;

    {1, 2}> b minus a;

    {4, 5}

    The empty set is denoted by { }To extract part of an expression sequence, a list or a set we use a selection operation. If A isa variable containing a list or set or expression sequence then A[1] returns its first operand,A[2] its second and so on. Extraction is only possible when the list or set or sequence hasbeen assigned to a variable.

    > a:={5,4,0,2};a := {0, 2, 4, 5}

    > a[3];4

    > b:=-1,x,-sin(z);b := 1, x, sin(z)

  • 8/2/2019 Maple Book 2010

    29/36

    29

    > b[2];x

    5.4 Conversion from lists to sets

    The conversion from expression sequence to a list or set is simple as follows.

    > a:=1,x,1-z;a := 1, x, 1 z

    > [a];[1, x, 1 z]

    > {a};{1, x, 1 z}

    Conversion in the opposite direction is done using a special case of the selection operator. Ifyou specify no selection range, then all elements are selected.

    > a:=[1,2,3,4];a := [1, 2, 3, 4]

    > b:={x,y,z};b :=

    {x,y,z

    }>a[];

    1, 2, 3, 4

    > b{};x,y,z

    So lists can be converted to sets or sets to lists using expression sequences as a half-wayhouse. An alternative to this is to use the Maple command convert which is of the form

    convert(a,set) which converts the list a into a set.

    convert(b,list) converts the set b into a list.

    The keywords op and nops are useful when using lists and sets. op can pick out specificelements and nops gives the number of elements. With the above definitions of a and b wehave

    > op(2,b);y

  • 8/2/2019 Maple Book 2010

    30/36

    30

    > nops(a);4

    6 The Student Package

    This package enables the use of several integration methods, both analytical and numerical.We shall use it solely for looking at analytical evaluation of integrals. It enables you to workthrough a problem step by step rather than just being given the answer and it may help giveyou a better understanding of the techniques of integration. It is called by using

    > with(student);

    which loads the package.

    6.1 Integration by Substitution

    First set up the integral you require by using Int. Make sure you dont use int otherwisethe answer will be displayed immediately and this will take all the fun out of it!

    > I1:=Int(4*x^3*cos(x^4+2),x);

    I1 :=

    4x3 cos(x4 + 2)dx

    One of the key methods for integration is by substitution. In this case we want to make achange of variable u = x4. This can be done in Maple by using

    > I2:=changevar(u=x^4,I1,u);

    The first argument is the change you want, the second is the integral to which it is to beapplied and the third is the variable you want in the new integral. In this case this shouldproduce

    I2 :=

    cos(u + 2)du

    A second change of variable would be

    > I3:=changevar(v=u+2,I2,v);

    giving

    I3 =

    cos vdv

    At this stage you should know the answer to this integral and so all you need to do is totype

  • 8/2/2019 Maple Book 2010

    31/36

    31

    > value(%);

    which will produce

    sin v

    Finally you will back substitute to produce

    > subs(v=u+2,u=x^4,%);

    to give

    sin(x4 + 2)

    This technique can be applied in just the same way to definite integrals.

    6.2 Integration by Parts

    This is again a part of the Student package and so

    > with(student);

    is the first operation to perform. The integral is set up in the same way as before.

    > I1:=Int(x*sin(x),x);

    and the Maple command to be used is intparts which operates as follows

    > intparts(I1,sin(x));

    The first argument is the integral and the second is the part of the integral which is to bedifferentiated. In the above case this would produce

    1

    2sin(x)x2

    1

    2cos(x)x2dx

    This is clearly more complicated than the original integral. This is because we have chosenthe wrong part to differentiate. The correct method would be to write

    > intparts(I1,x);

    x cos(x) cos(x)dx

    This can be simplified using

    > value(%);x cos(x) + sin(x)

  • 8/2/2019 Maple Book 2010

    32/36

    32

    Some integrals need repeated application of integration by parts. This can be done usingMaple by repeating the command intparts. One integral which will often occur throughout

    your work is of the form ex cos(x)dx

    This needs two integrations by parts, by which time the original integral reappears. If theoriginal integral is defined as I2 then once we have used parts twice the answer can bedetermined by using

    > isolate(I2=expand(%),I2);

    which should give the answer.

    7 Elementary programming in MAPLE

    Internally, Maple consists of a rather small number of commands for the formation of loops,branches, procedures, etc.. These commands make up the programming language of Maple.All other commands (e.g. solve) are written in this programming language. We can usethese commands to write our own programs.

    7.1 Loops

    Loops can be formulated using for or while with do ... od:

    > for x from 1 to 3 do

    > print(x):

    > od;1

    2

    3

    > for x from 5 to 1 by -2 do> print(x):

    > od;5

    3

    1

  • 8/2/2019 Maple Book 2010

    33/36

    33

    > for i in [1,5,19] do

    > print(i):

    > od;1

    5

    19

    > x:=0:

    > while (x < 3 ) d o

    > x:=eval(x)+1:

    > od;x := 1

    x := 2

    x := 3

    Hence a program to sum the first 10 odd integers could look like the following:

    > total:=0;total := 0

    > for i from 1 to 19 by 2 do

    > total:=total+i:

    > od:

    > print(total);100

    As we saw previously, we could also have done this using the sum keyword

    > sum(2*k+1,k0..9);100

    A useful facility is the concatenation operator || which can be used for example to generatethe variable names aO, al, a2 etc. automatically

    > for j from 1 to 4 do

    > a||j:=(2*j+1)*sin(j*Pi*r);> od;

    a1 := 3 sin(r)

  • 8/2/2019 Maple Book 2010

    34/36

    34

    a2 := 5 sin(r)

    a3 := 7 sin(r)

    a4 := 9 sin(r)

    If we subsequently wanted to add these all up, then

    > total2:=0;total2 := 0

    > for j from 1 to 4 do

    > total2:=total2+a||j;> od;

    total2 := 3 sin(r)

    total2 := 3 sin(r) + 5 sin(r)

    total2 := 3 sin(r) + 5 sin(r) + 7 sin(r)

    total2 := 3 sin(r) + 5 sin(r) + 7 sin(r) + 9 sin(r)

    (Of course, we could have added them up in the loop where we generated them.) If wewanted to evaluate total2 at say r = 0.1 then

    > r:= 0.1;

    r := .1

    > evalf(total2);41.45233957

    7.2 Branches

    Branches, case distinctions and conditions can be formulated in Maple using the keywordsif, then, elif , else and fi.

    The syntax is:

    if condition1 then ....

    elif condition2 then ....

    elif condition3 then ....

    ...

    else ....

  • 8/2/2019 Maple Book 2010

    35/36

    35

    fi;

    For example:

    > x:=0:

    > do

    > x:= eval(x) +1:

    > if x3 then break; fi;

    > print (x):

    > od:1

    2

  • 8/2/2019 Maple Book 2010

    36/36

    36

    A FAQ and Common Mistakes

    It takes ages for the MAPLE to calcuate something.

    You are too impatient; wait a bit longer! You have filled up the memory with rubbish (eg by calculating to 106 digits). Restart

    Maple.

    Nothing happens when I press Return at the end of a line Ive typed in or itdoesnt recognise commands Ive typed in.

    You have forgotten to put a ; or a : at the end of a line. Certain quantities that are defined in the worksheet seem not to be defined; check that

    you have executed the line by pressing Return when the cursor is on that line.

    If a plot does not appear; are you trying to plot complex numbers on real axes? You are trying to use a command that has not been loaded, eg you cannot use matrix

    until you have typed with(linalg);.

    It seems to produce the wrong answer in a calculation.

    You have omitted the * in an expression, eg 4(2 + 5) instead of 4 (2 + 5). You have omitted some brackets, for example 2^1/2 is different from 2^(1/2) You have not defined a function correctly (see section 3.1).