Vous êtes sur la page 1sur 36

1

Introduction to Maple

Autumn 2010

Mathematics, University of Exeter

Contents
1 Using MAPLE 1.1 What is MAPLE? . . . . . . . . . . . . . 1.2 What can MAPLE do? . . . . . . . . . . 1.3 How do I access MAPLE from Windows? 1.4 How do I get MAPLE to work? . . . . . 1.5 Inserting comments in Maple . . . . . . 1.6 Execution groups . . . . . . . . . . . . . 1.7 On line help . . . . . . . . . . . . . . . . 1.8 Saving your Work on Disk . . . . . . . . 1.9 Restarting MAPLE . . . . . . . . . . . . 2 Examples of Uses of MAPLE 2.1 Recalling Previous Answers . . . . . 2.2 Integer Arithmetic . . . . . . . . . . 2.3 Floating Point Arithmetic . . . . . . 2.4 Unevaluated Expressions . . . . . . . 2.5 Complex Numbers and Arithmetic . 2.6 Polynomials . . . . . . . . . . . . . . 2.7 Labels . . . . . . . . . . . . . . . . . 2.8 Elementary Functions . . . . . . . . . 2.9 Rational Functions . . . . . . . . . . 2.10 Plotting Graphs . . . . . . . . . . . . 2.11 Solving Equations . . . . . . . . . . . 2.12 Finding Complex Roots of Equations 3 Basic Calculus 3.1 Functions in Maple . . . . . . . . . . 3.2 Denition of a Function . . . . . . . 3.3 Functions of More than One Variable 3.4 Dierentiation of a Function . . . . . 3.5 Composite Functions . . . . . . . . . 4 Further use of Maple 4.1 Dierential Equations . 4.2 Limits . . . . . . . . . 4.3 Taylor Series . . . . . . 4.4 Vectors and Matrices . 4.5 Sums and Products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4 4 4 5 6 6 6 7 7 7 7 8 9 9 9 11 11 13 14 14 15 17 18 19 19 20 20 21 22 22 23 23 24 25

3 5 Expression Sequences, Lists and Sets 5.1 Expression Sequences . . . . . . . . . 5.2 Lists . . . . . . . . . . . . . . . . . . 5.3 Sets . . . . . . . . . . . . . . . . . . 5.4 Conversion from lists to sets . . . . . 26 26 27 27 29 30 30 31 32 32 34 36

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

6 The Student Package 6.1 Integration by Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6.2 Integration by Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Elementary programming in MAPLE 7.1 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Branches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A FAQ and Common Mistakes

P.Ashwin@exeter.ac.uk September 16, 2010

1
1.1

Using MAPLE
What is MAPLE?

It is well known that computers can perform numerical calculations very quickly but over the last few years it has also become possible to use computers to perform algebraic calculations using symbols as well as numbers. MAPLE is one of the most powerful of these systems and it 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 nd that all of the other Mathematics courses will use Maple throughout the year. This course will not only teach you something about Maple but it will also serve as a means of revising some of the Mathematics which you have seen at A-level. NB If you nd any errors or have any comments on this booklet, please email them to me at p.ashwin@ex.ac.uk. 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 oating point arithmetic. It can manipulate formulae involving polynomials, trigonometric functions, logarithms and many so called special functions which you will encounter later in your degree course. It can solve systems of equations. It can dierentiate, integrate and solve dierential 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 nd that a menu appears.

5 (iii) Move the cursor to Programming, Maths and Stats Programmes and you will nd 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 of you 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 here that you use the former; note that you can get a worksheet from File >New >Worksheet mode. 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 start typing. In these notes, when you see > anything; this means that anything is the MAPLE command you type in. The result will be given by MAPLE 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 (;) and then press Return and wait for MAPLE to give the result. The semi-colon tells MAPLE when you have nished typing. To begin with your most common mistake will be to forget the semi-colon. You can spread your expression over as many lines as you wish, ending each one with an Return. Use the arrow keys or mouse to move around within the expression or within the worksheet. MAPLE will do nothing until it sees the semi-colon followed by Return. You can put more than 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 cursor to go back to that statement, edit it and press return again to re-execute it.

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 nd it very helpful to 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 square backets of variable size on the extreme left hand side of the worksheet. These are called execution groups. If you press Return anywhere within an execution group, all commands in the execution group will be executed by MAPLE. You can put more commands in an execution group by typing SHIFT-Return at the end of the line and Return only when you have got to the end of the group. Useful commands include being able to join two execution groups or to split them; go to the EDIT menu at the top and select Split or Join to get several options for splitting/joining groups. If you would like to add an execution group between two that you have already typed in, go to the INSERT menu at the top and then Execution Group. You can add one 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 of two ways. Firstly it is possible to use the pull down menus at the top of the screen where you can obtain information on the whole system. To do this, move the cursor up to the Help menu and hold down the left button on the mouse. Now, while still holding the mouse button 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 specic information on some keyword, say for example the word expand, but are unsure how to use it then you need 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

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 nished your session on MAPLE you may wish to save it in order to use it on another occasion. To do this either on permanent disc space or on your pen drive, see the appendices at the end of this booklet.

