Application Center - Maplesoft

App Preview:

Classroom Tips and Techniques: Shading between Curves, and Integrating Vectors Componentwise

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

Learn about Maple
Download Application


 

Image 

Classroom Tips and Techniques: Shading between Curves, and Integrating Vectors Componentwise 

 

Robert J. Lopez
Emeritus Professor of Mathematics and Maple Fellow
Maplesoft
 

Introduction 

This month, we will provide a method for shading between two arbitrary curves, and will show how to integrate a vector componentwise.  The first issue is a long-standing one in Maple.  Maple can shade between a curve and the horizontal axis, but cannot easily provide shading between two curves.  Over the years, a variety of methods have been proposed by different users, methods typically requiring special coding to implement.  In Maple 11, the implicitplot command now supports the option filledregions = true/false, and we will show how this new option can be used to shade between curves. 

Our second topic was the subject of a recent inquiry by a colleague I have know for many years.  He's new to the use of the VectorCalculus package, and wanted to know how to integrate componentwise a vector in this package. 

Initializations 

 

> Typesetting:-mrow(Typesetting:-mi(
> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

Shading between Curves 

Shading with the plot Command 

Maple's plot command has long had the functionality of shading between a curve and the horizontal axis.  However, it has never had the ability to shade between two arbitrary curves.  Consider the curves defined by the functions 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 

`+`(1, `-`(`*`(`^`(x, 2))))
`*`(`^`(x, 2))

The Typesetting:-mrow(Typesetting:-mi(-coordinates of the intersections of these curves are given by
 

> Typesetting:-mrow(Typesetting:-mi(
`+`(`-`(`*`(`/`(1, 2), `*`(`^`(2, `/`(1, 2)))))), `+`(`*`(`/`(1, 2), `*`(`^`(2, `/`(1, 2)))))
 
Our ultimate objective will be to explore ways to shade the region bounded by these two curves and the vertical lines Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(.  Maple easily shades from the graph of Typesetting:-mrow(Typesetting:-mi( to the Typesetting:-mrow(Typesetting:-mi(-axis, as we see in Figure 1.
 

> Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(

Plot_2d
 

Figure 1   Shading between the graph of Typesetting:-mrow(Typesetting:-mi( and the Typesetting:-mrow(Typesetting:-mi(-axis 

Unfortunately, provision was never made for displaying the curve in a color different from the color of the shading.  To show the curve in black, for example, takes a superposition of the shaded and unshaded versions of the graph, as we see in Figure 2. 

> Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 2   Graph of Typesetting:-mrow(Typesetting:-mi( shaded between the curve and the Typesetting:-mrow(Typesetting:-mi(-axis 

In Figure 3, we show what happens if we include the graph of Typesetting:-mrow(Typesetting:-mi(, along with shading between this curve and the Typesetting:-mrow(Typesetting:-mi(-axis.  Note the use of the transparency option, without which the pink shading would cover the cyan, making the cyan invisible. 

> Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 3   Shaded graphs of Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( 

Although the region between the bounding curves is shaded in pink, the image in Figure 3 suffers from the cyan shading between the graph of Typesetting:-mrow(Typesetting:-mi( and the Typesetting:-mrow(Typesetting:-mi(-axis.  In Figure 4, we deal with this by shading the region under the graph of Typesetting:-mrow(Typesetting:-mi( in white. 

> Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 4   Shading between the graphs of Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( 

Of course, the graphs of the curves themselves can be made visible by adding them to Figure 4.  This is done in Figure 5. 

> Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

Plot_2d
 

Figure 5   Graphs of Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(with shading of the region between the curves 

Shading with the implicitplot Command 

The following remarks should clarify some aspects of the behavior of the implicitplot command. 

The implicitplot command can be applied to an expression, an equation, or a single inequality. 

As per Table 1, setting the filledregions option to true causes implicitplot to shade regions in the graph.   

Rule 1 

Expression Typesetting:-mrow(Typesetting:-mi(  

Region satisfying Typesetting:-mrow(Typesetting:-mi(shaded one color; satisfying Typesetting:-mrow(Typesetting:-mi(another. 

Rule 2 

Equation 

All terms are moved to the left.  Calling this left side Typesetting:-mrow(Typesetting:-mi(the shading is as in Rule 1. 

Rule 3 

Inequality 

Just the region satisfying the inequality is shaded. 

Table 1   Rules governing the behavior of the filledregions option 

These basic functionalities are illustrated in Figure 6, where the options 

> Typesetting:-mrow(Typesetting:-mi(

are used.
 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 

Plot_2d
 

Figure 6   Rules Typesetting:-mrow(Typesetting:-mn( in Table 1 are illustrated respectively in Figures 6(a), 6(b), and 6(c) 


To extract from these basic functionalities the ability to shade between two arbitrary curves, write the function
 

> Typesetting:-mrow(Typesetting:-mi(
`*`(`+`(y, `-`(1), `*`(`^`(x, 2))), `*`(`+`(y, `-`(`*`(`^`(x, 2))))))


where Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( are as defined in the previous section.  Since the
filledregions option will shade the region where Typesetting:-mrow(Typesetting:-mi(in red and the region where Typesetting:-mrow(Typesetting:-mi(in yellow, Figure 7 shows in red the region satisfying Typesetting:-mrow(Typesetting:-mi(and Typesetting:-mrow(Typesetting:-mi(.  The first factor in Typesetting:-mrow(Typesetting:-mi( is therefore negative; the second, positive.  Hence, the region between the two curves is graph in red because on this region Typesetting:-mrow(Typesetting:-mi(In the upper yellow region, Typesetting:-mrow(Typesetting:-mi(because the inequalities Typesetting:-mrow(Typesetting:-mi(and Typesetting:-mrow(Typesetting:-mi( are both satisfied.  In the lower yellow region, Typesetting:-mrow(Typesetting:-mi(again because now the inequalities Typesetting:-mrow(Typesetting:-mi(and Typesetting:-mrow(Typesetting:-mi( 

 Figure 8 sets the colors to red and white so that just the region between the curves is shaded. 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 

Plot_2d
 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 

Plot_2d
 

Figure 7   Shading between Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( via the device of plotting the product Typesetting:-mrow(Typesetting:-mi( 

Figure 8   The plot in Figure 7 with modified color scheme 

Other Solutions 

 

Here are two other ways to shade between two curves.  The first is the command FillBetweenCurves generated by the following Maple code.  It was written by one of the Maple programmers in response to my constant badgering for a complete solution to the the problem of shading between two curves.  It's not built into Maple because it is as yet only a partial solution to the problem. 

 

> FillBetweenCurves := proc(f1, f2, {[color, colour] := red, transparency := 0.0})
 

>    local p, c, n, pts1, pts2, polys, i, x, ytop, ybottom, yvals;
 

>
 

>    p := plot([f1, f2], _rest, _options['color'], adaptive=false);
 

>    c := select(type, p, 'specfunc'('anything', 'CURVES'));
 

>    if nops(c) <> 2 then
 

>        error "cannot fill between curves";
 

>    end if;
 

>
 

>    pts1 := op([1,1], c);
 

>    pts2 := op([2,1], c);
 

>    n := nops(pts1);
 

>    if n <> nops(pts2) then
 

>        error "cannot fill between curves";
 

>    end if;
 

>
 

>    polys := NULL;
 

>    x := Vector(n, datatype=float);
 

>    ytop := Vector(n, datatype=float);
 

>    ybottom := Vector(n, datatype=float);
 

>    for i to n do
 

>        x[i] := op(1, pts1[i]);
 

>        yvals := op(2, pts1[i]), op(2, pts2[i]);
 

>        ytop[i] := max(yvals);
 

>        ybottom[i] := min(yvals);
 

>    end do;
 

>
 

>    polys := [seq([x[i], ytop[i]], i=1..n), seq([x[i], ybottom[i]], i=n..1, -1),
 

>                  [x[1], ytop[1]]];
 

>    plots[display](plottools[polygon](polys, _options['color'],
 

>        _options['transparency']), p);
 

> end proc:
 


An example of its use is provided by Figure 9.
 

 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(
 

Plot_2d
 

Figure 9   Shading between Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( via FillBetweenCurves command 

On obvious shortcoming of this FillBetweenCurves command is that the curves are colored with the same color as the shading. 

A second solution is provided by the inequalityplot command in the InequalityGraphics package written by Robert Ipanaqu? Chero, available from the Maple Applications Center (InequalityGraphics), and described in the Maple Reporter article A Package for Graphing Solution Sets of Nonlinear Inequalities, also available from the Maple Applications Center (Reporter Article).  The interested reader can follow these links to download the articles and the additional Maple code.  

Integrating a Vector Componentwise 

For a user moving only now from implementing vector calculus with the old linalg package to implementing it with the newer VectorCalculus package, there are many differences to consider.  One difference is how the operations of differentiation and integration are redefined so that they act immediately on objects, and do not have to be mapped onto them as in the old linalg package. 

Just recently, a colleague I've known for many years began the transition to VectorCalculus.  I received the following question from him soon after. 

How can the vector 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mverbatim(


be integrated componentwise in the
VectorCalculus package?  In the VectorCalculus package, differentiate this vector with 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-maction(Typesetting:-mverbatim(

or even
 

> Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-mo(
Typesetting:-maction(Typesetting:-mverbatim(
 


The action of the
diff command is immediate, and does not require the older syntax Typesetting:-mrow(Typesetting:-mi(  

As with diff, the int command is also immediate in the VectorCalculus package, but integration requires a bit more care.  If we issue the command 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-maction(Typesetting:-mverbatim(

or even
 

> Typesetting:-mrow(Typesetting:-mo(
Typesetting:-maction(Typesetting:-mverbatim(
 


the integration is indeed immediate.  But what if we want the unevaluated definite integral?  This is indeed what my colleague wanted, because ultimately, the vectors he was integrating required numeric integration.  The unevaluated integral in the
VectorCalculus package is obtained with the int command, not the Int command. 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mverbatim(
 
Notice the use of the "inert" parameter to obtain the equivalent of the
Int command.  With this formulation, numeric evaluation of the vector of componentwise integrals is obtained with 

> Typesetting:-mrow(Typesetting:-mi(
Typesetting:-maction(Typesetting:-mverbatim(
 
and does not require mapping the
evalf command either. 

This violation of the top-level paradigm of int for immediate integration, and Int for delayed, is a consequence of the additional functionality given int in the VectorCalculus package, where iterated integration, and integration over predefined domains are both supported.  Details and examples are available on the help page for VectorCalculus,int. 


Legal Notice: The copyright for this application is owned by the author(s). Neither Maplesoft nor the author are responsible for any errors contained within and are not liable for any damages resulting from the use of this material. This application is intended for non-commercial, non-profit use only. Contact the author for permission if you wish to use this application in for-profit activities.
 

Image