This action might not be possible to undo. Are you sure you want to continue?

All you’ll ever need to know about Maple !

J J O’Connor A P Naughton MT4111/5611 2009

You can find electronic versions of each of the lectures in the Maple folder on the L-drive on each of the computers in the microlab. You will not be able to change them there, but you can copy them into your own filespace and then you can play with them and modify the code to see what happens. If you want to remove all the output you can use the Remove output command on the Edit menu. In due course this folder will also contain copies of the practice exercises given at the end of each lecture and (eventually) solutions to the tutorial sheets. Some pages of summaries are at the end of this booklet.

Contents

Using Maple as a calculator Help Polynomial expressions Trigonometric expressions Assigning Substituting Differentiating Defining functions Formatting worksheets Plotting Plotting in 3 dimensions Integration Solving equations Looping If clauses Lists Sets Summing Procedures More procedures Reversing Pythagorean triples Some more equations Interpolation More plotting 2 4 5 6 7 9 11 12 13 14 18 22 26 32 34 35 37 38 40 45 46 49 49 51 52 Recursion More recursion Looking for prime numbers Testing for primes Linear algebra package The Power method Solving differential equations DEtools package Other methods Random numbers Shuffling Sudoku Countdown Sums of two squares Number of different ways Continued fractions Geometry package: Menelaus Ceva's theorem Product of chords Ptolemy's theorem Pascal's theorem The Euler line The Nine-point circle Summaries Mathematicians' pictures 55 57 58 62 63 69 73 75 78 79 82 84 86 90 91 93 97 99 100 102 103 104 107 109 118

**Using Maple as a calculator
**

As a calculator, Maple works like any electronic calculator, except that you have of put ; after each calculation and press the Return key. To move the cursor down to the next line without doing the calculation, use Shift-Return. You can press the key when the cursor is anywhere in the red bit of the "group" and Maple will calculate for you. You can go back and calculate with an earlier bit of code by clicking on the red bit and pressing Return. O 22/7-355/113; sqrt(45);

1 791 3 5

To get a decimal answer, use the evalf (= evaluate as a floating point number ) function The % stands for the last result Maple calculated (even if this was not the last thing on the screen -remember you can go back and recalculate earlier results). O evalf(%); 6.708203931 %% stands for the last but one result. This time you can get the answer to 20 significant figures O evalf(%%,20); 6.7082039324993690892

Using a decimal point in your input tells Maple that you want the answer as a decimal. O sqrt(2.0); 1.414213562

Maple knows about !, which it calls Pi (the capital letter is important) and will give it to very great accuracy. Over the centuries mathematicians spent a lot of time calculating many digits of !. The methods developed included a series for arctan discovered by James Gregory,the first Regius Professor of mathematics at St Andrews. The English mathematician William Shanks published 707 places of ! in 1873 and it was not discovered until 1943 that the last 179 of these were wrong. The expansion of ! is now known to many billions of places. O evalf(Pi,1000); 3.1415926535897932384626433832795028841971693993751058209749445923078164062\ 86208998628034825342117067982148086513282306647093844609550582231725359\ 40812848111745028410270193852110555964462294895493038196442881097566593\ 34461284756482337867831652712019091456485669234603486104543266482133936\ 07260249141273724587006606315588174881520920962829254091715364367892590\

2

001264489 2. put ( ) around what you evaluate. It uses lower case letters for them. Maple will calculate the difference between these two approximations and !.36001133053054882046652138414695194151160943305727036575959195309218611\ 73819326117931051185480744623799627495673518857527248912279381830119491\ 29833673362440656643086021394946395224737190702179860943702770539217176\ 29317675238467481846766940513200056812714526356082778577134275778960917\ 36371787214684409012249534301465495853710507922796892589235420199561121\ 29021960864034418159813629774771309960518707211349999998372978049951059\ 73173281609631859502445945534690830264252230825334468503526193118817101\ 00031378387528865875332083814206171776691473035982534904287554687311595\ 62863882353787593751957781857780532171226806613001927876611195909216420\ 199 22/7 is a well-known approximation for !. If you want the answer as a decimal. The pallettes on the left of the screen (if you want to bother with them) will remind you of some of the functions. but less well-known approximation is 355/113. cos. sqrt(2). Maple works in radians. log = ln. evalf(%. O evalf(22/7).142857143 3. To use the functions. evalf(355/113). 2^(1/2). sin(Pi/2). you will have to ask for it. 3. This was discovered by the Chinese mathematician Ch'ung Chi Tsu in about 500AD. sin 3 1 3 . This was known to the Greek mathematician Archimedes about 250BC (and indeed earlier). sin.50). sin(60* Pi/180).66 10-7 Maple knows about all the functions you have on your calculator: sqrt. not degrees. evalf(22/7-Pi). etc as well as exp.141592920 0. evalf(355/113-Pi). the sine of 60° evalf(%). log[10] or log10 and lots more besides. O sin(3). A better.

you will find some examples of how to use the command. 120 93326215443944152681699238856266700490715968264381621468592963895217599993\ 22991560894146397615651828625369792082722375825118521091686400000000000\ 0000000000000 2 97 48 2 24 16 9 7 5 5 4 3 3 2 2 2 3 5 7 11 13 17 73 19 79 23 83 29 89 31 37 41 43 47 53 59 61 67 71 97 You can even apply ifactor to a fraction: O ifactor(123456/234567). type the command and highlight it. ifactor(100!). Alternatively. 3 2 3803 3607 The function factorial will calculate the product 1 ! 2 ! 3 ! . type ? and then the command. Then go to the Help menu and you will see an entry for the command. O ?print You can also use help(command). factorial(100). ! n usually written n! Maple recognises the ! notation too. (This is the 4 .8660254040 2 2 1. ifactor (for integer factorise) will write an integer as a product of prime numbers. For example... 2 3 643 67 389 6 Help To see the help files on a Maple command. O factorial(5). O ifactor(123456789).4142135623730950488016887242096980785696718753769 Maple knows about some other functions your calculator (probably) can't handle.1 2 3 0. (and you do need the semi-colon!) O help(sin). (You don't even need a semi-colon!) . At the bottom of a help file.

x2 K y2 xKy xCy Maple will factorise expressions as well -. Then you can change them to do what you want. O (x+y)^5. Use the function simplify. xCy 5 x5 C 5 x4 y C 10 x3 y2 C 10 x2 y3 C 5 x y4 C y5 O expand((sqrt(2*x)+sqrt(x))^6). Use the expand function to "multiply out". 99 x3 C 70 2 x3 Maple will (sometimes) succeed in manipulating an expression to make it "simpler". 5 . The Help menu also has a "Full text search" facility which will point you in the direction of any help files where the word or phrase you enter is mentioned. O (x^2-y^2)/(x-y). Each help file has a list of links to related topics at the bottom which may let you hunt down exactly what you want. expand(%).most useful bit!) You can copy and paste these lines into your worksheet and look at what happens. This tends to produce too much output to be very useful! Polynomial expressions One of the most important things Maple can do is to calculate with expressions as well as numbers. factor((x-y)^3*(x+y)^5+1). simplify(%). This last is too difficult! xKy 3 5 xCy x8 C 2 x7 y K 2 x6 y2 K 6 x5 y3 C 6 x3 y5 C 2 x2 y6 K 2 x y7 K y8 xKy 3 xCy 5 x8 C 2 x7 y K 2 x6 y2 K 6 x5 y3 C 6 x3 y5 C 2 x2 y6 K 2 x y7 K y8 C 1 Maple will also handle ratios of polynomials in this way.if it can ! Use the factor function. O (x-y)^3*(x+y)^5. expand(%). factor(%).