1.9

Restarting MAPLE

As you execute MAPLE statements in a worksheet, MAPLE will remember the denitions and results you have generated; if you wish that MAPLE forgets all denitions and starts with its memory empty, type > restart; This will not remove any text that you have typed in, but all statements will not be executed until, for example, you move the cursor to the start of the worksheet and execute each line by typing Return on each line.

Examples of Uses of MAPLE

MAPLE uses + and for addition and subtraction. It uses for multiplication, / for division and 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 with your typing, you do not need to type everything in again. You can click the mouse so that the cursor is where you want to add or delete the text and just alter the text. If you want to copy something similar to a previous expression use the cut and paste facility under the Edit menu.

2.1

Recalling Previous Answers

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

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 the number 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 specic integer then some other useful keywords are ithprime(n) which returns the 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 a number is prime by using isprime(n) which tests to see whether n is prime and returns an answer true or false.

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 oating point evalf(x,n) evaluates x to n signicant gures. The default is that the system works to 10 signicant gures. To change this type >Digits:=18; Now all oating point operations are evaluated to 18 signicant gures. 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 unless you specically 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 complex numbers. You can perform complex operations in MAPLE in just the same way as for real arithmetic. 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

10 The sum, dierence, product and quotient can be obtained by use of the standard mathematical 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 4 arctan( ) 3

> argument(z2);

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); 1 z3 := polar(2, ) 3

convert(z,polar), converts z into its polar form > convert(z1,polar); 1 polar( (2), ) 4

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

11 > z4:=evalc(z3); z4 := 1 + I (3) evalf can be used to produce oating 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 order to perform complex evaluations. Hence > evalc(sin(1+I)); sin(1) cosh(1) + I cos(1) sinh(1) > evalf(%); will evaluate this.

2.6

Polynomials

So far everything we have seen has involved a calculation with numbers. MAPLE is perfectly happy 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 allowed by the % notation in the previous section. In order to do this we use the := symbol to attach a label to an expression.

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 system variable), and whenever this variable is referred to in an expression the value is used. > x:=1; x := 1 With the denition 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 dened as a function of z. This is, however, not a true function (it is sometimes called an apparent function) because the parameter x is xed. That is, as we saw 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

13 or > f:=x^2+3*x; f := x2 + 3x > subs(x=1,f); 4 The obvious way of dening functions, f(x):=, does not work because when Maple sees say f(x):= 3*x, Maple stores in a table the information that f, in the special case when the argument is x, gives the result 3x. If you now ask for f(2) or f(y) then Maple does not know what 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 dening functions in the next sections, but remember for now that > f:=x^2+3*x; f := x2 + 3x denes an expression and NOT a function.

2.8

Elementary Functions

MAPLE recognises all of the standard trigonometric functions. sin(x) will give the value of the sine of x, with x in radians. You can use the other trigonometric functions in a similar way. Inverse trigonometric functions are also available. For example arcsin(x) and arctan(x) will return the angle in radians within the range [ , ]. Similar restrictions will 2 2 apply 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 you will see most of during your rst year. MAPLE uses many of the trigonometric identities when dealing with this type of function.

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); > factor(%); 2 sin(x) cos(x) + 4 cos(x)3 3 cos x cos(x)(2 sin(x) + 4 cos(x)2 3) (x + y) (x y)2

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

2.9

Rational Functions

These can be dened 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); 2 1 1 1 + 3x+1 3x2

2.10

Plotting Graphs

Another very useful aspect of MAPLE is its ability to plot graphs. The keyword for producing a graph is plot. In order to obtain a graph of, say, y = x2 between x = 2 and x = 3 you must type > plot(x^2,x=-2..3); The graph appears drawn on the worksheet. If you click on the graph, a box appears round it. You can nd the coordinates of any point on the graph by moving the cursor to the point and click the left mouse button. With your graph boxed, investigate the functions of the row of 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 on the same graph, use plot: > plot({y,y^2,y^3},y=-1..1);

15 The graphs should appear each in a dierent colour. The range can also be specied 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 buttons on the screen immediately above the workspace. Then try clicking on Style, Colour, Axes and 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 exact solution. If you plot a graph of x and cos(x) on the same graph you will see a root between 0 and 1. The equation can be solved by using the keyword fsolve which is the oating point solve > 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;

