RE: Plotting bounded domains

• To: mathgroup at smc.vnet.net
• Subject: [mg23151] RE: [mg23110] Plotting bounded domains
• From: "David Park" <djmp at earthlink.net>
• Date: Thu, 20 Apr 2000 03:21:17 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```John wrote:
>
>     In Mathematica, how does one plot a bounded and closed domain in R^2
> that includes variables? For example let R be y=x; x=2; y = 0; for the
> function,  f(x,y) = x*y.
>

There are probably a number of solutions to this problem, but I am going to
shamelessly push my DrawingPaper and DrawingCube routines (available at my
web site) which I believe offer a good solution to the problem.

Needs["Graphics`DrawingPaper`"]
Needs["Graphics`DrawingCube`"]

The first step is to produce the plot with a variable limit for y. But
Mathematica does not allow variable limits with plotting iterators (unlike
integration iterators.) So we use the IteratorSubstitution routine from
DrawingCube (it is used more often in 3D plotting).

?IteratorSubstitution
"IteratorSubstitution[expr, {var, lim1, lim2}, newvar:w] is used to
transform \
a plot range iterator, which may contain variable limits, to one which \
contains constant limits. The plotting expr is transformed to the new \
variable and returned along with the new iterator."

IteratorSubstitution[x y, {y, 0, x}]
{w*x^2, {w, 0, 1}}

So instead of plotting x*y versus x and y, we plot  w*x^2 versus x and w,
and w now has a fixed limit.  But we don't want the plot to be displayed
with w coordinates but with y coordinates. So we have to transform the
coordinates of the plot with a function from DrawingPaper called
DrawingTransform.

?DrawingTransform
"DrawingTransform[f1, f2], where f1 and f2 are function names, is a rule
which applies \
the following transformation to all points in the graphics object:
{x_?NumberQ, \
y_?NumberQ} -> {f1[x, y], f2[x, y]}."

In our case y == x*w. In DrawingPaper, ContourDraw replaces ContourPlot. We
can also add other elements within the Show[Graphics[{}]] statement. Here I
have added a solid line around the region.

Show[Graphics[
{ContourDraw[w x^2, {x, 0, 2}, {w, 0, 1}, ColorFunction -> Hue] /.
DrawingTransform[Function[{x, w}, x], Function[{x, w}, x w]],
AbsoluteThickness[2], Line[{{0, 0}, {2, 0}, {2, 2}, {0, 0}}]}],

AspectRatio -> Automatic, PlotRange -> {{0, 2}, {0, 2}}, Background ->
Linen];

The result is a contour plot confined to the triangular region.

But suppose we want to make a contour plot of the same function but with the
upper boundary given by y == Sqrt[x]? This gives such a contour plot with

Show[Graphics[
{ContourDraw[w x^(3/2), {x, 0, 2}, {w, 0, 1}, ContourShading -> False] /.
DrawingTransform[Function[{x, w}, x],
Function[{x, w}, w  Sqrt[x]]],
AbsoluteThickness[2], Draw[ Sqrt[x], {x, 0, 2}],
Line[{{0, 0}, {2, 0}, {2, Sqrt[2]}}]}],

AspectRatio -> Automatic, PlotRange -> {{0, 2}, {0, 2}}, Background ->
Linen,
Frame -> True];

But if we do the same thing with ContourShading -> True, the shaded contour
regions come out very rough. This is because the polygons which define these
regions have some long straight line segments at the boundaries and
DrawingTransform transforms them into new straight line segments, which do
not match the curve that we want. To solution is to breakup these segments
into smaller ones so they can follow the new curved boundary. DrawingPaper
also has a routine to do that.

?PolygonFineGrain
"PolygonFineGrain[expression, distancefuncion, grain] will breakup all \
segments in Polygons into shorter segments if the seperation between two \
adjacent points satisfies the distance function. grain tells how many points
\
the segment will be broken into. The function will be applied to all
polygons \
in the expression. The principal purpose of this function is to fine grain a
\
polygon which is going to be transformed, say by DrawingTransform. Long \
straight segments may not map into straight segments and need to be broken
up \
into shorter segments."

This produces the contour plot in the new region with contour shading, with
a solid line drawn around the region.

Show[Graphics[
{PolygonFineGrain[
ContourDraw[w x^(3/2), {x, 0, 2}, {w, 0, 1},
ColorFunction -> Hue], Sqrt[(#2 - #1).(#2 - #1)] > 0.3 &,
50] /. DrawingTransform[Function[{x, w}, x],
Function[{x, w}, w Sqrt[x]]],
AbsoluteThickness[2], Draw[Sqrt[x], {x, 0, 2}],
Line[{{0, 0}, {2, 0}, {2, Sqrt[2]}}]}],

AspectRatio -> Automatic, PlotRange -> {{0, 2}, {0, 2}}, Background ->
Linen,
Frame -> True];

The DrawingPaperTutorial shows another example of a contour plot in an
annular region.

Finally, the Tom Wickham-Jones ConstrainedContourPlot in his ExtendGraphics
package is useful in many cases, but it does not allow contour shading.

David Park