These polynomials were first investigated by the Russian mathematician Pafnuty Chebyshev (1821 6 . expand(%).) O (x-2*y)^4+(3*x+y)^3. K 8 y C 27 16 y4 C y3 Trigonometric expressions Maple will handle many trigonometric identities using the expand function. collect(%. (The sort function works in a similar way. collect(%. 2 x2 2 y2 C 3 x3 K y3 x K y3 2 x2 C y2 x3 K y3 2 x2 C y2 xKy x2 C x y C y2 Maple can simplify polynomials in some other ways. coeff(%%.x^3).y). simplify(%). you can ask it to collect together the terms in (say) xn using the collect function. factor(%).O expand(((x-y)^2+(x+y)^2)/(x^3-y^3)). factor(%). sin x C y sin x cos y C cos x sin y sin x cos y C cos x sin y You can use Maple to expand cos( n x) for different values of the integer n and get polynomials in cos(x).x. xK 2 y 4C 3 xC y 3 x4 K 8 x3 y C 24 x2 y2 K 32 x y3 C 16 y4 C 27 x3 C 27 x2 y C 9 x y2 C y3 16 y4 C K 32 x C 1 y3 C 24 x2 C 9 x y2 C K 8 x3 C 27 x2 y C x4 C 27 x3 x4 C K 8 y C 27 x3 C 24 y2 C 27 y x2 C K 32 y3 C 9 y2 x C 16 y4 C y3 You can find the coefficient of a given power of (say) x O coeff(%. expand(%).x). It won't factor back again though ! O sin(x+y). In particular.0).

This process is called assignment. expand(%).. They are very important in Numerical Analysis. expand(%).to 1894). q:=75. 0 2 2 Assigning Maple will store things (numbers. functions. cos 5 x 16 cos x 5 K 20 cos x 3 C 5 cos x cos 12 x 2048 cos x 12 K 6144 cos x 10 C 6912 cos x 8 K 3584 cos x 6 C 840 cos x 4 K 72 cos x 2 C 1 Maple will (sometimes) simplify trigonometric expressions. cos(12*x). . expressions. p/q.) in "containers" or "variables". simplify(%). Think of these as labelled boxes. O p:=15. simplify(1/(1+tan(x)^2)). O sin(x)^2+cos(x)^2.. p := 15 q := 75 1 5 One can also store expressions in these boxes. One can then apply any Maple function to the contents of the box. cos x 1 1 C tan x You may have to help it a bit: O simplify(cos(x)^2-1/(1+tan(x)^2)). 7 . O simplify(1-sin(x)^2). O cos(5*x). sin x 2 C cos x 1 2 Though sometimes the answer isn't what you might expect.

z). if we had already assigned numbers before we put them into the box. z. a := 5 8 However. O x:=5. however. c. z. O c:=a+b. x := 1 y := 3 z := 4 4 and this time altering one of the numbers will not alter anything else. using: O a:='a'. a.b:=3. O a:=5. Maple remembers that c contains a+b and will change c too. c := a C b a := 1 b := 3 4 If we now change either a or b. x := 5 4 To "empty" one of our boxes or variables we unassign it.O quad:=(x+2*y+3*z)^2. c. expand(quad). collect(quad^2. 8 .y:=3. Maple will just put in the number ! O x:=1. quad := x C 2 y C 3 z 2 x2 C 4 x y C 6 x z C 4 y2 C 12 y z C 9 z2 81 z4 C 108 x C 216 y z3 C 18 x C 2 y 2 C 6 x C 12 y C 12 y z C x C 2 y 4 2 z2 C 2 x C 2 y 2 6x One has to be a bit careful. a:=1. z:=x+y.

O c. O f:=x^2+1.5 13. x:=2.5.f).5.b. x:=1.25 x := 2. use restart. a.5 3.25 Substituting Evaluating an expression f in x can be done using the subs function. f:=x^2+1. x. O a.f. subs(x=3.f.b. O restart.5.25 x := 3.5 7. subs(x=1.f).25 9 .f. restart. This does not assign anything to x. a 3 aC3 a b c One can use this process to evaluate an expression. a := a a aC3 To unassign all the variables. f := x2 C 1 x := 1.f).5. x:=3.c.5. f := x2 C 1 3.5. subs(x=2.c.

collect(t. t:=subs(x=y+k.cub). cub := a x3 C b x2 C c x C d t := a y C k 3 C b y C k 2 C c y C k C d a y3 C 3 a k C b y2 C 3 a k2 C c C 2 b k y C a k3 C d C c k C b k2 Now we replace k by b/3a to remove the y2 term. x := 1 2 You can also use the subs function to substitute one expression for another. You have to tell it what to differentiate with respect to. 10 . that if x already has a value assigned to it. subs(x=y+5. y C 5 5 sin y C 5 yC5 2 C y2 C y K 5 2 3 y2 C 50 We can illustrate this with the process of simplifying a general cubic equation O cub:=a*x^3+b*x^2+c*x+d. This process is actually Partial Differentiation .%). It will do several substitutions at the same time.25 x Note. 1 b2 2 b3 1 cb a y3 C K Cc yC CdK 2 3 a 27 a 3 a Differentiating Maple will differentiate expressions. however.y).z=y-5. subs(x=y+5.25 13. It is analagous to the process of completing the square for quadratic equations and is the first stage in reducing a cubic equation to a form in which it can be solved O subs(k=-b/(3*a).x^5*sin(x)). you won't get what you want ! O x:=1.x^2+y^2+z^2). subs(x=3. O restart.f).5. simplify(%). This substitution is known as a Tschirnhaus transformation after the 17th Century German mathematician who first used it. Anything else will be treated as if it were a constant.7.

x)=diff(x^9.) O Diff(x^4.x).x).x).x). which is not valid for its 2nd argument O x:='x'. x := 1 Error. (It knows when the differention is partial. O diff((1+x^2)^3. 360 x2 C 72 The French mathematician Adrian-Marie Legendre (1752 – 1833) defined some important polynomials in connection with solving the problem of how the gravitational effects of the moon 11 . diff(a*x^2+5.x$4). diff(x^4. Diff(x^9.x). you can use x$4.x. Unassign the variable or use restart to be safe ! O x:=1.a). 6 x C 6 y2 12 x y As a short cut. Diff(x^4*y^4. if you want to differentiate (say) 4 times wrt the same variable.y).x). 3 x2 2ax x2 If you try to differentiate with repect to something which has had a value assigned to it. O diff(x^3+y^3+3*x^2*y^2. diff(x^3+y^3+3*x^2*y^2. x := x 4 x3 Maple uses the function Diff (with a capital letter) to write out the formula for the derivative. Maple will complain. but without actually doing the differentiation.x. d dx v vx d dx x4 x4 y4 x9 = 9 x8 Maple will differentiate more than once -.with respect to the same variable or different variables. diff(a*x^2+5.x). diff(x^4. invalid input: diff received 1.O diff(x^3.x).

diff((x^2-1)^n. In the past. the function itself is not an expression in x. n := 7 x7 C 21 2 x2 K 1 x5 C 105 8 x2 K 1 2 3 x C 35 16 x2 K 1 3 x 429 7 693 5 315 3 35 x K x C x K x 16 16 16 16 Defining functions One can store functions in Maple's "boxes" as well as numbers or expressions.2).744 y3 O g:=y->sin(y). but Maple can calculate them very easily.x$n)/(n!*2^n). Note that although we may use x in the definition of a function. O f(g(x)). mathematicians had to look up the coefficients in tables. The pair of variables must be in brackets with a comma between them. f(0. O f:=x->x^3. g(f(x)).and sun affected the tides. sin x sin x3 We may use the same method as above to define functions of two (or more) variables.4).y)->x^2+y^2. f := x/x3 2. O f:=(x. O n:=7. f(y). f(1.0). collect(%. 3 f := x. It usually matters which order we do this in. g := y/sin y Once we have defined two such functions we can then compose them by applying one to the other. f(1.x). Here x is what is called a "dummy variable". y /x2 C y2 0 12 . A function is a "rule" for assigning a value to a number.

symbol.5 Maple differentiates expressions. or the keyboard shortcut Control-T or click on the T on the Tool bar.x). 3 x2 You can however. When you have made such a section you can type a heading next to its symbol to label it. diff(f. If you have defined a function f and want to differentiate it with respect to x. this assigns the value y2 to the variable x. Error. 13 . attempting to assign to `D` which is protected Formatting worksheets To put in comments (like this paragraph!) when the cursor is at a Maple prompt > either use the Insert text item from the Insert menu. f := x/x3 0 O diff(f(x). The Edit menu has a Join command which lets you put the comment in the same group as the command. use the operator D which acts on a function to produce a function . O D(f). You can also put comments on the same line as Maple input. O D:=5. You get the y2 by inserting "nonexecutable maths text" from the Insert menu or using the short cut Control-R.x). if you wish. O x:=y^2. You can use the same method to get a new execution group anywhere in your worksheet and then. not functions. When you have finished. you can use this to insert some explanatory text. To get rid of such a section select Outdent from the Format menu or the symbol from the Tool bar. by using one of the keyboard shortcuts Control-K or Control-J or clicking on the [> on the Tool bar. x/3 x2 Note that this means you can't use D as the name of a variable. start a new Execution group (what Maple calls the group enclosed by the bracket at the left) by using the item Execution group in the Insert menu. then you will have to turn it into an expression by evaluating it at x by using f(x). Do this by selecting what you want to put into it and then selecting Indent from the Format menu or the symbol from the Tool bar. You can make a "collapsible section" which you can 'expand' by clicking on the + symbol or 'contract ' by clicking on the . O f:=x->x^3.

O plot(x*(x^2-1).2). (in plot) invalid arguments You need to put in some 'single quotes' O plot('x*(x^2-1)*(x+2)'.though you can specify the colours if you want)..plot(x*(x^2-1). 6 4 2 K 2 K 1 0 K 2 K 4 K 6 You can't plot using a variable that has been assigned to without a bit of trouble. 1.-2. ..x). 14 .0 to 6) to get a better idea .. 2 1 K 3 K 2 K 1 0 K 1 K 2 1 x 2 3 Maple will plot several functions on the same axes. x := 1 1 x 2 Error. If you don't specify a range Maple will take -10 . O x:=1. Maple will use different colours for the output (some of which do not print very well -. 10.3. Put the expressions to plot into a list (with [ ] brackets round them) or a set (with { } brackets around them).x=-2.'x'=-3..Plotting Maple will plot the graph of a function y = an expresssion involving x on a given interval which you specify (as as a range) by (say) x = 0 . You can click on the picture to see the coordinates of the cursor. Enlarge the picture (using the View menu or Control.2).

x).5 y 1 0. plot([f.x$2)]. 2 1.x^3.2.diff(f.diff(f. blue])..x^2..5 1 x 1.y=0. O f:=x->sin(x)/x. plot(f(x).5 2 Maple will plot expressions.x^4. If you don't want this.1).2. O plot([x. f := x/ sin x x 15 .5 0 0 0. f := x3 K x 10 5 K 2 K 1 0 K 5 K 10 1 x 2 Maple will choose the vertical axis so that all the graphs fit in.colour=black).. you can specify the vertical range.colour=[black.x^5].x=-1.O restart. Leave out the " y=" if you don't want the axis labelled.x=-2.2. If you have defined a function f you can turn it into an expression by f (x). f:=x^3-x.x=0..red.

.5 K 1 K 0.20).but then you musn't mention x at all.86 K 1 K 0.5 1 If you want.90 0.0 0.2 0 K 0. 1 0.6 0. 0 . O plot(f.2*Pi]).. (in plot) invalid plotting of procedures.96 0.4 0..20).8 0.1.0.5 0 x 0. 1. named after the French mathematician Jules Lissajous(1822 to 16 . Error.5 0 K 0.x=0.t=0. 20) O plot(f.5 1 K 1 You can plot Lissajous figures..2 5 10 15 20 You can also plot curves given parametrically: O plot([cos(t).5 0. perhaps you mean plot(f.sin(t).00 0. you can plot the function directly -.

O plot3d(x^2-y^2.3 K 0. 0. You can find out about this using the help facilities on plot which you can get by typing in ?plot.10].t=1..5.5 0. 17 .1 0 K 0. 1 0.t..5 Plotting in 3 dimensions Plotting in three dimensions works similarly. or choose to plot them with dots.5 1 0.coords=polar).y=-5.2*Pi]). crosses.5 0 K 0.sin(5*t)..6 0. You can then click on the picture to move it around.3 0. .t=0.4 0..2 0.4 0.1880): O plot([cos(3*t).2 One can also specify the colours of the various graphs.8 0..2 K 0.5 K 1 You can even use polar coordinates: O plot([1/t.1 0.5)..x=-5. K 1 K 0.

5.view=0..1.1).25).y=-5.1.y=-1.. See ?plot3d or ?plot3d[options].5.x=-1. For example.x=-5. you can specify the vertical range.There are lots of options you can specify for plot3d.. 18 .. One can plot more than one function (using { } this time).x+y+1}. cut a cone with a plane to get a parabola: O plot3d({sqrt(x^2+y^2)...view=0. For example. O plot3d(x^2+y^2.

x+2*y+1/2}.or a double cone with a plane to get a hyperbola: O plot3d({sqrt(x^2+y^2).. .1.x=-1.1.view=-1.. 19 .y=-1.-sqrt(x^2+y^2).1).

One can also plot parametrically.. (This is where you use [ ] brackets. z coordinates. a torus 20 .p=0.. by specifying formulae for the x.view=-2.2*Pi.(2+cos(q))*sin(p).sin(q)].2*Pi. q=0. y..2).) O plot3d([(2+cos(q))*cos(p).

5^x*sin(y).. .Pi.2*Pi.y=0.view=-1.1).sin(p)*sin(q).p=0. a sphere Three-dimensional Lissajous figures: O plot3d([sin(2*p). view=-1.q=0.1. There are lots of other things you can explore with these functions! O plot3d(1.2*Pi.p=0..thickness=3).numpoints=5000.O plot3d([sin(p)*cos(q).2*Pi..x=-1.Pi..cos(p)]..q=0.cos(5*p)]..sin(3*p)..coords=spherical).1. 21 .

x). 1 6 x 6 x5 O f:=int(sqrt(1+sqrt(x)). O int(x^5. 8 4 K !K 15 15 f := K ! 1C ! x 3/2 3 x K2 22 .x). Archimedes' efforts to measure the area of a circle ( and hence calculate a value for !) in about 250BC are equivalent to trying to integrate a function. simplify(f). simplify(g). you should get back to where you started. g:=diff(f. If you differentiate the integral. but quite "easy" functions may be difficult even for Maple. Maple will calculate indefinite integrals when it can.x).Integration The process of integrating is much older than differentiating and goes back to the Ancient Greeks. diff(%. For example.x).

O int(cos(1+x^3). .x 6 2 5 1 3 .x).x 6 2 11 1 3 . f := cos cos 1 C x2 1 C x2 dx Sometimes it can do the integral but it isn't much help.8 4 C 15 15 1 K 5 ! 1C x x xC 3 x 4 5 1C x xK 2 K 5 8 15 ! 1C 1C x x x 3/2 1C x K2 ! 1C x g := K Sometimes Maple can't do it.x 6 2 3 x7 22/3 cos x3 x3 K sin x3 K ! 1 sin 1 6 3 4 9 4 ! 21/3 x3 LommelS1 17/6 K 3 x 22/3 sin x3 4 ! K 9 x 22/3 cos x3 x3 K sin x3 4 ! x7 22/3 sin x3 LommelS1 ! x3 11/6 K 5 3 3 . . that integration should involve a "constant of integration" and so if you integrate a derivative. however.x 6 2 x7 22/3 cos x3 x3 K sin x3 ! x3 LommelS1 17/6 C Remember. 23 . the answer may look different. But it still knows how to get back when it differentiates. 1 cos 1 6 9 7 ! 2 1/3 9 2 22/3 2 6 2 x C 7 3 ! x2 sin x3 C 3 22/3 cos x3 x3 K sin x3 ! x2 x7 22/3 sin x3 LommelS1 ! x3 11/6 K 11 3 3 .x). diff(f. O f:=int(cos(sqrt(1+x^2)). . .x).

x).x). simplify(g). g:=int(f. simplify(h). there is no shorthand for multiple integration as there is for multiple differentiation. f:=sin(x)^3/(1+cos(x)^3).O f:=(1+x^2)^2. O f:=x^2-sin(2*x). 24 .x).x). h:=int(g. simplify(h-f).x). 1 1 f := tan x 2 K ln 1 C tan x 2 2 g := tan x 1 C tan x tan x f := g := 1 ln 1 K cos x C cos x 2 h := 2 3 3 3 2 2 K tan x sin x 1 3 1 C cos x K 3 arctan 1 3 K 1 C 2 cos x sin x K 1 C 2 cos x 2 3 1 sin x K 2 cos x sin x 2 C 2 2 3 1 1 K cos x C cos x 1C 3 K sin x K 1 C cos x 2 1 K cos x C cos x 0 Although one can repeatedly integrate a function. h:=diff(g.x). f := 1 C x2 2 g := 4 1 C x2 x h := x4 C 2 x2 1 C x2 2 K x4 K 2 x2 1 Maple will calculate integrals of trigonometric functions which would be very tedious to tackle "by hand". f-h.x). g:=diff(f. simplify(f-h). In every case. O f:=int(tan(x)^3. differentiation should bring you back to where you started but it might be a bit of a struggle. g:=diff(f. int(%.

x=-1. int(f. ..O int(%..x. you can ask it 25 . 1 ! 2 Maple will (sometimes) handle integrals over infinite ranges as well as integrals over ranges where the function goes off to infinity.x=0. This only integrates once. int(sin(x)^2.x=0.x=0. 1 N N undefined Even if it can't work out exactly what the answer is.x).Pi). int(tan(x).Pi/2). O int(exp(-x).x=0.infinity).. int(%. int(exp(x).x. int(tan(x).. int(%.infinity). Note the way you specify the range of integration: x = 0 .x).1).. It will give an exact answer if it can.x=0.x). 1 etc O int(x^4. f := x2 K sin 2 x 1 3 1 x C cos 2 x 3 2 1 4 1 x C sin 2 x 12 4 1 5 1 x K cos 2 x 60 8 1 1 x6 K sin 2 x 360 16 1 3 1 x C cos 2 x 3 2 Maple will also do definite integrals.x.x=0.. 1 5 1 ! 2 The calculation mentioned above that Archimedes used to calculate " (t he quadrature of the circle ) is equivalent to the integral: O int(sqrt(1-x^2).x)..1).Pi).

4074043129 As in the differentiation case. Int(cos(x)^5.x).x=0.x)=int(cos(x)^5. O Int(cos(x)^2..Pi).1). evalf(%). it will assume you mean expression = 0.x). solve(7*x-22).. You have to give it the equation and tell it what to solve for.x=0. If there is only one variable in the equation. Maple will write out the formula for the derivative if you ask for (with a capital letter). If you give it an expression instead of an equation. 1 k := 0 cos 1 C x2 dx 0.570796327 cos x 5 dx = 1 cos x 5 4 sin x C 4 cos x 15 2 sin x C 8 sin x 15 Solving equations Maple will try and solve equations. cos x 2 dx ! Int cos x 0 2 dx 1. solve(all_my_problems).x). Int(cos(x)^2. solve(x^3-2*x^2-5*x+1=0. O solve(7*x=22.4).x). O solve(a*x^2+b*x+c=0. 22 7 22 7 22 7 0 Maple will solve equations which have more than one solution.to do a numerical integration by using the evalf function. evalf(%. solve(7*x=22).x). it will solve for that without being told. evalf(k). O k:=int(cos(sqrt(1+x^2)). 26 .

0007660 I. O solve({y=x^2-4.575773473. label = _L4 y=K 3. 0.4).0009660 I A short cut if you only want to see the decimal expansion is to use the function fsolve.x=3. x = 0. evalf(%).390 K 0.K 1 6 316 C 12 I 2355 1/3 K 19 3 316 C 12 I 2355 1/3 C 2 1 K I 3 2 3 316 C 12 I 2355 K 38 3 316 C 12 I 2355 1/3 3. 0.388489748 If you want to find solutions in a particular range you may have to specify it O fsolve(sin(x).576 K 0..141592654 Maple will solve simultaneous equations. label = _L2 y=K 2 RootOf 2 _Z K 2 C _Z2.1 bK K 2 1 6 316 C 12 I 2355 1/3 1/3 b2 K 4 a c 1 bC .0003 I. fsolve will only give the real roots of the equation.464101615. You have to enter the equations as a "set" (with { } round them and commas between).x). y=K 2 RootOf 2 _Z K 2 C _Z2. x = RootOf 2 _Z K 2 C _Z2. label = _L2 K 2.7320508076 and of course. If you want to solve for several variables.1871 C 0. 3. solve({y=x^2-4.y=-2*x-2}). K 1. label = _L4 K 2.K 3 12 3 316 1 6 C 19 C 12 I 2355 K C 3 316 C 12 I C 12 I 2355 1/3 2355 2 1 C I 3 2 1 12 316 1/3 K 38 3 316 C 12 I 2355 1/3 . There are ways of getting complex roots out of it. x = RootOf 2 _Z K 2 C _Z2. Usually. 3. O fsolve(x^3-2*x^2-5*x+1=0.1872837251.{x. K 1. you can use fsolve here too.y=-2*x-2}. 27 . If you leave out the variables you want to solve for. If you want to know what they are then you can consult the Help facilities for fsolve. you have to enter these as a set too.y}). Maple will assume you want all of them.K a 2 38 3 316 C 12 I 2355 1/3 1/3 b2 K 4 a c a C 2 1 .

28 .x=-3. sol:=solve(f=0. Then you see that we've missed one of the solutions O plot({x^2-4.{x=-3..-2*x-2}.y=-2*x-2}.0.732050808 If there is more than one solution you can pick out the one you want using [1] or [2] or .381966012 and then you can draw the graph to see where it is O plot(x^2+3*x+1. y = 3. y=K 3.464101615.7320508076 You can then use the plot facility to see the intesection of the two curves.O fsolve({y=x^2-4..y=-2*x-2}.x).{x.5}).K K 2 2 2 5 K 0.y})...colour=black).. 5 4 3 2 1 K 3 K 2 x K 1 K 1 K 2 K 3 K 4 1 We can find the missing one by specifying ranges O fsolve({y=x^2-4.y=0. evalf(sol[1])..0). O f:=x^2+3*x+1.464101615.1. x = K 2.x=-1. f := x2 C 3 x C 1 sol := 1 2 5 K 3 3 1 . x = 0.

06944110450 y = 0.c).06944110450 y 29 .5 K 1 K 0. Recall that if the tangent is y = mx + c the gradient m is the derivative at x = x0.2 0 K 0.colour=black).x=0. y=m*x+c0.4 K 0. f := x/sin x x0 := 0.1. Then we have to choose the constant c so that the line goes through the point ( x0.m*x+c0].6 fd := cos x m := cos 0. c0:=solve(f(x0)=m*x0+c..6 c0 := 0.8253356149 x C 0..6.fd). f(x0)) O f:=x->sin(x). fd:=diff(f(x).x). x0:=0. m:=subs(x=x0.6 x K 0.y=0.5 K 1 As an illustration of how this can be used. plot([f(x).1 0.1.8 K 0. we will calculate the equation of the tangent to a curve = f(x) at some point x0 say.

2). fd:=diff(f(x). semi-circle. We'll make x1 the value of x0 when this happens. we'll choose one of them.2 0. O restart. The circle (well. We first use the same method as above to calculate the equation of the tangent to the curve at a variable point x0 We'll begin by unassigning all our variables and then use a similar bit of code to that used above.fd).c). Since this produces two answers. f:=x->sqrt(1-x^2).1 0. actually) can be specified by y = ! (1+x2) and we'll find a tangent to it from the point (say) (0.6 0.x).8 y 0.2 0 0 0.8 1 m := K c0 := x0 x 1 K x02 y=K Then vary x0 until the tangent goes through the point (0. y=m*x+c0. c0:=solve(f(x0)=m*x0+c.4 0. 2). f := x/ 1 K x2 fd := K x 1 K x2 x0 1 K x02 1 1 K x02 C 1 1 K x02 0.4 x You could now go back and alter the function f and the point x0 and run the same bit of code to calculate the equation of the tangent to anything! As a further illustration. we consider the problem of finding a tangent to a circle from a point outside the circle.6 0. m:=subs(x=x0. 30 .

m1*x+c1]. y=m1*x+c1. The first is a for-loop.2.The gradient m1 of the tangent is then the value of m at this point and the intercept c1 on the y-axis is the value of c0 at this point. O x1:=solve(subs(x=0.m). y = 2 by any other point and work out the equation of the tangents going through that point.y=m*x+c0). 1 x1 := K 3 2 m1 := 1 2 4 4 3 xC 4 3 c1 := y= 1 2 4 We'll plot the answer to see if it really works.x0)[1]..y=2. Looping There are several ways to get Maple to perform what is called a "loop". m1:=subs(x0=x1.colour=black).x=-2.c0). O plot([f(x). c1:=subs(x0=x1. You could now replace x = 0. So we can find the equation of the tangent: y = m1 x + c1.y=-1. You put what you want done between the do and the end do. 2 y 1 K 2 K 1 0 1 x 2 K 1 Asking Maple for the second solution above would give another tangent to the circle. 31 .2..

x := 1 x := 2 x := 3 x := 4 x := 5 It is often better to stop Maple printing out everything it does. you can include it in "back quotes" (between the left-hand shift and the z). O for i from 1 to 5 do x:=i. (a semi-colon).Use Shift-Return to get a new line without Maple running the code. Error. print(x). one_word two words "two words" O print(two words). However. end do: 1 2 3 4 5 If you want to include some text. Putting a : instead of a . if you do want it to print something about what is going on. O for i from 1 to 5 do x:=i. print(`two words`). See below for the effect of the usual ".` There is another forms of for-loop: one in which we get the variable to increase itself by more than one between implementing the things in the loop: O for i from 1 to 15 by 3 do x:=i. but more than one word can't. Single words (which Maple will interpret as variables) can get away without quotes. after any command will stop Maple from printing it out as it executes the command. you can ask for it with a print command. end do. 32 . print("two words"). You can do this by putting a : (a colon) after the loop instead of . O print(one_word). missing operator or `.

4 the value of x is . term:=a:total:=0: 33 . a := 1 d := 2 10000 In a similar way. a := 3 r := 2 3145725 As an illustration of another kind of loop we answer the question of how many terms of a GP we need to take before the sum is > (say) 10000 ? We use a while-loop which will be implemented until the "boolean expression" in the first line of the loop becomes False. 3. 1 the value of x is . 5. 9. 7. 12. end do: total. O a:=3. term:=term*r. named after the English mathematician George Boole (1815-1864) who was one of the first to apply mathematial techniques to logic.O print(`the value of x is `.x). since it takes Maple much longer to print than to calculate..r:=2.. . Suppressing printing has the effect of speeding things up. 10 the value of x is . term:=a: total:=0: for i from 1 to 100 do total:=total+term.. term:=a: total:=0: for i from 1 to 20 do total:=total+term. A boolean expression.. 7 the value of x is . end do: the value of x is . we calculate the sum of an Arithmetic Progression (AP) to several terms e. we can calculate the sum of a Geometric progression (a GP) like 3. 24.1. O a:=1. We will suppress printing in the loop.g. O a:=3. . 13 As an illustration of what to do with a loop. is something which takes the values either True or False. term:=term+d. end do: total. 6. r:=1.d:=2.

We'll look for solutions a. if the boolean expression (between the if and the then) is False. if a > 60 then print(`a is bigger than 60 `). For example. O a:=90. then nothing gets done. term:=term*r. O a:=50.end if. a := 3 r := 1. It asks for an integer solution ( a. if a>60 then print(`a is bigger than 60 `). We put what we want Maple to do between the then and the end if.1 61 If clauses We now show how Maple can make a choice of several different things to do. b for small(ish) values of a and b.consider Pell's equation . We can alter that: O a:=50. a := 90 a is bigger than 60 In the above. if a > 60 then print(`a is bigger than 60 `). a := 50 a is bigger than 40 but less than 60 We can apply these ideas to get Maple to search for the solutions of an equation. a := 50 a is smaller than 60 You can put in lots of other alternatives using elif (which stands for else if ). else print(`a is smaller than 60 `). count:=count+1. elif a > 40 then print(`a is bigger than 40 but less than 60 `). else print(`a is smaller than 60 `).O count:=0: while total < 10000 do total:=total+term. named (probably incorrectly) after the English mathematician John Pell (1611 to 1685) but in fact studied by the Indian mathematician Brahmagupta (598 to 660) much earlier. Here is an example. 34 . b) to the equation a2 K n b2 = 1 where n is a fixed integer. This branching is controlled by soething called an if-clause. end do: count.end if.end if.

400. 2401. 9. 225. To add extra elements: O L:=[]. 64.-1].4.end do: n := 5 9. 1849. 16. 676. 196. 2304. 3. out of bound assignment to a list The elements of a list are op(A) which stands for the operands of A.5]. 841. The last element of L is L[-1]. 121. 784. 729. 1296. A[2.O n:=5. 1521. 1024. for a from 1 to 1000 do for b from 1 to 1000 do if a^2-n*b^2=1 then print(a. 5 3 4 2. 72 Lists A list in Maple is an ordered set and is written with [ ]. 3.2. 289. 5 You can't assign to an element that isn't there! O A[6]:=22. 2025. 324. 2209. 49. 4 161. 81. 1764. 36. 35 . The nth element of a list L (say) can then be referred to later by L[n].b). 4. 361. A[2]:=55. 169. 1600. 25.end do: L. It is often convenient to put results of calculations into such a list. 1681. 4. 2116. 1936. A[-2]. 2500. A[3]. 3. 4. O A:=[1. 4. 441. 625. 256. for i from 1 to 100 do L:=[op(L). 1444. A := 1. end do. 484. 900. 1369. end if..i^2]. You can treat the elements of a list as variables and assign to them. etc. 144. A. 1156. 2.3. 529. L := 1. 576. Error. 1089. 100. 961. 2601. 55. 1225. 5 A2 := 55 1.

36.10.L0). 2116. 961. 9. 16. 8464. 5776. 4356. 5625. O L0:=[1. 4761. 900. 2. 81. 3600.n=1. 5 As above one can use a for loop to put elements into a list.3. 3721. 0 T := 1. 441. 1849. 3. 9604. O nops(L). 36. 4900. 36 . 2. 5184. 400.4. 3. 7056. 1521. 7744. 6561. 5 1. 3969. S := 0. 3364. 6241. 6241. 2916. L0 := 1. 1444. 9216. 5041. 7921. 3. 2401. 9025.. 64.5]. 2809. 4225. 2209. 5329. 3721. 6084. 6724. 2601. 1681. You can use $ instead of the seq function (but the variable you use must be unassigned this time). 8649. 7569. 121. T:=n^2 $ n=1. 25. 5 L1 := 2. 4.2704. 9409. 4096. We could have done the same thing using the seq function: O M:=[seq(n^2. 196. 49. 8281. Note that the original list is unchanged. 81. 3969. 576. 6400. 5329. 484. 10000 O restart. 4900. 4624. 9409. 3249. 3025. 289. 4489. 4096. 256. 5476. 1764. 4356. 4624. 9216. 6400. L0. 4. 0. 9801. 5184. 49. 7921. 7225. L1:=subsop(-1=NULL. 1296. O S:=0 $ 5. 2704. 529. 4. 4 L2 = 55. 3844. 1156. 7396. 7744. 8100. 729. 7056.2]. 1089. 9. 100.2. 10000 The number of elements in a list is nops ( = number of operands). 7569. 5625.100)]. 625. 2. 3481. 4489. 4. M := 1.1=NULL. 6724. 5929. 3844. 5041. 9604. 100 You can delete elements from a list (or replace then) using subsop (= substitute operand). 7396. 8464. 8836. 8281. 324. 6889. 5776. 225. 3364. 5929. 5476. 676. 8100. 16. 7225.[]]. 4761. 9025. 0. 3249. 64. 3. 4. 1600. 3481. 100 The elements of a list do not need to be all the same kind and they can even be other lists: O N:=[x^2. 9801.. 1225.[1. 3136. 841. 144.L0). 2304. 6889. 1936. 2025. 361. 2500. 2916. 1369. 784. L2=subsop(1=55. 8649. 2809. 4225. 3136. 3025. 1024. 169. 3600. 8836. 0. 6084. 6561. 25.

181. 263. 691. 149. 151. 809.6.6.7]). 599. 683. 5. 641. 293. 929. 389. 719. 73. 449. 617. 653. 5. 827. 607. 701. 43. 601. 269. 787. 271. 727. 971. 337. 251. 977. 71. 751. 367. 937. 953. 79. 281. 907. 199. 383. 773. 829. 853.10. 101.end do: L. 37. 523. S := 3. 839. 397. 467. 613. 631. 8 T := 1. 317. 89. 677. 233. O S:={5. 113. 3 You can add an element to a set using union: O S:=S union {13}. 487. 911. 31. 661. 461. 919. 137. 131.3. The function isprime returns either true or false. 17. 647. 179. 991. The elements are not in any particular order. 571. 421. here are all the primes < 1000. 1. 997 There are 168 of them! O nops(L). and if an element is "repeated" it will be left out. 5. 409. 479. 769.3}. 8. 821. 313. 563. 53. 463. 223. 97. 1. 10 You can do quite useful things with lists. 19. 811. 67. 577. For example. 433. 109. 5. 757.8}. 857. T:={1. 593. 733. 673. 229. 877. 83. 401. 211. 191. 547. 13 You can use other set-theoretic connectives. S := 3. 107. O L:=[]: for i from 1 to 1000 do if isprime(i) then L:=[op(L). 241. 761. 331. 6. 431. 587. 659. 373. 491.i]. 557. 983. 439. 709. 41. 37 . 739. 239. 967. 59. You can sort lists: O sort([1. 3.N := x2. K 4. 509.-4.end if. 941.5. 797. 157. 127. 349. 823. 503. 13. 379. 499. 643. 163. 139. 569. 863. 457. 227. 619. 307. 353. 2 . 103. 541. 23. 6. 197. 277. 2. 359. 168 Sets Maple can also deal with sets. 47. 883. 347. 2.2. 283. 521. 257. 311. 7. 6. 743. 61. which it puts in { }. 859. 193. 443. 167. 887. 881. 11. 419. 173. 29.2. 7. 947.

4 1. We may list these values either as a list: O for i in [2. What you sum over had better be an unassigned variable. 5 the value of x is .2.4} intersect {3. 338350 In fact Maple is clever enough (sometimes) to even work out the general formula for a sum — and can (sometimes) sum all the way to infinity.n). If Maple uses a set it will usually put it into order before implementing the commands (but don't count on it). n = a . {1.3. 2 the value of x is .x). end do: the value of x is .i=1. end do: the value of x is ..5.3} do x:=i.100).5.infinity).n=1. 5 the value of x is .x).4.3. 8 You can find out more with the Help command: ?list Summing Earlier we used a for loop to sum the terms of Arithmetic and Geometric Progressions. 3.3] do x:=i.6}.5. 8 the value of x is .4} minus {3. You enter sum(expression.8. 38 . sum(a*r^i. 3 or as a set. print(`the value of x is `.2. print(`the value of x is `..5. O restart. sum(a*r^i.O {1.i=1. The process of summing the terms of a sequence is so common that Maple has a special function that lets you do it without writing your own loop. 3 the value of x is . 2 We can get Maple to loop over only certain specified values..4. O sum(n^2. b) and Maple will take the sum over the range from a to b..6}. 2 the value of x is . O for i in {2.8. or there will be trouble.

infinity). sum(cos(sqrt(n)*Pi). O sum(cos(sqrt(n)*Pi)..i=1. sum(i*(-1)^i.n)...N). N n=1 " 3 > cos n ! 6 ! C cos 7 ! K 1 C cos C cos 2 ! C cos ! 8 ! C cos 3 C cos 5 ! C cos 10 ! K 1. 1 2 ! 6 In the next case it gives the answer in the form of the Riemann zeta function. 50. a rn C 1 ar K rK 1 rK 1 K ar rK 1 C 1 sin 1 2 1 sin 1 cos n C 1 2 cos 1 K 1 K 1 1 sin 1 cos 1 sin n C 1 K 2 2 cos 1 K 1 It knows the answer to the "Basel" problem that Leonhard Euler (1707 to 1783) solved: O sum(1/i^2. O sum(1/i^3. Sometimes it can't do it.i=1.infinity)...877848844 Maple knows what happens if "something goes wrong" O sum(i*(-1)^i.100). 39 .i=1..10).n=1.O sum(sin(i).infinity).. N 50 K 1 i >i i=1 As with Diff and Int using a capital letter just prints the formula: O Sum(i*(-1)^i.i=1. evalf(%).100). 100 >i i=1 K 1 i O evalf(%)..i=1.n=1.i=1.

count:=count+1. so that if the same variable names had been used somewhere else. term := term * r. term := a. we tell Maple what the values of a and r are and then apply the procedure to a number. Note that variables which are only needed inside the procedure get declared to be local . If we had wished we could have put return count. end proc. O howmanyterms:=proc(x) local term. count. as the last thing in the "body of the procedure".count.r:=2. we can adapt the code we wrote above to define a procedure which returns the number of terms of a Geometric Progression needs for its sum to go past some given value n. total := 0. a := 3 40 . end proc. term:=term*r. To call the procedure. term:=a:total:=0: count:=0: while total<x do total:=total+term. This is in fact shorthand for using a construction known as a procedure. f := proc x x ^2 end proc 400 Procedures can act like functions and return a value (like x2 in the above example) but can implement functions which are more complicated than just evaluating a formula. count := 1 C count end do. howmanyterms := proc x local term. We can get the same effect with: O f:=proc(x) x^2. count := 0. these will not be changed by the procedure. howmanyterms(10000). end do: count.Procedures We saw earlier how to define a function using an assignment like: f := x -> x^2. total . while total ! x do total := total C term.total. O a:=3. indenting the code to indicate where the procedure or loops start and finish. For example. count end proc The value returned by the procedure is the last thing in the listing before the end statement. f(20). Notice that Maple will pretty-print the procedure.

f := proc x if x ! 0 then x ^2 else x C 1 end if end proc 1 4 We can plot this function. end do. count:=0.r := 2 12 We can use an if-clause to define a function. return count. O f:=proc(x) if x < 0 then x^2. When we plot this we get a kind of "step function". 1 ).5 1 We count the primes up to a real number n. by: plot( f . otherwise Maple gets unhappy.-1. f(-1). end if. i. or you can do it by putting in some single quotes: ' ': plot('f(x)'.end if. .5 K 1 K 0.. for i from 1 to n do if isprime(i) then count:=count+1.end proc. 2 1. You can either plot them without mentioning the variables at all. O countprimes:=proc(n) local count. O plot(f.1).. for i to n do if isprime i then count := count C 1 end if end do. countprimes := proc n local count. return count end proc 41 . f(3). else x+1. -1 . x = -1. but it is necessary to be a bit careful plotting functions defined by procedures.1).i. count := 0.5 0 0. end proc.5 1 0. If the variable x had been assigned to you would have to put that in quotes too.

plot(countprimes.08)..910).1.100).1. 25 20 15 10 5 0 10 20 30 40 50 60 70 80 90 100 160 100 60 0 100 200 300 400 500 600 700 800 900 1000 There is a "flat bit" near 890.880.. 155 154 153 152 151 880 890 900 910 The French mathematician Adrien-Marie Legendre (1752 to 1833) approximated the growth of the number of primes with the function: O L:=x/(log(x)-1. O plot(countprimes. In fact a sequence of 19 consecutive non-primes from 888 to 906.O plot(countprimes.1000). L := x ln x K 1.08 42 ..

O plot(['countprimes(x)',L],x=0..100,0..25,colour=[black,red]);

25 20 15 10 5 0 0 20 40 60 80 100

x

The German mathematician Carl Friedrich Gauss (1777 to 1855) used a different approximation: the logarithmic integral : O logint:=proc(x) return int(1/log(t),t=2..x); end proc; logint := proc x return int 1 / log t , t = 2 ..x end proc O plot([countprimes,logint],1..100,colour=[black,red]);

25 20 15 10 5 0 10 20 30 40 50 60 70 80 90 100

You can see these different approximations together(we'll specify the colours since otherwise Maple will use colours which do not print well): O plot(['countprimes(x)',L,'logint(x)'],x=0..100,0..25,colour= [black,red,blue]);

43

25 20 15 10 5 0

0

20

40 x

60

80

100

More Procedures

If you want to make a procedure do something to a variable you have already defined outside the procedure, you have to declare it as global. We'll add an element to the end of a list. In this case we don't want to return anything so we put return; and nothing else at the end of the procedure. O addon:=proc(x) global A; A:=[op(A),x]; return; end proc; addon := proc x global A; A := op A , x ; return end proc Then apply this: O A:=[1,2,3]; addon(0); A;

A := 1, 2, 3 1, 2, 3, 0

We could do something similar without using a global variable. But notice that in this case the original list is unchanged. O addend:=proc(A,x) return [op(A),x]; end proc; addend := proc A, x return op A , x end proc O A:=[1,2,3]; B:=addend(A,0); A;

A := 1, 2, 3

44

B := 1, 2, 3, 0 1, 2, 3 One thing to be careful of is that you cannot assign to the parameters passed to the procedure as if they were local variables. O test:=proc(n) n:=n+1; return n end proc; test := proc n n := n C 1; return n end proc O test(9); Error, (in test) illegal use of a formal parameter

Reversing

As an example of the use of procedures we'll use Maple to find a solution to the following problem: Find a four digit number which is multiplied by 4 when its digits are reversed. We start by defining a procedure which turns the digits of a number into a list. Note that we test each procedure as we write it. O digits:=proc(n) local ans,m,d; m:=n;ans:=[]; while m<>0 do d:=m mod 10;m:=(m-d)/10;ans:=[d,op(ans)]; end do; return ans; end proc; digits := proc n local ans, m, d; m := n; ans := ; while m!O 0 do d := mod m, 10 ; m := 1 / 10 * m K 1 / 10 * d; ans := d, op ans end do; return ans end proc O K:=digits(12345008); K := 1, 2, 3, 4, 5, 0, 0, 8

**It's easy to write a list in the opposite order: O reverselist:=proc(L) local i,M; M:=[];
**

45

end proc. for i from 1 to nops(L) do ans:=10*ans+L[i]. end do. i. for i to nops L do M := L i .i. return M Now we have to get a number back from its list of digits O buildit:=proc(L) local ans. op M end do. end if end do: 2178 Do the same for a 5 digit number O for n from 20000 to 25000 do 46 end proc 13587 . 5. 0. 0.op(M)]. Now we can look for our four digit number O for n from 1000 to 9999 do if reversenum(n)=4*n then print(n). ans:=0. buildit := proc L local ans.O for i from 1 to nops(L) do M:=[L[i]. reversenum := proc n buildit reverselist digits n O reversenum(78531). 12345008 Put the ingredients together: O reversenum:=proc(n) buildit(reverselist(digits(n))). reverselist := proc L local i. return ans. 2. end proc. M := end proc O reverselist(K). for i to nops L do ans := 10 * ans C L i end do. 8. 3. M. return M. ans := 0. end proc. 4. end do. return ans end proc O buildit(K). 1 .

n := 1790 2761 4433 7777 Palindromic in .. end do: print(`Palindromic in `. print(n).count. count:=0: while reversenum(n)<>n do n:=n+reversenum(n).O if reversenum(n)=4*n then print(n). count:=count+1. end if end do: 219978 So it looks as if we might have a theorem! O 219999978*4. steps Numbers to try are 89 or 296 or . count:=0: while reversenum(n)<>n do n:=n+reversenum(n). . computation interrupted 47 .` steps`). A number NOT to try is 196 O n:=196. 3.count. print(n).` steps`). n := 196 887 1675 7436 13783 52514 Warning.. count:=count+1. 879999912 An old question asks if one can always make a number "palindromic" by adding it to its reverse O n:=1790. end do: print(`Palindromic in `. end if end do: 21978 and even for a 6 digit number O for n from 200000 to 250000 do if reversenum(n)=4*n then print(n).

20 15. 65 . 8. integer) to check whether a number has an integer square root. For example.sqrt(x^2+y^2)) fi. 40. 35. 7. 39. 91. 73 . 41 . 101 . 20. 36. 5 . 13 .[x. 97 Some other equations Similarly. 4. Notice how we combine the two conditions with an and. 24. 15. 33. We do this by insisting that x and y have no factor bigger than 1 in common. 21.y) = 1 then L:=[op(L). 20.integer) then print(x. 85 . 13 6. looking for Pythagorean triples satisfying a2 C b2 = c2 we can use the Maple function type(x. 80.. 4. 9. 65. 29 . 53 . 84. 17 . We arrange this using the igcd (= integer greatest common divisor or highest common factor) function.y. we can look for non-zero solutions of other equations like a2 C 2 b2 = c2 or 2 a2 C 3 b2 = c2 or . 45.integer) and igcd(x. od: od: L. y) twice. We'll leave out those which are multiples of others we have found.y. 85 . 37 . 25 Here are all solutions up to x = 100 and y = 100. 12. 72. 12. 48. 60.Pythagorean triples We can use Maple to search for solutions to equations with integer solutions. 99. 16. 25 . 11. 56. 109 . 3. 15 12. O L:=[]: for x from 1 to 100 do for y from x to 100 do if type(sqrt(x^2+y^2). 12. 20. 17 9. 55.. 5. 65 . Such equations are called Diophantine after the Greek mathematician Diophantus of Alexandria (200 to 284 AD). 89 . 63. 12. 77. 10 8. od od: 3. 61 . 16. O for x from 1 to 20 do for y from x to 20 do if type(sqrt(x^2+y^2). fi. 15. 8. Sometimes we don't find any and then we could try and prove mathematically that no such solution exists! 48 .sqrt(x^2+y^2)]]. 28. 60. We take y # x since otherwise we will get each pair ( x. 13. 5 5.

c). O n:=5: for x from 0 to 2000 do y:=sqrt(n*x^2+1). if type(y. (1067571958860. 3 12. 361643617). r and each point will be a list of length 2. q. (39695544. 99 408. end do: 0. We'll call the points p. 13447). y) to the equation n x2 C 1 = y2 where n is a fixed integer. 1 2. 577 Can you see what recurrence relation is satisfied by the solution? If you could you could generate lots more solutions. Though if you take the clue from the last one you might manage it! The next pair is: (23184. if type(c. 1595011813884802) Interpolation A parabola has an equation y = a x2 C b x C c with three coefficients we can choose. end if. end if. 9726043422151).O for a from 1 to 100 do for b from 1 to 100 do c:=sqrt(2*a^2+3*b^2). 1 The Indian mathematician Brahmagupta solved the equation in 628AD with n = 83 and found solutions: (9. 59307347962).integer) then print(a.b. (242055. 2205226). O n:=2: for x from 0 to 2000 do y:=sqrt(n*x^2+1).y). (1476. 51841) O 51841^2-5*23184^2. 161 1292. and solve it more efficiently than we did before. end do end do: Work modulo 3 to see that one can't find solutions to this last one! We can now look again at Pell's equation . So in general one can find a (unique!) parabola through any three points in the plane. end do: 0. if type(y. 2889 It's harder to spot the relation this time. 1 4. end if.integer) then print(x. 9 72. We look for a solution ( x. 17 70. (6509827161.y).integer) then print(x. 82). 49 . (175075291425879.

eqn2. You can use ?plot[options] to see what all the other things you can specify are. 1.b.Q]). a. eqn2. par:=a*x^2+b*x+c. parab := proc p. eqn1:=subs(x=p[1].-3. q. We do this by assigning our plots (things Maple calls "Plot structures" to variables P and Q and then using the display function from the plots package.[1. 2 eqn1. K 2 . r local par.r) local par.O restart. a.style=point. 2. K 1 . points := K 1.3]).x=-2..2].-1].q.c. subs(s.3): Q:=plot({points}.c}). b. q 2 = par .-1]. P:=plot(parab(points). eqn1.a. par := a * x ^2 C b * x C c.symbolsize=20. end proc. r 2 = par . s:=solve({eqn1. c. colour=blue): plots[display]([P.r[2]=par). eqn2:=subs(x=q[1]..[2. p 2 = par .3. b.-2].b. par end proc O parab([-1. c .q[2]=par). O parab:=proc(p.s.{a. eqn2 := subs x = q 1 . s.eqn3}. s := solve subs s.par).-4]. eqn1 := subs x = p 1 .eqn2. Note how we plot the individual points. 3 2 1 K 2 K 1 K 1 K 2 K 3 50 1 x 2 3 . 5 7 K x2 C xC2 2 2 We'll now plot the parabola and some points.symbol=cross. O points:=[-1. eqn3 := subs x = r 1 .[1.eqn3.p[2]=par).eqn1. eqn3 . eqn3:=subs(x=r[1].eqn2.[2. eqn3.

end do: plots[display](L.coords=polar.t..5 0 K 0. If you want to see the result with the frames in sequence put in insequence=true otherwise you'll get them all on top of one another..insequence=true). Put all the curves (one "frame" at a time) into a list and then give it to plots[display]. O L:=[]: for k from 0 to 10 by 0. The only problem with this is that it can produce very large files. 1 0.More plotting You can use a similar method to animate the drawing of (for example) curves.t=0.-1.1)]. Then click on the window and choose Play from the Animation window or click on the Play icon on the tool bar.thickness=2)] .plot(cos(k*sin(x)).1 to 10 by 0.Pi.x=0. 1 x 2 3 2 1 K 3 K 2 K 1 0 K 1 K 2 1 2 O L:=[]: 51 .5 K 1 In polar coordinates: O L:=[]: for k from 0.1 do L:=[op(L).insequence=true).plot([(t/5). O restart.2 do L:=[op(L).k*Pi]. end do: plots[display](L..

insequence=true).1 do 52 .4 K 0..insequence=true).1.6 K 0.t.5 K 1 You can also animate 3-dimensional pictures in a similar way..2*Pi].01 do L:=[op(L).x=-1. end do: plots[display](L.8 K 0.y=-1. If you do a lot of this.plot3d(k*(x^2+y^2). there are commands plots[animate] and plots[animate3d] which you can learn about.t=0.5 K 0.2 0.1.2 0 0.5 do L:=[op(L). O L:=[]: for k from -1 to 1 by 0. O L:=[]: for k from 0 to 3 by 0.plot([sin(k*cos(t)).6 K 0. 1 0.coords=polar)].view=-1..O for k from 0 to 20 by 0. end do: plots[display](L.1)]..4 0.

sin(p)*sin(q).p=0. 21. He introduced them in a problem involving rabbit breeding. 1. One of the best known illustrations is the process by which the Fibonnaci numbers: 1. are calculated. 9227465 53 . Maple will let procedures call themselves.. 8.2*Pi)].plot3d([sin(p)*cos(q).O L:=[op(L).. These were introduced by Fibonacci of Pisa (1170 to 1250) who was the person who introduced the Arabic (or Indian) numeral system to Europe. 13.p^k]. 5. fib := proc n if n ! 3 then 1 else fib n K 1 C fib n K 2 end if end proc O fib(35). Of course they can't carry on doing this indefinitely. end do: plots[display](L.. but in fact they were known by the Indian mathematician Hemchandra more than 50 years earlier (and other Indians had considered them even before that).insequence=true). O fib:=proc(n) if n<3 then 1. This is called recursion.else fib(n-1)+fib(n-2). so things have always got to be arranged so that the process terminates.. 2. . Recursion O restart.end proc.Pi.q= 0. 3.end if.

if n ! 3 then 1 else fib n K 1 C fib n K 2 end if end proc O count:=0:start:=time(): fib(30). 15.else fib(n-1)+fib(n-2). We can see how the time increases by modifying our program. 832040 1664079. Maple has a device: option remember. calls .` calls `. secs 54 . 5.end if.` calls `. end proc.` secs`). count := count C 1. calls . 9227465 0.003. if n ! 3 then 1 else fib0 n K 1 C fib0 n K 2 end if end proc O count:=0:start:=time(): fib0(300). count:=count+1. secs O count:=0:start:=time(): fib(35).` calls `. 222232244629420445529739893461909967206666939096499764990979600 300. end proc. if n<3 then 1. count := count C 1. This makes the calculation linear in n.time()-start. O fib:=proc(n) global count. count:=count+1.` secs`). print(count.else fib0(n-1)+fib0(n-2). print(count. calls .time()-start. print(count. O fib0:=proc(n) option remember. 0. secs To get round this.698. if n<3 then 1. global count.` secs`).end if. which stores the result of any calculation so it doesn't have to do it again. global count.374. fib0 := proc n option remember.Unfortunately the process by which this works is "exponential" in n and so it is not a very practical algorithm. fib := proc n global count.time()-start.

Warning. elif r=0 then 1 else bc0(n-1. end proc. r global count.` calls `. elif r=0 then 1 else bc(n-1.r). 3268760 10623469.r) option remember. end if.10). calls .10). 55 .time()-start.` secs`). computation interrupted To see why: O bc:=proc(n.r). if r>n then 0. print(count.end if. bc := proc n. print(count. elif r=0 then 1 else bc(n-1.r-1)+bc0(n-1.469.r) global count.` secs`). 3. count := count C 1.r-1)+bc(n-1.end if. r K 1 C bc n K 1. 15. r end if end proc Unfortunately this too soon gets out of control! O bc(30. if r>n then 0.13).` calls `.057. 184756 705431. end proc. secs O count:=0:start:=time(): bc(25. if n ! r then 0 elif r = 0 then 1 else bc n K 1. r K 1 C bc n K 1. secs Again option remember. calls . gets us out of the hole! O bc0:=proc(n.time()-start. r if n ! r then 0 elif r = 0 then 1 else bc n K 1. bc := proc n. r end if end proc O count:=0:start:=time(): bc(20.We can apply a similar process to calculating the elements of Pascal's triangle. O bc:=proc(n. end proc. count:=count+1.r). count:=count+1.r-1)+bc(n-1.r) if r>n then 0. global count.

184756 131. global count.end if.7896).1. b if mod b.b) if b mod a =0 then a else hcf(b mod a. b local t. a = 0 then return a. O euclid:=proc(a. return t 1 . euclid := proc a. count := count C 1. if n ! r then 0 elif r = 0 then 1 else bc0 n K 1.` calls `.t[2]. if mod b. a . calls . a . This already exists as a standard Maple function igcd. 1.001.time()-start. print(count.a). secs More recursion The highest common factor or greatest common divisor can be defined very economically using a recursive procedure. r option remember. end proc. t 2 56 . t 3 K t 2 * trunc b / a . r end if end proc O count:=0:start:=time(): bc0(20. a .a). a end if end proc O hcf(12345.` secs`). hcf := proc a. 0 else t := euclid mod b. r K 1 C bc0 n K 1.end proc.bc0 := proc n. O hcf:=proc(a.b) local t. You can get this as a standard Maple function as igcdex (= extended integer gcd ). if b mod a=0 then return a. 0. end if. a = 0 then a else hcf mod b.7896). igcd(12345.10).0. 3 3 One can also implement the Euclidean algorithm which writes the hcf as a combination of the original numbers. return t[1]. else t:=euclid(b mod a.t[3]-t[2]*(trunc(b/a)).

3.7896). For example. end do: count. Maple has a function nextprime which would do this for us. he would spent 15 minutes calculating the primes in a "chiliad" ( a range of a 1000 numbers). end proc. count := 0.i. local count. By the end of his life.t.'t').end do: a.end if end proc O euclid(12345. Maple tests for primeness using the function isprime which returns the answer True or False. i. countprimes := proc n option remember. 57 .) O a:=1234567.7896. O countprimes:=proc(n) option remember. we may look for the next prime after (say) 1234567 (Actually.'s'. it is reckoned that he had counted all the primes up to about two million. local count. K 197.fi. a := 1234567 1234577 We may count the number of primes in any given range. 3 K 197 308 Looking for prime numbers Recall that a prime number is an integer which is not exactly divisible by any smaller integer except ±1. The German mathematician Gauss (1777 – 1855) was interested in how the primes were distributed and when he had any free time.s. but let's not spoil the fun. count:=0: for i from n*1000+1 to n*1000+1000 by 2 do if isprime(i) then count:=count+1. for i from 1000 * n C 1 by 2 to 1000 * n C 1000 do if isprime i then count := count C 1 end if end do. while not isprime(a) do a:=a+1. 308 O igcdex(12345.

The mathematician Leonhard Euler ( 1707– 1783) discovered that the formula x2 K x C 41 is particularly prime-rich. howmanyprimes(400). count := 0..x=0. O howmanyprimes:=proc(n) local count. howmanyprimes(40). 160 150 140 130 120 110 100 90 80 0 20 40 x 60 80 100 There are some other interesting places to look for primes. for x to n do if isprime x ^2 K x C 41 then count := count C 1 end if end do.count end proc O plot('countprimes(round(x))'.end if. count:=0: for x from 1 to n do if isprime(x^2-x+41) then count:=count+1. for x = 41) and for lots of others as well. of course. howmanyprimes := proc n local count.x. end do: count. end proc. In fact it produces primes for every integer from 1 to 40 (but not. count end proc 40 270 58 . x.100).

257 16. 618970019642690137449562111 59 . ifactor(a). 7 5. He too investigated primes and looked at numbers of the form 2n – 1. 31 7. though a lot of effort has gone into looking for them. 8191 17. investigated primes in the sequence 2n + 1.end do: 1. O for n from 1 to 50 do if isprime(2^n+1) then print(n. but that n = 32 does not give a prime. 127 13. (In fact nobody knows if the formula 22 +1 produces any other primes after m = 4. 5 4. 17 8. Euler was the first to show (150 years after Fermat guessed that 232 + 1 would be prime) that it is composite. You can use the Maple function ifactor (= integer factorise) to verify this. 3 3. a := 4294967297 641 6700417 m One of Fermat's correspondents was the mathematician Mersenne (1588 – 1648).For many years mathematicians have tried to find big primes.end if. 65537 You should note that the values of n which give primes are all of the form 2m. O for n from 1 to 150 do m:=2^n-1. 131071 19.2^n+1). 524287 31. 2305843009213693951 89. end if.) O a:=2^32+1. 3 2. if isprime(m) then print(n. The French mathematician Fermat (1601 – 1665) best known for his so-called Last Theorem.m). end do: 2. 2147483647 61.

In 1876 he proved that the number 2127 K 1 (see above) is prime. With a bit of effort. 1108378657 270549121 Prime numbers of the form 2n K 1 are called Mersenne primes and they are almost always the largest primes known. 162259276829213363391578010288127 127.297818850 107 This is (well) outside the range of Maple. if one can find a number a for which the above does not hold. Note that to run a test like this we need to be able to work out high powers efficiently. 170141183460469231731687303715884105727 In fact it is fairly easy to show that if n is not prime the neither is 2n – 1. O (2^35-1)/(2^5-1).107. O isprime(2^521-1). The most recent is 237156667K 1 and was discovered on 6th September 2008 using GIMPS (the Great InterNet Mersenne Prime Search). The largest (and largest known prime) was the 45th to be discovered and was found on August 23rd 2008 and is 243112609K 1. then p is not prime. If the above holds with a = 2. O p:=3456789. 7 then we'll call p a probprime. 235 – 1 is divisible by 25 – 1 and by 27 – 1. true Testing for primes The Maple function isprime uses an indirect method for deciding whether or not a number is probably prime. First: how not to do it: To calculate (say) 23456789modulo 3456789 you can get Maple to calculate this (very) big number and then reduce it modulo 3456789. At present 46 Mersenne primes are known. It is based on a Number Theory result called Fermats Little Theorem: If p is prime then for any a we have a p = a modulo p. 60 . O evalf(43112609*log[10](2)). This is because a French mathematician called Lucas (1842 – 1891) invented a test for such primes using the Fibonacci numbers.(2^35-1)/(2^7-1). but you can test the next after those listed above. Maple has some tricks for doing this. This was the largest known prime until people started using computers in the 1950's. In particular. For example. 1. we can show that it has 12 978 189 decimal digits (and won a prize of $100 000 for being the first one found with more than 10 million digits). 3.

it can reduce modulo p as it goes along and never have to handle integers bigger than p. end proc. 3 and testa n.7). end if.O n:=2^p: n mod p. 2 =2 O probprime:=proc(n) testa(n. true Let's see how good out test is by comparing it with the isprime function in Maple. a mod a &^ n. 2&^p mod p.. 5 and testa n.3) and testa(n.5) and testa(n.2). O p:=3456789. They are called Carmichael numbers or pseudoprimes. probprime := proc n testa n. p := 3456789 2288630 Much more efficiently. testa := proc n.end proc. O for n from 3 to 2000 by 2 do if probprime(n) and not isprime(n) then print(n). end do: 561 1105 1729 In fact. The isprime test in Maple uses a (slightly) more sophisticated test. the above numbers will pass testa for any value of a. p := 3456789 2288630 So we'll test a prime candidate with some a: O testa:=proc(n. It is not known to produce any incorrect answers! The Linear Algebra package 61 . 2 and testa n.a) a&^n mod n=a. 7 end proc O probprime(1234577).2) and testa(n. To do this use &^ instead of ^. n = a end proc O testa(1234577.

. ReducedRowEchelonForm . If you don't want to see this list. HankelMatrix . SubMatrix. HermitianTranspose. CharacteristicPolynomial . MatrixFunction . LA_Main. EigenConditionNumbers . BackwardSubstitute . MatrixScalarMultiply . DeleteColumn . Minor. SingularValues. GivensRotationMatrix . Diagonal. O restart. SumBasis. MatrixNorm. Eigenvalues . ZeroMatrix. Equal. DiagonalMatrix. . LUDecomposition. VectorNorm. QRDecomposition. Eigenvectors . ScalarMultiply . HessenbergForm. CompanionMatrix. FrobeniusForm. Row. HouseholderMatrix.package. Note that all the commands in this package have capital letters. Norm. MatrixPower. IsUnitary.A lot of clever stuff has been written for Maple and has been put into Packages. Map2. CharacteristicMatrix . To see what packages are available type in ?index. Transpose. IdentityMatrix . ScalarMatrix. ZeroVector. &x. Dimensions. ConstantMatrix . The LinearAlgebra package lets you work with Matrices (and Vectors). Modular. ColumnDimension. NullSpace . MatrixMatrixMultiply . Trace. Zip The above lets you use all the functions it lists. Copy. BandMatrix. LinearSolve. BezoutMatrix . MatrixExponential . Pivot . IsDefinite . GenerateEquations . Rank. VectorAdd . PopovForm. ForwardSubstitute . SubVector. OuterProductMatrix . HilbertMatrix . RandomVector. SylvesterMatrix . SmithForm. VectorAngle . Add. CrossProduct. Basis. IsSimilar. ToeplitzMatrix . RationalCanonicalForm . IntersectionBasis . CreatePermutation . GenerateMatrix . Map. LeastSquares. ConditionNumber. RandomMatrix. NoUserValue. MinimalPolynomial . SchurForm. GramSchmidt.. O with(LinearAlgebra). Permanent. IsOrthogonal. ). TridiagonalForm. ScalarVector . UnitVector . VandermondeMatrix. ConstantVector . BilinearForm. Column. GetResultShape . RowOperation. ColumnSpace. VectorScalarMultiply . VectorMatrixMultiply . ColumnOperation. MatrixAdd. DeleteRow . GaussianElimination . 62 : instead of . RowDimension. MatrixVectorMultiply . Multiply . HermiteForm. put when you enter with ( . BidiagonalForm. RowSpace. you will have to read the package in again. GetResultDataType . Adjoint . JordanForm. JordanBlockMatrix . DotProduct. Normalize. Dimension. If you ever use restart. Determinant. MatrixInverse.

a b A := c d B := a b c d e f a b C := c 0 0 0 Z := 0 0 You can enter a matrix by rows: written < a | b | c > or by columns: < a . b . B:=Matrix(2.) The second parameter is a list of lists.e.5. (In fact you don't need to read the package for this bit.b.c].d]]).1). O A:=Matrix(2.A matrix is a "box of numbers". c > and then rows of columns or columns of rows.b]. There is something called the matrix palette on the View menu which can help.[[a.e.[[a. C:=Matrix(3.c>|<d.j) -> i+2*j).[d.f]]).i>>.b].[c.f>|<g. 3 5 7 9 11 M := 4 6 8 10 12 5 7 9 11 13 63 .2. There are several ways to enter matrices. Maple will put in 0 if you don't tell it what the entry is.[[a. You tell Maple the number of rows and columns (or just how many rows if you want a square one).[c]]).b.<d|e|f>. Z:=Matrix(2. O A:=<<a|b|c>.3.<g|h|i>>.h. a b c A := d e f g h i a d g B := b e h c f i You can initialise the entries of a matrix using a double for-loop or you can use the following: O M := Matrix(3. B:=<<a.(i.

2 m1.B.A^3. (giving the inverse of the matrix). B:=<<e|f>. O A. a eCb g a fCb h c eCd g c fCd h a2 C b c a C a b C b d c c a C d c a C b c C d2 c 64 a2 C b c b C a b C b d d c a C d c b C b c C d2 d . 3 M := m2. A+B.<c|d>>. 1 m1. 1 m2. m1. A-B. 3 You can add or subtract matrices of the same size and can multiply them by a number (or a variable) using *.(i. 1 m3. 2 m3.<g|h>>. and you can take powers of matrices by (for example) A^3.You can even get a matrix with "unassigned variables" for all the entries. j) -> m[i. A := a b c d e f g h p q r s B := C := aCe bCf cCg dCh aKe bKf cKg dKh 3p 3q 3r 3s You can multiply together matrices of compatible shapes by A. O M := Matrix(3. 3 m3.j]). 2 m2.A^(-1). or A^(-1).B. 3*C. C:=<<p|q>. O A:=<<a|b>.<r|s>>.

b gKc f c eCd gKg aKh c a fCb hKe bKf d c fKb g a fCb hKe bKf d c fKb g Here are some useful matrices: RandomMatrix produces a matrix with entries in the range -99 . c eC d g p C c fC d h rK c e p C f r K d g p C h r .(B+C)-(A. a f C q C b h C s K a f K b h Ka qKb s . 65 .(B. c eC d g q C c f C d h sK c e q C f s K d g q C h s O simplify(%). c e C p C d g C r K c e K d g K c p K d r. 0 0 0 0 O A.A.C) = (A. 0 0 0 0 O A. 99. RandomMatrix(3.A. O IdentityMatrix(4). a e C p C b g C r K a e K b g K a p K b r.C and distributive A. a eC b g q C a f C b h sK a e q C f s K b g q C h s . b gKc f c eCd gKg aKh c O simplify(%).3). c f C q C d h C s K c f K d h Kc qKd s O simplify(%).B).B $ B.B-B.2).C but not (in general) commutative A. a eC b g p C a fC b h rK a e p C f r K b g p C h r . O (A.C).B).(B + C) = A.C-A..C).B + A. ZeroMatrix(2.d a dKb c K c a dKb c K b a dKb c a a dKb c Multiplication of square matrices is associative : A.B+A.(B.

3 K m2.xI where x is an unassigned variable. 3 K m3. 66 . N := a b c d a dKb c O Determinant(M). Arthur n := 2 a1.<c|d>>. 1 m3. 1 m2.1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 K 50 K 79 30 K 71 62 28 The determinant of a matrix is a combination of the entries of a square matrix (in rather a complicated way!) which has the property that it is 0 if the matrix does not have an inverse. 1 m1. 3 C m3.j)->a[i. 1 m2. This is a polynomial in x called the characteristic polynomial . 2 m1. Determinant(N). 2 m2. m1. 1 m1. O N:=<<a|b>. O n:=2. 2 1 0 0 1 A := O Id:=IdentityMatrix(n). 2 m3. 2 C m2. 2 m1. 2 a2. A:=Matrix(n. 1 a2. Id := Then we take the determinant of the matrix A .j]). 2 m3. First we take a "general matrix". 3 We can use Maple to demonstrate a theorem discovered by the English mathematician Cayley and the Irish mathematician William Hamilton . 1 a1. 3 m3.(i. 1 m2. 3 K m1.

O V:=Vector([-2. O p:=Determinant(A-x*Id). 1 a2. O with(LinearAlgebra): O A:=Matrix(2. Maple won't let you use the subs function with matrices so we do it the hard way.k=0. p := a1. p := x2 C K a1. O Q:=sum(coeff(p. That is..x. 2 x C a1.k)*A^k. 2 a2. A := It doesn't matter what vector we start at. V := K 2 1 67 2 1 3 K 4 . 1 C K a1.x). 0 0 0 0 Changing n from 2 to a bigger number will make Maple do more work but the result still holds! a1. 1 a2. 1 a1. 1 x K x a2. 2 2 The Power method As an example of how to use the LinearAlgebra package we'll look at a technique for calculating the largest eigenvalue of a linear transformation. 1 K a2. if we substitute A for x in this polynomial.1]). 2 K a1.(The roots (possibly complex!) of this are the eigenvalues . 1 a2. we get the zero matrix. In fact this method works providing the largest (in absolute value) eigenvalue is real and distinct. 2 C a1. 1 a2.) Then the Cayley-Hamilton theorem says that the matrix A satisfies its characteristic polynomial.1]. 1 a2. 1 a1.-4]]).[3. 1 K a2. 2 K a1. Q := a1. We use the fact that if we apply the transformation over and over again to a vector the resulting vectors settle down to being in the same line and this is the direction of an eigenvector (associated with the largest eigenvalue). O restart.[[2. 1 O p:=collect(p.n). 2 K a1. 2 a2. 2 a2. 2 a2. 2 O simplify(Q). 2 C x2 K a1. 1 Unfortunately. 2 a2.

.18104 0.99997 . K 10 K 0.28734 .16143 0. 0. 0.45865 0.98691 .99468 K 0. K 0.15144 K 0.88862 K 0.98831 .We'll print the vector and also something that enables us to see its direction.98899 K 0. .15245 K 0. 0. .24620 0.10276 K 0.5)).96924 .15368 0.13748 K 0.15548 0.evalf(W/Norm(W. 0. O for n from 1 to 15 do W:=(A^n). .2).0058138 K 0.99052 K 0. 68 .95780 K 16 31 K 1 K 172 K 174 685 337 K 3262 K 2588 14059 8883 K 64000 K 46234 282649 190181 K 1269298 K 888936 5647735 3869863 K 25257748 K 17518022 112640581 77604537 K 503116390 .98784 . end do: K 3 K 0.98816 . K 0.98347 .14818 K 0.V.98845 K 0. print(W. 0.

K 4.464101616 2.W)[1]/W[1]). O E:=evalf(Eigenvectors(A)).152881949816236512 0. We've got quite close to the larger eigenvalue. ev/Norm(ev. K 4.2).2).1547005384 1. 1.1]. We can now calculate how the vectors are stretched each time: the eigenvalue : O evalf((A. A := 2 1 3 K 4 K 2 0. V:=Vector([-2.988244458526477243 K 4.2). We can do the same thing with more iterations (and a different starting vector!): O A:=Matrix(2.0.98823 .V: W/Norm(W.154700534 K 0.469872901 Maple will calculate the eigenvalues and associated eigenvectors. 0.K 347907316 2245279171 1549464539 K 10024838632 .15275 K 0.-4]]). K 0. W:=(A^100).988244458599999986 So it wasn't bad.[[2.1547005384 E := . K 0.464101616 1.15313 0.152881949784357928 0.464101615 69 .9]). evalf((A.9 V := K 0.W)[1]/W[1]). To see how good our estimate of the eigenvector is: O ev:=Column(E[2]. 2.98828 After a few steps the vectors are "settling down".[3. ev := K 0.

. 0.99462 K 0. . 0. for n from 1 to 10 do W:=(A^n).[[1. .10376 . K 0. 0.17889 K 0.41295 0.89444 0.2).91074 0.75869 0. K 0.14142 .51377 .1]]).98994 K 0.73344 0.21609 K 0.65144 V := 2 4 14 K 2 8 K 44 K 124 K 68 K 328 304 584 1288 4448 K 464 3056 K 13808 K 38368 K 22976 K 107296 92128 .This process will only settle down if there is a real dominating eigenvalue. end do: A := 1 3 K 3 1 K 1 1 0.44722 0.[-3.3]. V:=Vector([-1.85795 K 0.evalf(W/Norm(W. .87680 K 0.48083 K 0.1]).98388 .V. For example: O A:=Matrix(2. print(W.97636 K 0.5)).67977 . 70 .

I 1. I E := . To specify the derivative we must use the D notation. C 3.It never settles down since the eigenvalues are complex: O E:=evalf(Eigenvectors(A)). 0 < a < 2 (light damping).x(t)). K 3. a = 2 (critical damping) and a > 2 (heavy damping). 1. We explore the dsolve Maple function. d2 d deq := 2 x t C a x t Cx t =0 dt dt O deq:=D(D(x))(t)+a*D(x)(t)+x(t)=0. 1. For example: 71 . x t = _C1 e K 1 1 aC 2 2 a2 K 4 t K 1 1 aK 2 2 a2 K 4 t C _C2 e This general solution contains "arbitrary constants". We can use a variety of notations. Using dsolve O restart.D(x)(0)=q}. First we define a differential equation. I K 1. a = 0 (undamped).t)+x(t)=0. 1. O s:=dsolve({deq. deq := D 2 x t C a D x t C x t = 0 O deq:=(D@@2)(x)(t)+a*D(x)(t)+x(t)=0. 1 s := x t = 2 a pC a K4 pC2 q e a2 K 4 2 qCa pK a K4 p e a2 K 4 2 K 1 1 aK 2 2 a2 K 4 t 2 K 1 1 aC 2 2 a2 K 4 t boundary 1 K 2 We get different kinds of solution for a < 0 (negative damping).x(0)=p. We can get rid of them by specifying or initial conditions . deq := D 2 x t C a D x t C x t = 0 O dsolve(deq. We'll look at the equation of (say) a pendulum executing SHM with a damping term a. Note the form of the second derivative using the D notation. O deq:=diff(x(t). I 1.x(t)).t$2)+a*diff(x(t).

t t s := x t = eK C eK t To plot this we first have to make Maple think that x(t) is this solution.and even colour them differently (if we didn't want a paper copy!).t=0.) O assign(s).3.5. O A:=[-0.2.gold.1).0.6 0. p:=plot(x(t).20.3]: rb:=red.8..t=0.blue. x:='x': s:=dsolve({deq.x(t).. We do this with the assign function. t t eK C eK t 1 0.x(0)=1. plot(x(t).CRITICAL DAMPING (This is how a measuring instrument is damped so that the "needle" settles down to its final position as quickly as possible) O a:=2: s:=dsolve({deq.green..black: L:=[]: for r from 1 to 5 do a:=A[r]. 72 .-3. (Note that if we want to use x afterwards we'll have to unassign it.x(t)).D(x)(0)=0}.x(t)). L:=[op(L)..p].0. assign(s).2 0 0 1 2 3 4 t 5 6 7 8 Now we can plot the above five different cases -.1.0.4 0.D(x)(0)=0}. end do: plots[display](L).8 0.colour=rb[r]).x(0)=1.

Xcommutator. endomorphism_charpoly. matrixDE. adjoint . DFactorLCLM. newton_polygon . mult. MeijerGsols. line_int . kovacicsols . buildsol. matrix_riccati . eulersols. dfieldplot . muchange. DEplot. hyperode. DEplot_polygon . GCRD. LCLM. leftdivision . dalembertsol. DFactorsols. firint . convertsys. casesplit. liesol. firtest . dpolyform. moser_reduce. gen_exp. exactsol . eta_k. dsubs. hamilton_eqs. convertAlg . This is a numerical method which works even if one could not find an analytic solution to the equation. checkrank . Zeilberger. gensys. diffop2de . mutest. RiemannPsols. genhomosol. PDEchangecoords. equinv. generate_ic . bernoullisol . Dchangevar. chinisol. expsols. integrate_sols. 73 . DEnormal.3 2 1 0 5 K 1 10 t 15 20 K 2 K 3 DEtools One can do the same thing using DEplot function from DEtools. normalG2. caseplot. dperiodic_sols. exterior_power. eigenring. Xchange. initialdata . DFactor. formal_sol. de2diffop . infgen. DEplot3d. hypergeomsols. O with(DEtools). maxdimsystems. indicialeq . canoni. abelsol. intfactor . invariants. clairautsol . dcoeffs. buildsym. constcoeffsols . diff_table . FunctionDecomposition . autonomous. Xgauge. linearsol.

5 2 4 t 6 8 10 As usual there are lots of options that the Help will tell you about.thickness=1).x(t). poincare.10. separablesol. symmetric_product. phaseportrait.stepsize=0.[[x(0)=0. reduceOrder.2 2 4 t 6 8 10 74 .[[x(0)=1. symtest. rightdivision .D(x)(0)=1]]. rtaylor.6 x(t) 0.2 0 K 0. super_reduce. O DEplot(deq.. solve_group. odepde. varparam. symmetric_power. 0. zoom O x:='x': deq:=(D@@2)(x)(t)+0. regularsp. ratsols.1. deq := D 2 x t C 0. remove_RootOf. reduce_order. singularities . ode_y1. symgen. untranslate. redode.5 D x t C x t = 0 1 x(t) 0. linecolour=red.t=0. rifread. odeadvisor. DEplot(deq.5*D(x)(t)+x(t)=0. Some even let you specify a sensible colour! Here is the same equation with different boundary conditions. parametricsol. transinv..10. riccati_system .D(x)(0)=0]]).5 0 K 0.ode_int_y . translate. regular_parts. polysols. power_equivalent . rifsimp.4 0. riccatisol .t=0.x(t).

05.y(t)].y(0)=0].20. 75 .[x(t).colour=grey.20.linecolour=black.t=0.stepsize=0.D(y)(t)+0. 0.5 x 1 If you give more than one set of boundary conditions then Maple will do several curves.colour=red.red. D y t C 0.[x(0)=2.2 y K 0.2 K 0.[[x(0)=1.y(0)= 0].linecolour= [black..stepsize= 0.y(t)].[x(t). Maple will also show the direction of the "vector field" at every point..We can convert our equation to a system by putting the velocity D(x) = y and then we can plot the velocity against the displacement.y(0)=0]].05.blue]. O sys:=[y(t)=D(x)(t).t=0.[[x(0)=1.4 K 0.thickness=1). O DEplot(sys.[x(0)=3. sys := y t = D x t .5*y(t)+x(t)=0].y(0)=0]]. This is called a phase plane plot.5 y t C x t = 0 O DEplot(sys.thickness=1).6 0.5 0 K 0.

20.05) .stepsize=0.5.x=-5.y=-5.t=0.y(t)].[x(t). 4 y 2 K 4 K 2 0 K 2 K 4 2 x 4 Other methods 76 ..1 K 1 0 1 x 2 3 y K 1 K 2 If you don't give any boundary conditions you just get the vector-field and you have to specify ranges for x and y..5. O DEplot(sys..

x(t).x(t). s2:=dsolve({deq. d x t =K 1.. s1:=dsolve({deq. deq := D 2 x t K 0. deq := D 2 x t K 0. x t = K 0.5*D(x)(t)+x(t)=0..D(x)(0)=0}.5 D x t C x t = 0 s1 := x t = 1 K or numerical: O deq:=(D@@2)(x)(t)-0. end proc O s2(2).x(0)=1.987128948772179272.59019568061103554 dt 1 2 1 3 1 4 7 5 t K t C t C t C O t6 2 12 32 960 77 .x(0)=1.D(x)(0)=0}.5 D x t C x t = 0 s2 := proc x_rkf45 .series). t = 2.Maple can use a variety of methods to solve the equation.5*D(x)(t)+x(t)=0. including series: O deq:=(D@@2)(x)(t)-0..numeric).

6): for i from 1 to 6 do die().. 4. 176 . 1233154478 918311113976 We'll make a die to give a random number in the range 1 .2]:=R[a.end do. 149 78 . 183 . 154 . end do: R._seed.for testing for example) and then it uses some number theory to get the next random number you ask for and then uses this as the seed to get another one. Asking for rand().i=1.. O R:=[seq([i.. which sets it to something to do with the clock. _seed := 100 741966908562 741966908562 111069327352 111069327352 You can make the seed somewhat random with randomize(). 2. and so on. O _seed:=100. 4 4 6 5 6 5 Let's see how even its output is. O randomize(). b). produces a (big) random number directly while rand(a . produces a random number generator (a procedure) which you can then use to get random numbers in the range a . 1.0]. 5._seed. 177 ..rand(). Maple starts with a global variable _seed (which you can set if you want to produce the same sequence -. 3. O die:=rand(1. Maple generates (pseudo) random numbers with the function rand. 6.6)]: for i from 1 to 1000 do a:=die(). 161 . 6. b.rand().. R[a.Random numbers O restart.rand().2]+1.

11. 0 . end do. 5..2]+1. O plot(R). 0 . i = 1 . plot(R).a. 10. O rollem:=proc(n) local R. 3. 159 .0]. a. end proc. R := seq i. end do: return R. return R end proc R := 1. 682 . i. 562 . 582 . 2 := R a. 434 .. 863 . 2. 261 . R:=rollem(2). 6. R:=[seq([i.6 * n . 9. 140 end do. R[a. rollem := proc n local R.j.i. 4. 8. We end up with a triangular distribution . for j from 1 to n do a:=a+die(). for i to 5000 do a := 0. Note that the compressed vertical scale makes it look more uneven than it is. j.Then we can plot it. 649 . 2 C 1 79 . 7. 381 .2]:=R[a.6*n)]: for i from 1 to 5000 do a:=0. R a. 287 . 12. for j to n do a := a C die end do. 180 170 160 150 1 2 3 4 5 6 Let's do something similar for the sum of two (or more) dice.i=1.

17. 0 . 0 . 172 . 343 . 14. 5 . 7. 282 . 38. 39. 246 . 22 . 5. 31. 9. 0 . 0 . 36. 2. 0 . 0 400 300 200 100 0 10 20 30 40 80 . 85 . 404 . 81 . 1 . 0 . 32. 4. 0 . 187 . 9 . 3. 118 . 37 . 319 . 0 . 18. 5 . 433 . 0 . 37. plot(R). 41. 0 . 12. 26. O R:=rollem(7). 421 . 19. 24. 16. 282 . 25. 35. 10. 152 . 0 . 29 . 34. 15. 369 . R := 1. 152 . 43 . 29.800 700 600 500 400 300 200 100 0 2 4 6 8 10 12 The Central Limit Theorem says that the more dice you take the closer the distribution gets to a Normal distribution. 20. 8. 42. 0 . 33. 27. 402 . 30. 28. 23. 40. 323 . 4 . 48 . 6. 21. 26 . 22. 13. 11.

. i = 1 . end proc. shuffle := proc n local res. deck 81 .r().. spin := proc n local r. res:=[]. end do.. choice . Note how we remove an element from a list using subsop. (There are other ways of doing it.end.deck).i.. deck choice . end do. r := rand 1 .choice. for i to n do choice := spin n C 1 K i . res:=[op(res).Shuffling We make a procedure which produces a number in the range 1 . deck. return res. We start with a "deck" 1 . deck := subsop choice = NULL. O spin:=proc(n) local r. n without having to make a separate procedure for each n. deck := seq i.deck[choice]]. deck:=[seq(i.deck. r:=rand(1. r end proc O for i from 1 to 10 do spin(2).n)]..n . res := op res .n). res := .. 1 1 2 1 1 1 2 2 2 1 Now we'll shuffle (say) a pack of cards. deck:=subsop(choice=NULL. i. for i from 1 to n do choice:=spin(n+1-i).i=1.n . n in order and remove cards at random to put into our shuffled set (which we'll call res (for result)).) O shuffle:=proc(n) local res.

44. 18.n="JOHN O'CONNOR")]. "N". 27.-2]). 32. 42. 33. 52. 44. P := "J". 35. 1. 23.P[-1]. 46. return [op(res[1. 10. "N". end proc. 6. 30. n := nops P . "H". "O". 22. 47. "'". 31. 6. 41. 21. 43. " ". 7. "C". 5. "R" 82 . 37. 11. 39. shuffle0 := proc P local n. 48. n:=nops(P).1 has been shuffled and then insert the last card at random. 37. 4. 27. 45. res:=shuffle0(P[1.. 12. 3. choice:=spin(n). 17. "O". 7.choice K 1 .4. 20.res. "O". 31.choice-1]).end do. 28. 10.op(res[choice. 36. 30. 24. 49. 16. 5. 51. if n = 1 then return P end if.-1])]. 18. return res end proc O shuffle(5). 50. 17. 23. 19. return op res 1 . 22. "O". res := shuffle0 P 1 . 40. 52. 35. 9. 26. 13. 24. 1. choice := spin n . 12. 36. 40.choice. 46. 15. 9. 29. 45. 1. We assume a pack of size n . 3. 49. 25. 4.5]). 26. 50. 2. We'll apply it to a list P. 13. op res choice . K 1 end proc O shuffle0([1. 34 We can shuffle other things too: O P:=[seq(n. 25. 20. 2.. 4 O shuffle0([seq(n. 41. 28. 14. if n=1 then return P. 5. 8. choice . res. 2.recursively. K 2 . 4. 33. O shuffle0:=proc(P) local n. P K 1 . 38 We can shuffle in a different way -. 5. 47. 14. 8..n=1.3.. 11. 16.52)]). 2. 29. 15. "N". 34. 48. 21.2. 32. 3. 19. 3... 1 O shuffle(52).. 51. 43. 42. 38. 39. end if.

"R" 83 . "J". " ". "O".O shuffle0(P). "O". "C". "H". "O". "N". "O". "N". "N". "'".

for i to 9 do ans:= ans minus {M[r.7. end do. if M[i.Matrix(3).end do.ans. ans:= ans minus {M[i.j). end do.n]:=` `.O An example of recursion: Sudoku As most people know.j]:=M[i. for i from 3*r1+1 to 3*r1+3 do for j from 3*c1+1 to 3*c1+3 do ans:= ans minus {M[i. end if. local i.A.j.j]}.c]} end do.nextchoice. for j from 0 to 2 do for m from 1 to 3 do for n from 1 to 3 do if M[3*i+m.2.j.10]. end do.ch. We write a procedure to print the matrix split up into its 9 sub-squares.n]:=M[3*i+m. nextone:=[0.j]=0 then holes:=holes+1.c1.end if. end do. c1:= iquo(c-1.6.Matrix(3)]. This is where recursion is used.3*j+n].holes. end do.0.end do. ans := {1.3).j]. for i from 1 to 9 do for j from 1 to 9 do N[i.5. ch:=choice(M. We repeat the process until either we have finished or we are stuck.i]}.i. In the latter case we go back and try the next possibility. 84 . O restart.3. end if. If we have not. O checkit := proc(M) global gotit. r. if not gotit then holes:=0.A[3]). for i from 0 to 2 do A:=[Matrix(3).soln. r1:= iquo(r-1. Sudoku attempts to fill in a 9 by 9 Latin square (invented by Euler: no equal entries in rows or columns) with the additional condition that no pair in each of the nine 3 by 3 subsquares are equal. O pprint := proc(M) local i.k. end proc: We calculate which numbers can be put into a given "hole" in the matrix.9}.j.A[2].3).m. end do.r1. else A[j+1][m. we look for the hole with the fewest number of possibilities and try one of these. return ans end proc: We check to see if we have filled in all the "holes". if nops(ch)<nextone[3] then nextone:=[i.n. O choice := proc(M.j. N:=Matrix(9).8.4.nextone.N. print(A[1].nops(ch)].3*j+n]=0 then A[j+1][m. c) local i.

[0.0.0].0.0].2.3.2.0].4].6.8.0.0. for k in nextchoice do N[nextone[1].3.5.0.6. end proc: We feed in a matrix with 0's for the spaces.0.0.0] ]): The computers in the microlab take less than a second to find the solution! O pprint(M).0.1].0.1.7. O M:=Matrix(9.8. !!! Recursion! end do.0.5.3.2. end if.9.6.9. 85 .0.0. [0.0].end if.0].0.if holes=0 then gotit:=true.0.1.0. [2.0.0.0.0.9.0.5].6. gotit:=false: checkit(M): if gotit then pprint(soln).nextone[2]).3.0.0. checkit(N).0.8.7.0.0.0.9. [ [0. else print(`NO SOLUTION`). [1.4.0.0.nextone[1].nextone[2]]:=k.0.0. [7.0.print(` `). [0.7.0. [0. soln:=M. [0.0. elif nextone[3]<>10 then nextchoice:=choice(M. end if.0.0.0.0.

5 6 8 1 5 8 7 2 9 3 6 4 9 8 6 4 7 1 5 3 2 8 4 5 6 9 3 2 1 7 You may change one of the entries in the original matrix and verify that no solution exists. 1 8 9 9 3 2 4 1 7 . Countdown: another example of recursion This implements the procedure of a well-known TV show. `` 8 `` 9 `` `` `` `` 7 . 86 . `` `` 5 `` 2 `` 6 `` 3 . 1 9 2 3 1 5 2 8 9 . 6 4 7 7 6 1 5 2 8 . It stops when it reaches lists of length 1 or when it finds the particular integer it was aiming at and and then it shows how it may be obtained. 75 or 100) and 5 small (1 . The procedure randy generates some large and some small integers and chooses a number (global variable: aim) to aim for.2 and so on. `` 6 `` `` 1 `` `` 7 `` `` `` 6 4 9 `` `` `` `` 1 `` 3 `` 8 `` 5 `` 9 `` 2 `` `` `` `` 8 . `` `` 7 7 6 `` `` 9 `` 3 `` `` . It stores the various intermediate steps in the variable record If it could only get close it prints out how it got there from the variable temprec. `` `` 4 4 7 3 8 5 6 .`` `` 3 `` 5 `` . 50. If it is not possible it gets as near as it can. The procedure carol (named for the previous expert!) is applied to a list of integers of length n (usually 6 -. -.. * and / to produce all the possible lists of length n . 8 7 5 7 2 4 6 5 3 .1 and then (recursively) all the lists of length n . 9 3 4 2 9 6 3 4 1 .often with one large (25. 1 `` 2 `` 1 `` 2 `` `` . 10) ) and runs through all the combinations of these with +.

top:=subsop(k=NULL. carol(v0). others:=subsop(k=NULL. v0:=subsop(i=w.b.10.closest..4. for i from 1 to n do record[i]:=[]. n:=nops(v).v).j=NULL. end proc: O carol:=proc(v) global aim. O spin:=proc(n) local r.others). od.9. record[n]:=[v.6.end proc: randy:=proc(big.100]. od.` --> `.`+`.a.a. for i from 1 to big do k:=spin(nops(top)).7.choice.6. r:=rand(1.2. if not gotit and a>1 and b>1 then !! products w:=a*b. record[n]:=[v. !!! run through all pairs of numbers recursion! end if.small) local top. local i. others:=[1. count:=count+1.75.3.9.7.5. global aim.8. choice:=[].2.w]. v0:=subsop(i=w.others.v0. 87 sums .others[k]].`*`.r(). else for i from 1 to n do if abs(v[i]-aim)<abs(closest-aim) then closest:=v[i].a.count.j.` --> `.w].3. aim:=99+spin(900).gotit.b. if not gotit then !!! w:=a+b.b:=v[j]. carol(v0).n. end if.v).w.i.top).top:=[25.temprec. for i from 2 to n do ! for j from 1 to i-1 do a:=v[i]. end do.k.1. choice:=[op(choice).10].4. choice:=[op(choice).b. for i from 1 to small do k:=spin(nops(others)).8. end if. if member(aim.temprec:=record.n).50.record.O restart.v) then ! we found it! gotit:=true.end do.5.j=NULL.top[k]]. end if. return choice.

differences quotients if gotit then print(`Got it in `. 4 . 2.`/`. record[n]:=[v.count.[]. 10.` secs after `. record[n]:=[v.w]. end if. 2. 8. 343320.5). for i from 6 to 2 by -1 do print(record[i]). carol(v0). elif b<a and b>1 and a mod b=0 then w:=a/b.` secs after `. end proc: To make sure we don't get the same numbers every time! O randomize(): O choice:=randy(1. `C`. if not gotit then !!! if b>=a and a>1 and b mod a=0 then w:=b/a.843. 7. 10.a.b. 3. 10 75.` --> `. 5. else print(`Couldn't get it in `. end if: choice := 75.` --> `.a. 2. calculations 75.w].b. 8. print(`Closest was `.[]. carol(v0).`/`.a.` calculations`). for i from 6 to 2 by -1 do print(temprec[i]).[].a. 5. 50 88 .[].`-`.j=NULL.` --> `.end do. v0:=subsop(i=w. end if.`-`. end do.v). record[n]:=[v. 5.if not gotit then !!! if a>b then w:=a-b. 7. if a<b then w:=b-a.v). secs after . 4 aim := 879 Got it in . carol(v0).b. end if. record[n]:=[v. !! start:=time(): record:=[[].w]. end if. 5.[]]: closest:=0:gotit:=false:count:=0: carol(choice). count.time()-start.w]. --O .` --> `. `*`. aim:=aim. 7.j=NULL. v0:=subsop(i=w.` calculations`). 8.b. end do.time()-start. v0:=subsop(i=w. end if.j=NULL.v).j=NULL.end do. 4 . v0:=subsop(i=w.v). carol(v0).closest). --O .

125.6. print(`Closest was `.end do. `*`. 4 . --O .7].` calculations`). start:=time():record:=[[].end do.[]. 125 7. 4 . else print(`Couldn't get it in `. --O . for i from 6 to 2 by -1 do print(temprec[i]). end if: 89 .closest).w. 125. 7. 4.[]. 879 Test a particular case to see if it's possible O aim:=820.5. L:=[50.` secs after `.` secs after `.` calculations`). 50.[].[]. for i from 6 to 2 by -1 do print(record[i]). 875. `C`.75.w. 4 . 50.count.count. `C`.1.7. 75. --O . w:=time()-start: if gotit then print(`Got it in `. 875 875. 7.[]]: closest:=0:gotit:=false:count:=0: carol(L).

17 . 5 17 . 100 Let's see the factorisation of all these numbers. 80. `C`. --O . 65. 7. 97 . 90 . 5. 73. end do. 20. 40. 5 41 . Maple will let you apply a function to every member of a list or set. 2 . 2 17 . 5. 7 . `C`. 16. 819 50. 58. The first proof was given by Euler in 1749. --O . 41. `C`. 37. 2 6. 3 13 . 819 Sums of two squares Asking which integers can be written as a sum of one or two squares goes back to Diophantus of Alexandria. 2 2 37 . calculations Closest was . 6. 57. end if. `*`. 2 2 5 . 8. 2 4. O n:=100: S:={}: for i from 1 to 10 do for j from 0 to i do c:=i^2+j^2. 2 2 2 13 . 57 57. 64. 7 . 7. 34. --O . 2 5 . 7 . if c<=n then S:=S union {c}. 4. 13 . 1. 7 2. 89 . 41 . 26. 7 Couldn't get it in . 37 . 1. 81. 5. 6. 2 4 29 .ifactor(S[i])]. 13. 7 2. 6. 13. 10. 29 . 668154. 72. 1. 7. 63. --O . 17. 3 4.end do: S. 18. 36. 2 3. The answer was given by the Dutch mathematician Albert Girard in 1625 and by Fermat a little later. 50. 61. 7. 25. 2 5 2. 2 17 . 6. 2 2 5 . 2. 2 2 5 . 53. 13 . 7 . --O . 32. 2 5. 5. 45. `C`. 3 13 . 2 3 2. 68. 2 2. O T:=[]: for i from 1 to nops(S) do T:=[op(T). 89.end do: T. 52. 85. 2 2 5 . 50. 1. 73 . 1. 6. secs after . 98. 63 63. 7. 29.aim := 820 L := 50. 2 3 5 . 9. 13 63. 2 5 2 More easily. 5. 61 . 74. 5 . 53 . 5 2. 62. 62 62. 90. 2 2 3 2. 1. 2 3 2 3 2. 6. 1. 49. 82. 97. 3 2.

1. 2 5 . 1. 0. 5 17 . 0. 2 2. 1. 0. 0. 1. 1. 1. 1. 0. 0. 0. 2 3 2. 1. 89 . 2 5 2 5 . 0. 2 2 4 5 . 0. 1. 0. 2. 5 17 . 0. 1. 0. 1. 0. 1. 1. 1.isprime)). 0. 0. 17 . 1. 0. 0. In fact the numbers which can be written as a sum of two squares are those in which all the primes of the form 4k + 3 are present as even powers in the factorisation. 0. 53 . 2 5 . The set S above is "multiplicatively closed". end do. 0. 37 . 0. 2 17 . 0. 0. 0. 2 4. 0. 0. 2 2.list). 0. One meets it as the set of norms of Gaussian integers. To store more use a "static" array (whose size stays fixed). 1. 0. 2. 0. 1 The only primes in this list are of the form 2 or 4 k + 1. 0. 2. 0. 0. 1. 1. 1. 3 5 . 1. 1. 0. 2 2 5 . 0. 5 . 2 5 . 0. 1. 0. 2 3 2 3 2. 1. 0. 2. 5 13 . 61 . 29 . 0.O ifactor(S). 7 2. 0. 3 3 2 5 . 1. 3 2. 0. 0. 0. 0. 0. 1. 0. 73 . 0. 0. 0. 2 . 2 5 . 1. 29 . 1. 1. 97 . 0. 1. 0. 1. 3 4. 2. 2 2 2 41 . 0. 2 2 7 2. 2. 1. 0. 2. 5 3 2. 0. 1. 2 41 . 0. 0. 0. 3 7 2. 0. 53 . 0. 1. 0. 1. 0. 89 . 0. O ifactor(sort(convert(S. 2 5 . 0. 0. 1. 0. 0. 0. 2 4 17 . 2 5. 0. 2 2 3 2.n. 0. 0. 0. 2 . 1. 2. 2 41 . 2 13 . end do: print(S). 1. 1. 0. 2 13 . 3 13 . 2 3. Maple won't let you use a "dynamic" list of size bigger than 100. 13 . 0. 2 6. 2 4. 1. 1. 0. 0. 13 . 0. 17 . 1. 91 . 61 . 1. 0. 2 2 5 2. 3 4. 0. 2 6. 0. 1. 2 29 . 1. 0. 0. 1. 0. 2 2 2 2 2 2 2 5 2. 0. 0. 2 29 . 0. 0. if c<=n then S[c]:=S[c]+1.[0$n]): t:=evalf(sqrt(n)): for i from 1 to t do for j from 0 to i do c:=i^2+j^2. 0. 2 17 . 0. 73 . 0. 0. 0. Number of different ways Let us investigate the number of different ways a number can be written as a sum of two squares. 1. 0. 1. 0. O n:=200: S:=array(1. 1. 0. 0. 37 . 2 3 2 5 . 0. 2 3. 0. 0. 0. 0. 0. 0. 1. 2 5 . 5 . 1. 37 . 97 . 1. 1. 1. 0. 2 37 . 1. 1. 1. 2 2 3 2.end if. 2. 0. 0. 41 . 1. 2 3 2. 2 2 5 2. 0. 1. 3 2. 2 5 2. 0. 2. 0. 1. 2 17 . 3 13 .. 1. 5 2. 7 2. 0. 1. 2 5. 13 .

O n:=1000: S:=array(1. 0. 2 O for i from 1 to n do if S[i]>1 then print(ifactor(i)). 0.n. 0. 1. end do: for i from 1 to n do if S[i]>2 then print(ifactor(i)). 0. 0.except for 53. end if. 0.[0$n]): t:=evalf(sqrt(n)): for i from 1 to t do for j from 0 to i do c:=i^2+j^2. if c<=n then S[c]:=S[c]+1. end do: 5 2 13 5 2 17 4 2 5 2 5 5 5 2 13 29 13 2 92 . 1. 0. 1. 0. 0. 1. end do. 2. Repeat this (with a bigger n) to get an estimate for when one can write a number as a sum of squares in 3 ways.end if.1. 1. 0. end do: 5 2 2 5 5 2 2 5 13 17 5 3 2 2 5 2 5 13 2 5 2 3 5 2 13 29 5 17 37 5 2 And so it appears that the elements which can be written as a product in two ways all have two factors of the form 4k + 1 -. 0. 0. 0. 0. 1. end if..

6 5 4 The actual result is quite complicated. Subtract this from the number. In particular.S[2*5^2*13^2].2 5 5 2 2 17 37 O n:=50000: S:=array(1.end if.S[2^2*5*13^3]. Continued fractions Here is one way of thinking about Continued fractions : You can regard calculating the decimal expansion of a (positive) real number as the result of implementing the algorithm: (*) Make a note of the integer part of the number. end do: O S[5^3*13^2].1).. (rk C 1) and The number of factors of 2 or of the form 4 k + 3 make no difference to the number of ways one can write it as a sum of squares. if we put in: ** Take the reciprocal 1/ x of x ** then the sequence of integers we get is the Continued fraction expansion of x. if c<=n then S[c]:=S[c]+1. pkk then put B = r1 C 1 the number of ways is B/2 if B is even. If all the primes of the form 4 k + 3 in n present as even powers then one can write it as a sum of one or two squares. Now repeat the loop from (*).. ** by anything else that makes x bigger. We can replace the step at ** .. If the primes of the form 4 k + 1 in n are p11 p22. If x $ 0 then: ** Multiply x by 10 ** This (perhaps) gives a number # 1..n. otherwise it is ( B C 1)/2 r r r r2 C 1 . 93 ...[0$n]): t:=evalf(sqrt(n)): for i from 1 to t do for j from 0 to i do c:=i^2+j^2.. end do. This gives a number x in the range [0.

1. 15. ans := s := r. 1. 1. or reassign Digits. O cf:=proc(r. 3. 12. 1. 1. 1. 1. 1. 28. 2. 1. 4. 6. 1. 1. 4. 1.s. 1. ans:=[].n) local ans. 6. 13. 1. O Digits:=50: cf(evalf(Pi). 99. 15. 3. 1. 2. 10. 3. 24. 1.We'll use Maple to calculate the first n terms in this expansion. cf := proc r. 1. 1. if s!O 0 then s := 1 / s end if end do. 1. 2. 1. 0 . 1. if s<>0 then s:=1/s. 26. 1. 1. 18. s:=s-floor(s). 1. 32. 1. s. 30. If we want it to work to 20 significant figures. 1. for i from 1 to n do ans:=[op(ans). ans end proc The continued fraction expansion of a rational number (a fraction) vanishes after a certain number of steps.s:=r. 3. 1. 1. for i to n do ans := op ans . 2. 22. i. 1. 1. 1.floor(s)]. 189. 1. 1. n local ans. 0. For irrational numbers we need to work with a decimal expansion and then we must tell Maple what accuracy to work to. 1 Continued fractions of square roots of integers repeat themselves: 94 . 1. 1.40). (Note the capital letter!) It will stay at that until you restart.50). 2. 1. 1. 1. 2. 1. 6. 84. 1. To round down a real number x Maple uses floor(x). end proc. 1. 2. 8. 292. 2. 6. 5 O cf(evalf(exp(1)). 1. ans. floor s . 6. O cf(12346/56789. 16. 14. 1.end if. s := s K floor s . 2. 2. we enter: Digits:=20. 14. 20. 1. 1. 1. end do. 1. 2. 2. 2. 1.10).i. 4. 7. 1.

if x !O 0 then x := 1 / x end if end do.2. 4. 2. 4. 0 . 0. 1. 1. 4.40). 1. 1. 1. 1. 1. 2. 20.3]). 0 O r:=1234/6789. 4. 2. 1. 5 r := 225 157 1. build(C). 0. 1.end if. for i to nops C do x := x C C K i . end do. 1 We can reverse the above process to get a (rational) number from a (finite) continued fraction. 1. build := proc C local x. 4. 5. 4. 1. 4. C:=cf(r. i. 1. cf(r. 3. 10. 1. 2 and so does the continued fraction of anything of the form aC b with a. 20. 2. 1. 1. 1. c O r:=evalf((sqrt(5)+1)/2). 1. 1. 4.4. 153. r := 1. return 1/x. 0. 1. 1. r:=build(C).3. 1. 4. 1. 1. 1.10). 1.i. 4. r := 1234 6789 95 C := 0. 1. 1. 2. 4. cf(r.O cf(sqrt(117). 5. c integers. 1. 1. 1. 1. 1. 1.5]. 1. 87 80 O C:=[1. 1. 1. return 1 / x end proc O build([1. C := 1. x := 0. 20. 20. 2. 1. 1.8).30). 3. 4.11. O build:=proc(C) local x. 1.2. for i from 1 to nops(C) do x:=x+C[-i]. 4. 1. 3. 20. b. 1. 1. x:=0. 0. 4. 2. 4. end proc. if x<>0 then x:=1/x. 1. 2. 20. 1.6180339887498948482045868343656381177203091798058 1.

1]). 3.5). 1.14285714286. 10.12). L := 3. 22. cf(build([1. 3 5 8 13 21 34 55 89 144 233 377 610 1. 0 3. 3. 1. . 1. 1.1]).2. 16.1. 3. .3.build(C[1.5). 2.12). 3. 2. 18. 2. 3. 1.1.i end do.confrac. build C 1 . 8. . L:=[]. 7 106 113 33102 33215 66317 99532 265381 364913 O evalf(L.4. 6. 1. . for i from 1 to nops(C) do L:=[op(L). 0 Maple has "built-in" continued fractions which you can find out about with: ?confrac.4. .2. . 1.14159265358.1. 1.37). 1. 1.14159265359 3. 14. 3.14159265359 Note that there is an ambiguity for continued fraction expansions ending in 1 or with 0 in the list.14150943396. 292.14159292035. 1. 3.. 96 . L:=buildall(C).1. O cf(build([5. 4. 7. 4. 1. evalf(Pi.1.1. 1 22 333 355 103993 104348 208341 312689 833719 1146408 . . O buildall:=proc(C) local L.5]). for i to nops C do L := op L . 1. return L C := 3. 5. 2 3 5 8 13 21 34 55 89 144 233 377 Note the Fibonacci numbers in the numerators and denominators. 15. 3. 3. 1. L := end proc O buildall([1. 12.0. . . . buildall := proc C local L. 4. 1. 1.1.14159265392. . 1. . . . end do.1.14159265362.10). You can then explore some of the discoveries of (among others) Leonhard Euler: O convert(exp(1). 3. . end proc. .14159265347. 1.1.1234 6789 Continued fractions give a (good) way of approximating reals by rationals.1. 3.3. 1.1. 5.1. 1. 3. 2. return L.i. . 3. 1. 1. 1. i. . 20.i])].. 1. O C:=cf(Pi. . .14159265301.. 1.

confrac. . You access the functions in the package using: O restart. RegularPolygon .29). and investigate their properties. MajorAxis. 2. 5 22 49 218 485 2158 4801 21362 47525 . OnSegment. RadicalCenter . ParallelLine . 180 The numbers which occur in certain of the convergents of the (periodic) continued fractions of square roots can be used to solve Pell's equation. 68. 37. 22. 97 . altitude . ArePerpendicular . NagelPoint . . 46.28). GergonnePoint. AreConcyclic . 90. 198) or . IsRightTriangle . Pole. 86. ExternalBisector . IsOnCircle. 11. VerticalCoord . InteriorAngle . 2. AreTangent. 0. O convert(sqrt(6). 52. Geometry package: Menelaus's theorem The geometry package allows you to work with a variety of geometric objects: point.. 36. 2. 60. 54. RadicalAxis . 43. 4. HorizontalCoord. 148. CrossRatio. TangentLine. 70. 2. AreConcurrent. 102.1. PerpenBisector . 6. . 19. 41. 24. 9. PedalTriangle . . 42. 18. DefinedAs. 17. 164. 55 O convert((exp(1)-1)/(exp(1)+1). 29. IsEquilateral . HorizontalName. 82. 1. 132. 2) or (49.confrac. EulerLine. MinorAxis. 53. circles. StretchRotation . 140. SpiralRotation . 172. 14. ExteriorAngle . FindAngle . SensedMagnitude . y) = (5. 38. 62. 39. 74. lines. . 5. StretchReflection . 1.24). 92. Polar. 15. 4.confrac. 51. 4. 20) or (485. Apollonius . 33. 44. 100. 20. with(geometry). 116. PerpendicularLine . .10.. CrossProduct. 94. 21. 98.cgt. AreCollinear . 2 9 20 89 198 881 1960 8721 19402 For example ( x. . Equation. EulerCircle . 49. AreParallel . AreSimilar. . 58. 12. 26. 3. 124. 2. 4. 25. CircleOfSimilitude . RegularStarPolygon . 0. 23. 45. 84. satisfy x2 K 6 y2 = 1. 30. 10. area. MakeSquare. 35. 0. 47. 31. 156. 106 O convert((exp(1/2)-1)/(exp(1/2)+1). 28. AreOrthogonal.'cgt'). GlideReflection . AreConjugate . 2 2. 7.confrac. 50. 13.. 2. IsOnLine. 4. AreHarmonic. VerticalName . 66. SimsonLine. apothem. . 27. 78. 1 O convert((exp(2)-1)/(exp(2)+1). 34. 76. 108.

asymptotes, bisector, center, centroid, circle , circumcircle , conic, convexhull , coordinates, detail , diagonal, diameter, dilatation , directrix , distance, draw, dsegment, ellipse , excircle , expansion, foci, focus, form, homology, homothety, hyperbola, incircle , inradius, intersection , inversion, line, medial, median, method, midpoint, orthocenter, parabola, perimeter, point, powerpc, projection , radius, randpoint, reciprocation , reflection , rotation, segment, sides, similitude , slope, square, stretch, tangentpc , translation, triangle, vertex , vertices It makes life easier if you tell Maple at the beginning how you are going to label your axes. O _EnvHorizontalName:=x;_EnvVerticalName:=y; _EnvHorizontalName := x _EnvVerticalName := y We'll investigate a theorem discovered by Menelaus of Alexandria in about 100AD. If a line meets the sides a, b, c of a triangle ABC in points D, E, F then the product of the ratios BD CE AF = -1. DC EA FB O point(A,0,0),point(B,1,0),point(C,0.6,0.7); ! Note the commas! A, B, C O line(a,[B,C]),line(b,[A,C]),line(c,[A,B]); a, b, c O line(m,y=-0.4*x+0.5); m

O intersection(D,a,m),intersection(E,b,m),intersection(F,c,m); D, E, F O a1:=SensedMagnitude(B,D);a2:=SensedMagnitude(D,C); b1:=SensedMagnitude(C,E);b2:=SensedMagnitude(E,A); c1:=SensedMagnitude(A,F);c2:=SensedMagnitude(F,B); a1 := K 0.1493010694 a2 := K 0.6569247054 b1 := K 0.4315531448 b2 := K 0.4904013010 c1 := 1.250000000 c2 := K 0.2500000000 O a1/a2*b1/b2*c1/c2; K 1.000000000

**O draw({a,b,c,m(colour=red),A,B,C,D,E,F},view=[-1..1.5,-0.5. .1],axes=none,printtext=true,colour=black);
**

98

C E A D B F

You can now repeat this with a different triangle and a different line.

Ceva's theorem

More than 1500 years later in 1678 the Italian mathematician Giovanni Ceva discovered a rather similar theorem. If points D, E, F lie on the sides a, b, c of a triangle ABC and the lines AD, BE, CF are concurrent BD CE AF then the product of the ratios = 1. DC EA FB O point(G,0.5,0.2); G

O line(d,[A,G]),line(e,[B,G]),line(f,[C,G]); d, e, f O intersection(D,a,d),intersection(E,b,e),intersection(F,c,f); D, E, F O a1:=SensedMagnitude(B,D);a2:=SensedMagnitude(D,C); b1:=SensedMagnitude(C,E);b2:=SensedMagnitude(E,A); c1:=SensedMagnitude(A,F);c2:=SensedMagnitude(F,B); a1 := K 0.3749887325 a2 := K 0.4312370424 b1 := K 0.5296334050 b2 := K 0.3923210407 c1 := 0.4600000000

99

c2 := 0.5400000000 O a1/a2*b1/b2*c1/c2; 0.9999999996

O draw({a,b,c,d(colour=red),e(colour=red),f(colour=red),A,B,C, D,E,F,G(colour=blue)},view=[-1..1.5,-0.5..1],axes=none, printtext=true,colour=black);

C

E G A F

D

B

Product of chords

O restart; with(geometry): _EnvHorizontalName:=x:_EnvVerticalName:=y: O randomize(): See the picture below: If AB and CD are chords of a circle meeting in E then: AE.EB = CE.ED O circle(cc,[point(A,0,0),point(B,1,0),point(C,0.6,0.7)], 'centername'=O); cc O randpoint(D,cc);

100

ch1.ch2(colour=red)].D]).E).view=[-0.3519143895] O line(ch1.C. O draw([cc(colour=COLOUR(RGB.E). E O a1:=distance(A.5.D. a1 := 0. -.0. 101 .4810111383 b1 := 0.B]).5)).3542632297 O evalf(a1*a2-b1*b2).4782617403.5.ch2).ch1 (colour=red).E). ch2 O intersection(E. 0. name of the object: D form of the object: point2d coordinates of the point: [.5189888617 a2 := 0.A.axes=none).E.B. ch1.D O detail(D).b2:=distance(D.E).1.5. b1:=distance(C.7046721255 b2 := 0.[C.0.a2:=distance(B...printtext=true. -0.[A.2.line(ch2.0.1].

D).C O A E B D Ptolemy's theorem If A.7502721188 102 . c1:=distance(A.B).058796651 b1 := 0. O a1:=distance(A.BD Discovered by Ptolemy (85 to 165AD) one of the foremost of the late Greek mathematicians and astronomers.C).b2:=distance(D. B.D).8062257748 b2 := 0. b1:=distance(B.9219544457 c2 := 0. a1 := 1 a2 := 1.DA = AC.c2:=distance(B.a2:=distance(C. C.CD + BC. We'll use the last calculation.A).C). D are four points on a circle then AB.4553066234 c1 := 0.

0):circle(cc.e.B. false In fact it's only rounding error O line(p. ! that stops them from appearing on the same line 6.D. Discovered by Blaise Pascal (1623 to 1662) when he was 16 years old.a.Q]): distance(R.O evalf(a1*a2+b1*b2-c1*c2).cc):randpoint(E.[F.[E. D are in this order around the circle! O evalf(-a1*a2+b1*b2+c1*c2).axes=none.cc):randpoint(B.A]): O intersection(P.D]):line(d.[C.Q.a.P.B]):line(b.f): O AreCollinear(P.7341598706 Unfortunately it only works if A.p(colour=blue.771136670 10-10 O draw([cc(colour=COLOUR(RGB.F.[D.0.0. O point(O.R).[P. C. 0.thickness=2)]. 10-10 Pascal's theorem Pascal's Magic Hexagram If a hexagram is inscribed in a circle then the meets of opposite sides are collinear.cc): O line(a.d.c.C]):line(c.C.R.[O.5)).colour=black).b.b.e):intersection(R.f.E]):line (e.5.cc): randpoint(D. 103 .1]): O randpoint(A.[B.Q.c. K 4.p).cc):randpoint(F.d):intersection(Q.0.F]):line(f. E.printtext= true.cc):randpoint(C. B.0.5.A.[A.

q). O point(A. We can assume that two vertices of our triangle are at given points and make the third variable. it will turn out to be not quite as easy as it seems. Maple then tags these variables with a ~.1. A.0). B.0).R F D A E O C B Q P The Euler line By using variables for coefficients of the points we construct. 104 . We add "assumptions" about the variables as we need them.point(B. O restart.with(geometry): _EnvHorizontalName:=x:_EnvVerticalName:=y: We'll investigate a result on triangle geometry due to Leonhard Euler (1707 to 1783). we can get Maple to prove geometric theorems rather than just look at special cases.point(C.p. C To get a triangle out of these we have to make sure they are not collinear or Maple won't handle them. Actually.0.

cc O orthocenter(H.[O.7: point(A.0).O assume(q>0).t). H G Euler proved that O. axes= none. O line(e. They lie on the Euler line.6:q:=0.t.C]): circumcircle(cc. true 2 O gh:=distance(G.H.e(colour=blue. O centroid(G.G].q): triangle(t. e To draw a picture we will have to take specific values for p and q and then define everything again O p:=0.t). t We can find the circumcentre O (the centre of the circle through the vertices).C]).G).printtext=true. To define the line we need another assumption O assume(p>1/2).G]): O draw([t(colour=red).G).H). the orthocentre H (the meet of the altitudes) and the centroid G (the meet of the medians) O circumcircle(cc. gh := 1 2 K p 3 3 2 C 1 p2 K p q~ C 3 q~ 2 og := 1 1 K p 6 3 1 K p2 K q~2 C p 1 C K K q~ 2 q~ 3 2 2 O simplify(gh/og). He also showed that |GH| = 2* |OG| O AreCollinear(O.centername=O): orthocenter(H.O. 105 .[A.t.1.p.0.[O.B.t): centroid(G.og:=distance(O.t): line(e. G.colour=black).point(B.[A. H are collinear. This completes the proof of the theorem.point(C.H.centername=O).G]).thickness=2). O triangle(t.0).B.

C

H G O

A B

**The nine-point circle
**

Now we'll look at another result that Euler could have discovered (but didn't!). It is due to Feuerbach (1800 to 1834). Karl

The midpoints of the sides of a triangle, the feet of the altitudes and the midpoints of the lines joining each vertex to the orthocentre lie on a circle. This is called the Nine-point circle . Its centre lies on the Euler line. O p:='p':q:='q': assume(q>0,p>1/2,(p-1)^2+q^2>0,p^2+q^2>0); O point(A,0,0),point(B,1,0),point(C,p,q); triangle(t,[A,B,C]): circumcircle(cc,t,centername=O): orthocenter(H,t): centroid(G,t): line(e,[O,G]):

106

A, B, C O midpoint(A1,B,C),midpoint(B1,A,C),midpoint(C1,B,A); A1, B1, C1 O altitude(aa,A,t,A2),altitude(bb,B,t,B2),altitude(cc,C,t,C2); aa, bb, cc O midpoint(A3,A,H),midpoint(B3,B,H),midpoint(C3,C,H); A3, B3, C3 O circle(npc,[A1,B1,C1],centername=N): O IsOnCircle([A1,B1,C1,A2,B2,C2,A3,B3,C3],npc); true O IsOnLine(N,e); This completes the proof of the theorem. To draw a picture we will have to take specific values for p and q and then define everything again O p:=0.7:q:=0.6: point(A,0,0),point(B,1,0),point(C,p,q); triangle(t,[A,B,C]): circumcircle(cc,t,centername=O): orthocenter(H,t): centroid(G,t): line(e,[O,G]): midpoint(A1,B,C),midpoint(B1,A,C),midpoint(C1,B,A); altitude(aa,A,t,A2),altitude(bb,B,t,B2),altitude(cc,C,t,C2); midpoint(A3,A,H),midpoint(B3,B,H),midpoint(C3,C,H); circle(npc,[A1,B1,C1],centername=N); A, B, C A1, B1, C1 aa, bb, cc A3, B3, C3 npc O draw([t(colour=red),e(colour=blue,thickness=2),O,H,G,A1,A2, A3,B1,B2,B3,C1,C2,C3,npc(colour=black),N,aa(colour=red),bb (colour=red),cc(colour=red)], axes=none,printtext=true, colour=black); true

107

C B2 C3 A2 H B1 A3 O C1 A C2 B G N B3 A1

108

A summary of Maple commands Elementary calculations. If it can Maple will produce exact answers containing fractions or square roots of fractions. Maple will do the calculation but won't print it out. factor will try to factorise a polynomial with integer (or fractions) as coefficients. Use ^ for to the power of and remember that the computer will stick strictly to the correct order when doing arithmetic. Use round brackets to alter this order if you want. If you want the answer as a decimal then use evalf(expression.. x. Use * and / for multiplication and division. – for addition and subtraction. sin. The coefficients of a polynomial can be obtained using coeff(p. arctan. To use them. but you must put a . arcsin. You can put more than one calculation on a line. and lots of other functions. manipulations. log = ln.. cos. The number ! lives in Maple as Pi. n) 109 . [number of figures]) which evaluates the expression as a floating point number. You can get Maple to calculate to higher accuracy with (say) Digits:=15. Maple has many other functions: For example.. You can move on to a new line (without calculating) using Shift-Return.. a ratio of polynomials or a trigonometric expression. expand(expr) will “multiply out” an arithmetic expression. tan. a polynomial expression. Maple will work like an ordinary calculator.. the function ifactor (for integer factorise) can be applied to an integer (or even a fraction) to write it as a product of prime factors and the function isprime tells you if an integer is a prime number or not. ! n. after each one. . x) will sort the terms of a polynomial and collect terms in xn together. sort(expr) and collect(expr. If you use a : instead. put a semi-colon after each calculation and then press the Return or Shift key. Use the usual symbols +.. Use % for the last expression that Maple calculated and %% for the one before.. log[10] = log10. put ( ) around what you evaluate. as well as exp. The function factorial (or n!) calculates 1 ! 2 ! 3 ! . Maple recognises the usual functions like sqrt. etc. Type in your sum. . Without the second argument you get the default (which is 10). .. .

by using one of the keyboard shortcuts Control-J or Control-K or click on the [> on the Tool bar. Lots of things. unassigns all variables. To put in comments when the cursor is at a Maple prompt > either use the Insert text item from the Insert menu. x$2). You can differentiate twice using diff(expr. You can evaluate it at a point a say. .. integration a:=1.x) or diff(expr. Note that the function f is different from the expression f(x) (which is the function evaluated at the “point” x). If the variable x already had something assigned to it before assigning it to a.. start a new Execution group (what Maple calls the group enclosed by a bracket at the left) by using the item Execution group in the Insert menu. You cannot differentiate (or integrate) with respect to a variable which has had something assigned to it. assigns the value 1 to the “store” or “variable” a. by f(a). . you can use this to insert some explanatory text. including numbers and expressions. or the keyboard shortcut Control-T or click on the T on the Tool bar.. Assignment. unassigns this variable — and makes it back into a proper variable again. y = b. x) differentiates the expression with respect to the variable x.. if you wish. a:='a'.. x. restart. f:= x -> expr assigns to the variable f the “rule” or “function” that maps x to the expression. Any other variables in the expression will be treated as constants when differentiating. If you store an unassigned variable x (or an expression containing x) in a variable a then altering x will also alter a. then a will not be affected by changing x. You can see the help files for any command by typing ?command or by highlighting the command and using the Help menu. b for y. differentiation. You can put in formulae using Control-R or the Standard Math Text item from the Insert menu When you have finished. You can use the same method to get a new execution group anywhere in your worksheet and then. diff(expr. 110 . can be stored in this way. in the expression. subs(x = a. expr) substitutes a for x.simplify(expr) will attempt to simplify an expression.

You can put other options inside the round brackets. y=c.The operator D can be applied to a function to get the differentiated function. t = 0. integrate (or plot) f without turning it into an expression by. for example.. for example f:= x->x^2.b... To plot a curve given parametrically by x = p(t).1). int(expr. [2. for values of x between a and b. If you leave out the range: plot(expr. 0. Int (capital letter) returns the formula for the integral. x = a.d) plots the two graphs on the same axes and uses the specified ranges for the (horizontal) x axis and the vertical axis. 2]. plot([expr1.d) or plot([expr1.. x = a.b) plots the graph of y = the expression.. Use evalf to get a numerical answer if you need one. t. coords = polar). [2.. diff(f(x).b.1). y = q(t) use plot([p(t). As in. x) then Maple takes it to be –10 ! x ! 10. 1]}. expr2].. c. 111 . Diff (with a capital letter) returns the formula for the derivative. but does not do the differentiation. x = a. int(expr.Pi]. style=point). In particular.b) calculates the definite integral of the expression from a to b. If a function has been assigned to f. Plotting plot(expr.. you cannot differentiate. You can even make a or b infinity or –infinity if you want. and can choose to join them up if you wish.. or use single quotes: plot('f(x)'. to plot a curve given in polar coordinates by r = f(!) use plot([f. You can plot individual points as (for example) plot({[1. To plot a function (or procedure) f do not mention the variable: plot(f. x)..10]). x) calculates the indefinite integral wrt x (if it can — integration can be difficult). q(t). 1]. x=0. expr2]. putting it in as (say) f(x). x = a. t = 0.

If you just want to see the floating point answer. and again other options can be put inside the round brackets.d}) will give the numerical answers. If you don’t put in style = point. q = 0. q).To plot points given in a list L of lists [x. etc.1 you do it with view = 0.. Plotting (a projection of) a surface in three dimensions given by an equation z = f(x. s[2]. z = z(p.. x) and then ask for s[1]. second. for example. q). You can animate a sequence of such plots by using the display function to show a list of plots and putting in insequence=true. x) tries to solve the equation for x.. x=a.b.. y) (an expression) for (say) 0 < x < 1 and 0 < y < 1 can be done with plot3d (f. You can solve simultaneous equations for (say) x and y using solve({eqn1.. eqn2}. eqn2}. y=c. The variable x had better be unassigned. y}) or fsolve({eqn1. 112 . . x). q) with (say) 0 < p < 1 and 0 < q < 1 use plot3d([x. style = point). eqn2}. you can use fsolve(equation. Similarly. z]... x = 0.1). {x.. {x=a. If you want to plot more than one expression then you must put the expressions in { } brackets.. to get the first. To get Maple to find solutions lying in a range a ! x ! b you can use fsolve(equation. y. y}).1. Q). If you want to make the range of z (say) 0. use plot(L. You can display several plots together by assigning the plots to variables P. p = 0.. you can assign them to a list by. Solving The command solve(equation.b). fsolve({eqn1. {x.. y]. As above there are lots of options you can put in. y = 0. If there are several solutions. s := solve(eqn.1). Q (say) and then using the display function from the plots package: plots[display](P. To plot a surface given parametically by x = x(p.1. Maple will join up the points with lines. y = y(p.1.

do and end do 100 times.. which increases the value of i by 3 end do. makes i take the values in the list Another form of loop is a while-loop. 10] do successively. if boolean expression then . end if. This has the form: while boolean expression do . end do. It will print the things you ask for (with commas between if there is more than one thing) with a new line each time it implements the print statement. each time it goes through the loop. end do... Examples of other forms of a for-loop are: for i from 1 to 100 by 3 do ... where the boolean expression evaluates to give either True or False. end do. finish the loop with a colon : You can then ask it to print some of what is going on using print(something. ` (to get these use the key at the top left of the keyboard).. . for i from 1 to 100 do .. If you want to include text.. it might continue for ever. to decrease i. You can use: for i from 10 to 1 by –1 do .... Maple will repeat the instructions between do and end do until the boolean expression becomes False. If clauses You can use a boolean expression to choose between alternatives using an ifclause. 2. end do. you have to put in back-quotes ` . If you make a mistake. 113 .. for i in [1. will make Maple do what is between then and end if provided the boolean expression evaluates to True. but you can stop it by clicking on Stop on the menu-bar. something else)..Looping You can make Maple repeat a calculation a given number of times using a forloop. 5. will go through the instructions between If you want to stop Maple printing everything it does..

elif another boolean expression then . else ... 20). They live inside { }. You can use a for-loop to sum a sequence. 20 in the expression. end fi. L[3 . 2. other lists. n=1. intersection and minus. 5].. The last element of a list is L[-1] and the one before is L[-2]. You can make a sequence with (say) seq(expression. L := [a. You can put in lots of other elif (= else if) bits if you want. You can get at an element of a list (if there is one) by asking for it by its index (or number): L[3]. b... It lives between [ ] and has commas between its elements. The elements can be numbers. You can sometimes get Maple to give the general formula for a sum and can sometimes even sum for n = 1 . c. end if. The empty list is [ ].. Sets and Summing A list is an ordered set of elements (or operands). else . Lists. infinity.Alternative forms are: if boolean expression then . n = 1 .. but unordered and with no repetitions. 3. produces a sequence of 10 zeros. d].. The sequence of elements of a list L is op(L) (which stands for the operands of L)..... n = 1 .10)]. For example [seq(0. .. .. 114 . but Maple has a special function to do this: sum(expression. A sequence is like the contents of a list (without the brackets). expressions. new elements]. Sets are like lists.. . You can assign to elements of a list in the same way as you can to any variable: L[3] := 4.. variables. You can sort a list L with sort(L).. 20) sums the terms obtained by substituting n = 1. You can deal with them using the operators union.. The number of elements in a list L is nops(L) (which stands for number of operands). etc. and make this into a list by putting it between [ ]. or the more elaborate: if boolean expression then .. You can add to the elements of a list using L := [op(L).

f := x –> x^2. these can be declared as local variables and will not then affect the values of variables used outside the procedure which happen to have the same names. 1).Procedures A procedure can be used to define more complicated functions than the ones given using an assignment f := x –> a formula involving x. Maple will rather crossly tell you that it has done the job for you. ex]. say) then you can enclose things in single quotes. immediately before end proc. For example. The last expression in the procedure. the values ouside will be altered. Even if there aren't any such parameters to pass. If you do need to mention the variable (for example if you want to simultaneously plot a function given by an expression ex. you still have to put in the brackets! If the procedure involves a complicated calculation with assignment to extra variables. To plot a function defined by a procedure. You cannot use the parameters that are passed to the procedure as variables which can be assigned to. is the value that the procedure returns. plot(['fn(x)'. If you don’t declare them as local variables.. is really shorthand for f := proc(x) x^2. -1 . For example to plot a function defined by a procedure fn: plot(fn. The parameters inside the brackets are described as being "passed to the procedure".1). do not mention the variable. If you do want the procedure to access variables you have already used. and if you change them inside the procedure. 115 . x = 0 . end proc. you can declare them as global..

Note that all the commands in this package have capital letters.C but not (in general) commutative A. A:=Matrix(2. j]). There is something called the matrix palette (on the View menu) which can help.The Linear Algebra package To use the commands in the LinearAlgebra package enter with(LinearAlgebra): (If you put a .A.B) = det(A). produces a square matrix with rows taken from the list of lists in the second parameter. You can make a matrix of (say) 3 rows by A := < <a | b | c> . Maple calculates the determinant of a (square) matrix with Determinant(M). you get a list of all the available commands. [d. Multiplication of square matrices is associative: A. c> | <d .(i. [[a. You can multiply a matrix by a constant (or a variable) by a*M. f> | <g . d]]). e. b. <g | h | i> >.B). or of 3 columns by B := < <a . makes a matrix with 2 rows and 3 columns. or by using (say) C := Matrix(3. b].(B + C) = A. 3. e .B. 5 . If you don't tell it what the entries are it fills up with zeros. h . [[a.C and distributive A. You can multiply together matrices of compatible shapes by A. You can even get a matrix with "undefined" entries by X := Matrix(3.) If you ever use restart. 116 . i> >. The LinearAlgebra package lets you work with Matrices (and Vectors). and you can take powers of matrices by (for example) A^3.det(B). or A^(-1). B:=Matrix(2. f]]).(B. <d | e | f> . (i. b . You can make a matrix whose entries are given by some formula by using a double for-loop. It satisfies det(A. c]. 3.B " B.C) = (A.B + A. you will have to read the package in again. [c. j) -> x[i. You can add and subtract matrices of the same shape. (giving the inverse of the matrix). j) -> i+2*j).

D(x)(0)=1]]).. before you generate a sequence of random numbers otherwise you will get the same sequence each time. D(x)(1)=1}. x(t).Differential equations You can use the dsolve function to find an analytic solution to a differential d 2x equation (say) 2 + x = 0 with: dt dsolve((D@@2)(x)(t) + x(t). Entering roll:=rand(2 . If you want to use y as a variable again afterwards. You can use the DEplot command from the DEtools package to plot the solutions of differential equations numerically: DEtools[DEplot]((D@@2)(x)(t)+x(t). x(0)=0. [[x(0)=0. If you have found a solution s as above and you want to plot (say) y as an expression in x you can use assign(s). remember to unassign it with y := 'y'. You ! must use the D operator if you specify boundary conditions for derivatives. t=0. The function DEplot in the DEtools package can be used to plot numerical solutions of single differential equations or of systems of equations. Maple will do the calculation. to make y into this expression. 117 . 5).. x(t)). With boundary conditions: dsolve({(D@@2)(x)(t)+x(t). Randomisation Maple generates (pseudo) random numbers with the function rand. If you want individual solution curves then you specify initial conditions as a list of lists: one list for each curve. creates a random number generator roll( ) which produces random integers between 2 and 5 each time it is called. There are lots of options you can include. Recursion A Maple procedure may call itself (hopefully with different values of its parameters!) and provided this process eventually gives a definite answer.10. Use randomize(). There are many options you can use to (for example) specify the method you want to use to solve the equation.x(t)). It is sometimes helpful to include option = remember at the beginning of the body of the procedure.

Do this with the assume( ) command. specifying its equation. For example.The Geometry package The Geometry package lets you create and manipulate several kinds of geometric object. To use it enter with(geometry): and if you want to label your axes x and y then enter: _EnvHorizontalName:=x: _EnvVerticalName:=y: Create a point A with (say) point(A. Among the more useful options you can specify are printtext = true (which labels any points you draw) and axes = none. Many other constructions are available to save you having to make them all yourself. B. radius 1). 0). 118 .C]). Maple will then put a little ~ symbol on anything which relies on the assumptions. To persuade Maple to let you do this it may be necessary to make assumptions about some variables (to ensure. you can make triangle(t. B. [A.B. [A. for example that two points are distinct so you can join them with a line). etc. 1]). Options allow you to label objects.B]). 0. [A. C]). By using variables rather than constants as coordinates you can use Maple to prove general results rather than just special cases. change colours. and circle(c. [X. y=2*x+5). (through A. and a line a from (already created) points A to B with line(a. or line(b. The draw command will let you draw a list or set of constructed objects (which had better not depend on any variables). C) or circle(cc. (centre X. You can find out about any object you have defined using detail.

England Pafnuty Chebyshev 1821—1894 Born: Okatovo. France .Pictures of some of the mathematicians whose work was encountered during the course Archimedes of Syracuse 287BC—212BC Born: Syracuse. Russia 119 Girard Desargues 1591—1661 Born: Lyon. Sicily Arthur Cayley 1821—1895 Born: Richmond.

France 120 . Libya Euclid of Alexandria about 325BC—about 265BC Leonhard Euler 1707—1783 Born: Basel.Eratosthenes 276BC—194BC Born: Shahhat. Switzerland Pierre de Fermat 1601—1665 Born: Beaumont-de-Lomagne.

Fibonacci of Pisa 1170—1250 Born: Pisa. Germany Jorgen Gram 1850—1916 Born: Nustrup. France Carl Friedrich Gauss 1777—1855 Born: Brunswick. Italy Joseph Fourier 1768—1830 Born: Auxerre. Denmark 121 .

France Heron of Alexandria about 10AD—about 75AD Born: Alexandria. England Charles Hermite 1822—1901 Born: Dieuze.William Rowan Hamilton 1805—1865 Born: Dublin. Egypt 122 . Ireland G H Hardy 1877—1947 Born: Cranleigh.

Algeria Edmond Laguerre 1834—1886 Born: Bar-le-Duc. France Adrien-Marie Legendre 1752—1833 Born: Paris.Carl Jacobi 1804—1851 Born: Potsdam. France 123 . Germany Gaston Julia 1893—1978 Born: Sidi bel Abbès.

France Colin Maclaurin 1698—1746 Born: Kilmodan. Norway Jules Lissajous 1822—1880 Born: Versailles.Sophus Lie 1842—1899 Born: Nordfjordeide. France Edouard Lucas 1842—1891 Born: Amiens. Scotland 124 .

Marin Mersenne 1588—1648 Born: Oizé in Maine. France Isaac Newton 1643—1727 Born: Woolsthorpe. England Blaise Pascal 1623—1662 Born: Clermont-Ferrand. France Ptolemy about 85AD—about 165AD Born: Egypt 125 .

Pythagoras about 569BC—about 475BC Born: Samos. India Erhard Schmidt 1876—1959 Born: Tartu. Poland 126 . Greece Srinivasa Ramanujan 1887—1920 Born: Tamil Nadu. Estonia Waclaw Sierpinski 1882—1969 Born: Warsaw.

Scotland Brook Taylor 1685—1731 Born: Edmonton. Germany Tsu Chongzhi 429—501 Born: Nanking.Robert Simson 1687—1768 Born: West Kilbride. England Ehrenfried Tschirnhaus 1651—1708 Born: Görlitz. China 127 .

Vito Volterra 1860—1940 Born: Ancona. Scotland More details at the MacTutor website: http://www-history.st-andrews. Italy William Wallace 1768—1843 Born: Dysart.uk/ 128 .ac.mcs.

- Maple by Example
- Maple programming
- Maple by Example 3ed Elsevier 2005 ISBN 0120885263 T O 563s SC PDF
- Adams - Introduction to Mathematics With Maple
- Maple Animation
- Maple
- maple
- Introductory Programming Guide
- Maple User Manual
- Inalcikthe Ottoman Empire
- Helen Vendler
- Article - Mass Extinctions
- Elizabeth Bishop Comprehensive Research and Study Guide Bloom s Major Poets
- Queen Margot 01 Duma
- elizabeth_bishop_2012_7.pdf
- The+painter+of+modern+life_baudelaire
- Partial Differential Equations - An Introduction With Ma Thematic A and Maple
- Auden, W.H. - Selected Poems (Vintage, 1979)
- On the Boundaries of Theological Tolerance in Islam by Sherman a. Jackson
- Maple 13 Manual
- Hyperspace
- Islam Without Extremes - A Muslim Case for Liberty - Mustafa Akyol
- Conceptual Mathematics A first introduction to category theory
- Ian Hacking- Why is There Philosophy of Mathematics at All¿ - [Cambridge University Press - 2014]
- Algebraic Geometry
- Maple

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

We've moved you to where you read on your other device.

Get the full title to continue

Get the full title to continue reading from where you left off, or restart the preview.

scribd