16 > solve(eq,x); 1, 1, 1 + > sols:=[solve(eq,x)]; sols := [1, 1, 1 + > sols[3]; 1 + > 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); 3 1 w = 1, u = , v = 2 3 3, 1 3];

3, 1

check solutions: > subs(sols,eqns); 1 = 1, 3 = 3, 0 = 0 Pick 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 ( Z 5 3 Z 4 + 2 Z 2 Z + 3, index = 1), RootOf ( Z 5 3 Z 4 + 2 Z 2 Z + 3, index = 2), RootOf ( Z 5 3 Z 4 + 2 Z 2 Z + 3, index = 3), RootOf ( Z 5 3 Z 4 + 2 Z 2 Z + 3, index = 4), RootOf ( Z 5 3 Z 4 + 2 Z 2 Z + 3, index = 5)

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, x<>0); 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 produce the results in the form of a complex number using the standard Maple notation a + Ib > solve(x^2+x+1=0,x); 1 1 1 1 + I 3, I 3 2 2 2 2

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 then you must use > fsolve(f=0,x,complex); 6.162277660, .1622776602, 1. 2.I, 1. + 2.I

18

Basic Calculus

The keywords di and int are used for dierentiation and integration respectively. > diff(x^2+log(x),x); 2x + > int(x*exp(x),x); xex ex In each case the second argument tells the system which variable you are dierentiating or integrating with respect to. If you need to evaluate a denite integral then use > int(x^2,x=0..1); 1 3 1 x

If you give it an integral which it nds diculty in evaluating, then it will merely display the integral. > int(exp(cos(x)),x=0..1);
0 1

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 4 x 4

Note the following: > Int(x^2,x=1..3);


1 3

x2 dx 26 3

> value(%);

19 but > Int(x^2,x=1..3): > evalf(%); 8.666666667

3.1

Functions in Maple

We have to be careful when talking about a function in Maple. When we have used a statement like > f:=x^2+2*x+3; f := x2 + 2x + 3 this is known in Maple as an expression, not a function.

3.2

Denition of a Function

The way in which a function is set up is by dening an operator. The symbol -> which is made up of a minus and a greater than sign can be read as maps to. It is used as follows to dene the function x2 . > f:=x->x^2; f := x > x2 This is read as f becomes x maps to x squared. The dummy parameter x used in the denition could be anything and you will see that if we write > g:=t->t^3; g := t > t3 > g(2); 8 Even redening the parameter which was used in the denition does not aect the denition of the function. > t:=3; t := 3 > g(4); 64

20 An alternative way of dening 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 dene functions of more than one variable. For example to dene f (x, y) = x2 y 2 the Maple command is > f:=(x,y)->x^2-y^2; > f(u,v); u2 v 2 f := (x, y) > x2 y 2

3.4

Dierentiation of a Function

We have seen already that we use the command di(y,x) to dierentiate the expression y with respect to x. In order to dierentiate a function we use the operator D > f:=x->x^2; f := x > x2 > g:=D(f); g := x > 2x However, if we use > D(f)(x); 2x this produces an expression, since it is evaluated at x.

21 If we dene > f:=(x,y)->x^4-y^4; (x, y) > x4 y 4 then D[1](f) produces the function based on the derivative of f with respect to the rst variable, namely x. D[1,2](f) produces the function based on the derivative with respect to x followed by y. The $ symbol can be used as in dierentiation of an expression to signify dierentiation several times with respect to that variable. Hence > D[1$4,2$3](f); will produce a function based on dierentiating the function f, 4 times with repect to x followed by 3 times with respect to y. > D[2,2](f); (x, y) > 12y 2 > D[2$4](f); (x, y) > 24

3.5

Composite Functions

If we have two functions dened as f (x) and g(x) then the composition f (g(x)) is written in 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 expand it by using

22 > expand(%); sin(sin(sin(z))) The dierentiation operator for functions, D, can also be repeated in the same way. If f is dened 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 denition of f this gives 12x2

4
4.1

Further use of Maple


Dierential Equations

In MAPLE y(x) means a function y which depends on x. When solving a dierential equation you will be trying to determine what function satises a specic dierential equation. If you need to solve dy = xy dx then this would be written as >de1:=diff(y(x),x)=x*y(x); de1 := y(x) = xy(x) x

In order to solve it you use the keyword dsolve > dsolve(de1,y(x)); y(x) = e 2 x C1 where C1 is an arbitrary constant.
1 2

23

4.2

Limits

Another very useful tool in Maple is the limit command. Again you will spend much more time on this topic in other courses. The command gives you the limiting value of an expression as the variable tends to a specic value. This is usually used when an expression is of the 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 innity 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); undened > 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 of Taylor series in more detail in your other courses. However you may have seen some simple examples of Taylor series already. The most well used are those for ex , sin x and cos x. The

