Application Center - Maplesoft

App Preview:

Classroom Tips and Techniques: Gems 31-35 from the Red Book of Maple Magic

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application




 

Classroom Tips and Techniques: Gems 31-35 from the Red Book of Maple Magic

NULL

Robert J. Lopez

Emeritus Professor of Mathematics and Maple Fellow

MaplesoftNULL

``

Introduction

 

Five of the Tips & Techniques articles in 2011 were "Maple Magic" articles, each containing five "gems" gleaned from interactions with Maple and the Maplesoft programmers. The Tips & Techniques article in the December 2012 Reporter contained five more additions to the Red Book. Here are five recent revelations (at least to me) that now populate my Red Book of Maple Magic.

 

Gem 31

``

An example in the help-page worksheet shows how to combine a numeric solution for an initial value problem with parameters and the new Explore command for generating graphs with sliders to control those parameters. Earlier in the development cycle, the developer of the Explore update emailed me a simpler form of the example now in the help page, and that is what I'd like to present here as Gem 31.

 

Recall that at one time solving an IVP numerically required all parameters to be numeric. Then, functionality was added to keep the parameters as names within the procedures produced by the call to dsolve, but before these procedures could be invoked, values would have to be passed to the parameters. An example of this, a simple driven damped harmonic oscillator started with inert initial conditions, is given in Table 31.1 Of course, this model can be solved symbolically, but for simplicity, seek a strictly numeric solution.

 

• 

Enter a differential equation in which a and b are parameters.

q := diff(y(t), t, t)+a*(diff(y(t), t))+b*y(t) = cos(2*t)

• 

Apply the dsolve command with the appropriate syntax for a numeric solution with parameters.

Q := dsolve({q, y(0) = 0, (D(y))(0) = 0}, y(t), numeric, parameters = [a, b])

• 

Pass parameter values to the procedure Q.

Q(parameters = [4, 13])

• 

Apply the odeplot command to Q to draw a graph of the solution corresponding to the values of the parameters a and b just passed to Q.

plots:-odeplot(Q, t = 0 .. 5)

Table 31.1   Numeric solution of an IVP containing parameters

 

Table 31.2 shows how to write a procedure that continually passes parameter values to the procedure Q, and how to apply the Explore command to that procedure.

``

• 

Define a procedure Y that passes parameter values to the procedure Q and returns a graph of solution y(t) via the odeplot command.

Y := proc (u, v) Q(parameters = [u, v]); plots:-odeplot(Q, [t, y(t)], 0 .. 25, view = [0 .. 25, -5 .. 5]) end proc

• 

Apply the Explore command with ranges and initial values for the parameters.

Explore(Y(a, b), parameters = [a = 0.1e-1 .. 2, b = 0.1e-1 .. 5], initialvalues = [a = .18, b = 2.45])

a:

b:

Table 31.2   The Explore command used to draw a graph with two sliders controlling parameter values

``

The sliders now control the values of the parameters for the given IVP that is solved numerically.

``

Gem 32

``

The content of Table 32-1, entered into the Red Book of Maple Magic on February 20, 2013, represents some recently-discovered (for me) manipulations with lists.

 

• 

Define the list A.

A := [r, s, t, w]

• 

Use bracket notation to extract from A, a sublist containing the first three elements of A.

A[1 .. 3] = [r, s, t]``

• 

Use subscripts to extract from A, a sublist containing the first three elements of A.

A[1 .. 3] = [r, s, t]``

• 

Use brackets and the notation for "from first to last" to obtain the list A.

A[1 .. -1] = [r, s, t, w]``

• 

Use subscripts to obtain the complete list A.

A[1 .. -1] = [r, s, t, w]``

• 

Define the list alpha consisting of positions of elements of the list A.

alpha := [4, 3, 2]

• 

Use bracket notation to extract from A, a sublist of elements in the positions recorded in alpha.

A[alpha] = [w, t, s]``

• 

Use subscript notation to extract from A, a sublist of elements in the positions recorded in alpha.

A[alpha] = [w, t, s]``

Table 32-1   Operations on a list

``

Some history: I first taught with Maple in 1988 (probably Release 2). In 1995 when RHIT introduced laptops into the classroom, I can recall preparing material in Release 3. In the summer of 1996 I visited Maplesoft and recall asking questions about Release 4. I continued using Release 4 to complete my Advanced Engineering Math text with Addison Wesley. I started working on the manuscript in the fall of 1997 and the text (with a 2001 copyright date) was available in November of 2000. By this time Releases 5 and 6 had appeared, and the worksheets that accompanied the text had been updated to those versions.

 