24 Taylor series is a power series and it will always be displayed in Maple up to a certain order which 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 1 1 5 1 x + O(x6 ) 1 + x + x2 + x3 + x4 + 2 6 24 120 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 Maclaurin series 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 nd that 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 polynomial by 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 and this must be loaded before you can use it. The command is very simple. All you have to do is to type in > with(linalg); The most important keywords in this section are vector and matrix which set up exactly what 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 see the entries in v on the screen you must type > print(v); or > evalm(v);

25 If a and b are dened 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 dened. In this case you need to specify 3 facts. These are 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 rst row and 0 3 1 in the second row. Calculations involving matrices can be performed using evalm which means 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 example det can be used to nd the determinant of a matrix. Other operations include the elementary row and column operations and reduction to row echelon form rref(). More details are given in the Vector and Matrices module.

4.5

Sums and Products

Maple is able to calculate nite sums and products and is sometimes able to work out innite ones. 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 nd a closed form for a sum then it is returned unevaluated and just as with integrals you can use evalf to obtain a numerical answer. The general form for products is very similar. It is

26 > product(expr,i=a..b); An example of this is >product(n,n=1..5); 120

Expression Sequences, Lists and Sets

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

5.1

Expression Sequences

The simplest of these is the expression sequence. This is entered simply by writing each of the 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

27 The null sequence is generated by the keyword NULL and nothing is shown on the screen as 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 dierence 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 numerical list 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}

28 Sets are more complicated than lists in that there are some automatic simplication rules. Sets are not ordered and they may not contain duplicates. > {1,2,3,2,1}; > {0,2,1}; {1, 2, 3} {0, 1, 2}

There are three operations dened on sets, namely union, intersect and minus which work in the same way as the mathematical denitions. > 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 is a variable containing a list or set or expression sequence then A[1] returns its rst operand, A[2] its second and so on. Extraction is only possible when the list or set or sequence has been 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)

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. If you 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-way house. 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 specic elements and nops gives the number of elements. With the above denitions of a and b we have > op(2,b); y

30 > nops(a); 4

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 work through a problem step by step rather than just being given the answer and it may help give you 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 otherwise the 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 a change of variable u = x4 . This can be done in Maple by using > I2:=changevar(u=x^4,I1,u); The rst argument is the change you want, the second is the integral to which it is to be applied and the third is the variable you want in the new integral. In this case this should produce 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 to type

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 denite integrals.

6.2

Integration by Parts

This is again a part of the Student package and so > with(student); is the rst 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 rst argument is the integral and the second is the part of the integral which is to be dierentiated. In the above case this would produce 1 sin(x)x2 2 1 cos(x)x2 dx 2

This is clearly more complicated than the original integral. This is because we have chosen the wrong part to dierentiate. The correct method would be to write > intparts(I1,x); x cos(x) This can be simplied using > value(%); x cos(x) + sin(x) cos(x)dx

32 Some integrals need repeated application of integration by parts. This can be done using Maple 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 the original integral is dened as I2 then once we have used parts twice the answer can be determined by using > isolate(I2=expand(%),I2); which should give the answer.

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 use these 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): 1 2 3 > for x from 5 to 1 by -2 do > print(x): 5 3 1 > od; > od;

33 > for i in [1,5,19] do > print(i): 1 5 19 > x:=0: > while (x < 3 ) do > x:=eval(x)+1: x := 1 x := 2 x := 3 Hence a program to sum the rst 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 generate the variable names aO, al, a2 etc. automatically > for j from 1 to 4 do > a||j:=(2*j+1)*sin(j*Pi*r); a1 := 3 sin(r) > od; > od; > od;

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; 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 we wanted to evaluate total2 at say r = 0.1 then > r:= 0.1; r := .1 > evalf(total2); 41.45233957 > od;

7.2

Branches

Branches, case distinctions and conditions can be formulated in Maple using the keywords if, then, elif , else and . The syntax is: if condition1 then .... elif condition2 then .... elif condition3 then .... ... else ....

35 ; For example: > x:=0: > do > > > x:= eval(x) +1: if x<>3 then break; fi; print (x): 1 2

> od:

36

FAQ and Common Mistakes

It takes ages for the MAPLE to calcuate something. You are too impatient; wait a bit longer! You have lled 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 it doesnt recognise commands Ive typed in. You have forgotten to put a ; or a : at the end of a line. Certain quantities that are dened in the worksheet seem not to be dened; 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 dierent from 2^(1/2) You have not dened a function correctly (see section 3.1).

Vous aimerez peut-être aussi