Prior to Release 5, the Maple notation x.(1 .. n) would generate the sequence of names x1, x2, () .. (), xn. In Release 5, the dot was sequestered for the noncommutative operator of linear algebra, and its role in generating sequences of names was taken by a double slash, and in x || (1 .. n). This doesn't look too bad in typeset math, but it looks terrible when typed in the text that Maple calls 1D math. I hated this new notation, and found its alternative, the use of table entries x[k] with the concomitant seq(x[k], k = 1 .. n) to be more typing than I wanted to burden my students with.

 

On March 19, 2013 I added the contents of Table 32-2 to the Red Book.

 

• 

Assign to two "subscripted" variables.
(A becomes the name of a table, and the subscripts refer to entries in this table.)

A[1] := a; A[2] := b

• 

Use the $ character as the sequence operator, and generate a sequence of values for the "subscripted" names.

`$`(A[k], k = 1 .. 2)

a, b

Table 32-2   Forming a sequence of indexed names

``

I guess the surprise is that it took me 15 years to see this.

``

Gem 33

NULL

Example 18.5.2 from my Advanced Engineering Math ebook explores the equipotentials of the electric field set up by two equal positive charges, one at the origin and one at 5, 5 in the Cartesian plane. The potential is

 

U := 1/sqrt(x^2+y^2)+1/sqrt((x-5)^2+(y-5)^2)

 

The Addison-Wesley 2001 version of my AEM text was originally written with Maple V Release 4, and the worksheets that accompanied it have been updated to every version of Maple since. At Maple 10, having gone out of print, it became the ebook. Now the contour plot of the potential function (Figure 18.5.2 in the ebook) was last obtained in Maple 16 with the following code, which I advise against executing in Maple 17!

 

plots:-contourplot(U, x = -5 .. 10, y = -5 .. 10, contours = [.4, .6, .7, .8, .9, 1, 2, 3], color = black, grid = [70, 70], tickmarks = [6, 6], labels = [x, y], scaling = constrained)

 

Worked fine in Maple 16, but in Maple 17 it produced the image shown in Figure 33-1.

NULL

Figure 33.1   Contour plot produced by Maple 17

``

One of my go-to guys, Dr. Allan Wittkopf, provided me with the following analysis. The 70×70 grid implies a nodal spacing of delta = (10+5)/(70-1) = 5/23, which makes the points 0, 0 and 5, 5 nodes. But U(x, y) is singular at these points. Hence, very large values are computed at these points and "the values of the function are considered relative to the maximum value of the function computed on the grid."

 

Clearly, then, I could modify the grid slightly so the singularities are not nodes. But Allan suggested a better alternative: "You can always get a good plot at any resolution by capping the value of the function to something sane." He suggested the code that results in Figure 33.2.

 

plots:-contourplot(min(10, U), x = -5 .. 10, y = -5 .. 10, contours = [.4, .6, .7, .8, .9, 1, 2, 3], color = black, grid = [70, 70], tickmarks = [6, 6], labels = [x, y], scaling = constrained)

Figure 33.2   Contour plot for min(10, U)

``

I have no idea why code that fails in Maple 17 should have worked in Maple 16, but the "gem" of graphing min(10, U) is one that I must try to remember.

``

Gem 34

``

Back on October 3, 2012, our graphics developer clarified a misconception I had long held about the sample option in the plot command. I had always believed that the list of points provided to this option had to be inclusive of all the points that would be used to generate the graph. Not so, I learned. Giving the option a list of the form [x[L], () .. (), x[R]] and letting adaptive plotting fill in between the two endpoints would force the sample points into the plot data structure that would also contain the additional adaptive points.

 

As an example, consider the graph of the ellipse defined by the equation 3*x^2-5*x*y+7*y^2 = 11, and drawn in Figure 34.1 by the implicitplot command.

 

plots:-implicitplot(3*x^2-5*x*y+7*y^2 = 11, x = -4 .. 4, y = -2 .. 2, gridrefine = 2, scaling = constrained)

Figure 34.1   Ellipse graphed with the implicitplot command

 

Solving for y = y(x) explicitly produces the two branches

 

q := solve(3*x^2-5*x*y+7*y^2 = 11, y)

(5/14)*x+(1/14)*(-59*x^2+308)^(1/2), (5/14)*x-(1/14)*(-59*x^2+308)^(1/2)

 

whose graph, naively drawn, appears in Figure 34.2.

 

plot([q], x = -3 .. 3, y = -2 .. 2, color = [black, red], scaling = constrained)

Figure 34.2   Naive graph of the separate branches of an ellipse

 

Note the small gap between the branches. This occurs because Maple does not sample either branch of y(x) exactly at the last value for which sqrt(-59*x^2+308) remains real. The plot command with the option

 

sample = [-(2/59)*sqrt(4543), (2/59)*sqrt(4543)] 

 

produces the graph in Figure 34.3. Of course, the endpoints are the zeros of -59*x^2+308.

 

plot([q], x = -3 .. 3, color = [black, red], sample = [-(2/59)*sqrt(4543), (2/59)*sqrt(4543)], scaling = constrained)

Figure 34.3   Branches of ellipse graphed with the sample option

 

(The actual graphing commands are hidden input for the tables in which the graphs are displayed.)

``

Gem 35

NULL

Corresponding elements of vectors and lists can be set equal to each other by the Equate command, which has a counterpart in the Context Menu system. There is also a keyboard equivalent to this functionality. Table 35.1 lists the possibilities.

NULL

The Equate command

• 

The Equate command applied to any combination of list and vector returns a list of equations.

Equate(`<,>`(x, y), `<,>`(1, 2)) 0 [x = 1, y = 2]

Equate(`<,>`(x, y), [1, 2]) 0 [x = 1, y = 2]

Equate([x, y], `<,>`(1, 2)) 0 [x = 1, y = 2]

Equate([x, y], [1, 2]) 0 [x = 1, y = 2]

Context Menu

• 

If at least one of the two objects is a vector, then the Context Menu will only contain the Equate option for the output form of the objects.

 

• 

The return is a list of equations.

`<,>`(x, y), `<,>`(1, 2) = Vector(2, {(1) = x, (2) = y}), Vector(2, {(1) = 1, (2) = 2})(->)[x = 1, y = 2]

`<,>`(x, y), [1, 2] = Vector(2, {(1) = x, (2) = y}), [1, 2](->)[x = 1, y = 2]

[x, y], `<,>`(1, 2) = [x, y], Vector(2, {(1) = 1, (2) = 2})(->)[x = 1, y = 2]NULL

• 

The Context Menu for a pair of lists provides the Equate option for input as well as output.

[x, y], [1, 2](->)[x = 1, y = 2]

Keyboard

• 

The help page for "tilde" brings up a list of "element-wise operators," one of which is the element-wise equal operator, =~.

 

• 

Placing the "element-wise equal operator" between two objects, one of which is a vector, results in a vector of equations.

`~`[`=`](`<,>`(x, y), `<,>`(1, 2)) 0 Vector(2, {(1) = x = 1, (2) = y = 2})

`~`[`=`](`<,>`(x, y), [1, 2]) 0 Vector(2, {(1) = x = 1, (2) = y = 2})

`~`[`=`]([x, y], `<,>`(1, 2)) 0 Vector(2, {(1) = x = 1, (2) = y = 2})

• 

Placing the "element-wise equal operator" between two lists results in a list of equations.

`~`[`=`]([x, y], [1, 2]) 0 [x = 1, y = 2]

Table 35.1   The Equate command, the Equate option in the Context Menu, and the element-wise equal operator

NULL

There are Maple commands that require expressions, not equations. In particular, these expressions would be the left-hand sides of an equation in which all terms were moved to the left. If any of the devices in Table 35.1 were used, additional processing would be needed to turn each equation into an expression. The alternatives are shown in Table 35.2.

 

• 

The difference of two vectors returns a vector of differences.

• 

Context Menu: Conversions_To List

`<,>`(x, y)-`<,>`(1, 2) = Vector(2, {(1) = x-1, (2) = y-2})(->)[x-1, y-2]NULL

• 

Arithmetic operations between lists and vectors are not recognized as valid mathematics in Maple.
Neither of these actions are viable alternatives.

`<,>`(x, y)+[-1, -2] = [-1, -2]+(Vector(2, {(1) = x, (2) = y}))NULL

[x, y]-`<,>`(1, 2) = [x, y]+(Vector(2, {(1) = -1, (2) = -2}))NULL

• 

The difference of two lists is a list of differences.

[x, y]+[-1, -2] = [x-1, y-2]NULL

Table 35.2   Alternatives for obtaining expressions instead of equations

NULL

``

Legal Notice: © Maplesoft, a division of Waterloo Maple Inc. 2013. Maplesoft and Maple are trademarks of Waterloo Maple Inc. This application may contain errors and Maplesoft is not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact Maplesoft for permission if you wish to use this application in for-profit activities.