plotcompare - Maple Help

Home : Support : Online Help : Graphics : 2-D : plotcompare

plots

 plotcompare
 compare formulae graphically

 Calling Sequence plotcompare(f(z), g(z), z = a+c*I..b+d*I, options); plotcompare(f(z) = g(z), z = a+c*I..b+d*I , options); plotcompare(f, g, a+c*I..b+d*I, options); plotcompare(f = g, a+c*I..b+d*I, options);

Parameters

 f, g - functions or expressions to be plotted z - variable name a, b, c, d - real constants options - expression_plot, same_box = true, scale_range = positive number, shift_range = complex number, colors=[c1, c2], and any of the optional arguments accepted by plot, plot3d and plots[display]

Description

 • The plotcompare command generates plots of the real and imaginary parts of two expressions, so that you can see where, if anywhere, they differ on a given domain. plotcompare can also generate plots for the real and imaginary parts of a single expression just for visualization purposes (see optional argument expression_plot explained below). When comparing two expressions f(z) and g(z), the output of plotcompare consists of four related plots as follows

$\mathrm{Re}\left(f\left(z\right)\right)\mathrm{Re}\left(g\left(z\right)\right)$

$\mathrm{Im}\left(f\left(z\right)\right)\mathrm{Im}\left(g\left(z\right)\right)$

 • If the domain specified, z = a+c*I..b+d*I, is real, that is, $c=d=0$, the plotcompare command generates four 2-D plots of $\mathrm{\Re }\left(f\left(z\right)\right)$, $\mathrm{\Re }\left(g\left(z\right)\right)$, $\mathrm{\Im }\left(f\left(z\right)\right)$, and $\mathrm{\Im }\left(g\left(z\right)\right)$ on $z=a..b$. If the domain specified is imaginary, that is, $a=b=0$, plotcompare generates these four 2-D plots on $z=Ic..Id$.
 • If the domain specified z = a+c*I..b+d*I is complex, the plotcompare command generates four 3-D plots of $\mathrm{\Re }\left(f\left(z\right)\right)$, $\mathrm{\Re }\left(g\left(z\right)\right)$, $\mathrm{\Im }\left(f\left(z\right)\right)$, and $\mathrm{\Im }\left(g\left(z\right)\right)$, where $z=x+Iy$ and $a and $c.
 • If the domain is not given, it defaults to be the complex unit box $-1-I..1+I$.
 • The third or its equivalent fourth calling sequence corresponds to the first one. They allow the expressions to be input as procedures of one argument instead of formulae in a variable.  In these cases the domain argument must be a range (real or complex), not an equation with a variable on the left-hand side.
 • The global variable _P is assigned to a 2 by 2 array of the four plots $\mathrm{\Re }\left(f\left(z\right)\right)$, $\mathrm{\Re }\left(g\left(z\right)\right)$, $\mathrm{\Im }\left(f\left(z\right)\right)$, and $\mathrm{\Im }\left(g\left(z\right)\right)$. This array can be accessed by the user as desired for subsequent manipulation.
 • The plotcompare command generates the four plots by first performing four calls to either the plot command or the plot3d command, then creating an array with these plots and assigning it to the variable $\mathrm{_P}$, finally sending this array $\mathrm{_P}$ to the plots[display] command.
 • CAVEAT: Sometimes, the two expressions or functions being compared are equal over the complex plane but differ only in the value at a point or line (for example, a branch cut). Consider the functions $f\left(x\right)=\frac{1}{\sqrt{x}}$ and $g\left(x\right)=\sqrt{\frac{1}{x}}$. In these cases the 3-D plots will appear visually identical. To make the difference on the values visible use plotcompare to generate 2-D plots, for instance, giving a real (or purely imaginary) range, or directly using the assuming facility as shown in the examples.
 • This command is part of the plots package, so it can be used in the form plotcompare(..) only after executing the command with(plots). However, it can always be accessed through the long from of the command by using plots[plotcompare](..).

Optional arguments

 expression_plot
 This optional argument is used to generate the plots of the real and imaginary parts of a single expression passed as first argument; it is useful to visualize a given mathematical function or expression without making any comparison.
 same_box=true
 Using this optional argument results in the plots of the real parts of both functions being superimposed on the same plot, and the graphs of the imaginary parts of both functions being superimposed on the same plot.  The real and imaginary parts of the first function are given the color red and the second function blue. In this case the variable $\mathrm{_P}$ is assigned an array of only two plots.
 scale_range=c
 If the optional argument scale_range=c is given, the range of the plot is scaled by a factor of c where c must be a positive real constant. Note this factor can be less than one.
 shift_range= r + s I
 When this optional argument is given, the range of the plot is shifted from its value, say $\left(a+bI\right)\mathrm{..}\left(c+dI\right)$ to $\left(a+r+\left(b+s\right)I\right)\mathrm{..}\left(c+r+\left(d+s\right)I\right)$. This is useful when the difference between the expressions being plotted is visible only after shifting the plotting range (for example, when comparing some functions with their series representation. See Examples section).
 colors=[c1,c2]
 If the optional argument colors=[c1,c2] or colours=[c1,c2] is given, the real and imaginary parts of the first function are given the color c1 and the real and imaginary parts of the second function c2.
 • The following standard options are used by default by the plotcompare command when displaying the plots.

 orientation = [-18,75] axes = FRAMED, axesfont = [TIME,ROMAN,10] shading = ZHUE grid = [10,10]

 To override these defaults, you can specify different options as is usually done with the plot, plot3d, or plots[display] commands.

Examples

 > $\mathrm{with}\left(\mathrm{plots}\right):$

A comparison of $\mathrm{sin}\left(x\right)$ with its cubic Taylor polynomial. (Note that if there is no imaginary part, as in this example, the graphs of $\mathrm{\Im }\left(f\right)$ and $\mathrm{\Im }\left(g\right)$ will be empty.)

 > $\mathrm{plotcompare}\left(\mathrm{sin}\left(x\right),x-\frac{{x}^{3}}{6},x=-3..3\right)$

If you shift the range, for example to the right by 10, the function and its cubic approximation do not look alike anymore.

 > $\mathrm{plotcompare}\left(\mathrm{sin}\left(x\right),x-\frac{{x}^{3}}{6},x=-3..3,\mathrm{shift_range}=10\right)$

Is the cube root of -8 equal to -2? In Maple, the function ${x}^{1}{3}}$ means the principal root, which is not real for $x<0$.  Hence the following are not the same for negative real x.

 > $\mathrm{plotcompare}\left({x}^{\frac{1}{3}},\mathrm{signum}\left(x\right){\mathrm{abs}\left(x\right)}^{\frac{1}{3}},x=-2..2\right)$

Comparing the same expressions but using the same_box option and optional arguments of plot for thickness and coloring: ${x}^{1}{3}}$ appears in blue and and $\mathrm{signum}\left(x\right){\left|x\right|}^{1}{3}}$ in green.

 > $\mathrm{plotcompare}\left({x}^{\frac{1}{3}},\mathrm{signum}\left(x\right){\mathrm{abs}\left(x\right)}^{\frac{1}{3}},x=-2..2,\mathrm{same_box}=\mathrm{true},\mathrm{thickness}=3,\mathrm{colors}=\left[\mathrm{blue},\mathrm{green}\right]\right)$

In the above plots, for positive values of $x$ both the real and imaginary parts of the expressions being compared are the same while for negative values of $x$ they are different. A 3-D plot can make the regions where these expressions are equal or different more evident. (Try rotating this plot with the mouse and using the same_box option).

 > $\mathrm{plotcompare}\left({x}^{\frac{1}{3}},\mathrm{signum}\left(x\right){\mathrm{abs}\left(x\right)}^{\frac{1}{3}},\mathrm{scale_range}=2\right)$

The following example shows two expressions which differ in value only over a line, so that the 3-D plots are visually the same.

 > $\mathrm{plotcompare}\left(\frac{1}{\mathrm{sqrt}\left(x\right)}=\mathrm{sqrt}\left(\frac{1}{x}\right),\mathrm{grid}=\left[20,20\right]\right)$

Though not visible, the difference is in the value of the imaginary parts, $\mathrm{\Im }\left(f\right)$ and $\mathrm{\Im }\left(g\right)$, when taken over real and positive values of $x$. Note the branch cut. To examine the cut, you can rotate the plots above with the mouse.

The procedure recommended to turn the difference (if any) visible in cases like this, that is, whenever there are branch cuts in the 3-D plots, is to use plotcompare to generate 2-D plots, for instance giving a real range or using the assuming facility together with the optional argument same_box.

 > $\mathrm{plotcompare}\left(\frac{1}{\mathrm{sqrt}\left(x\right)}=\mathrm{sqrt}\left(\frac{1}{x}\right),\mathrm{same_box}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{assuming}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{real}$

In the example above, the expressions are not valid for real (Im(x) = 0) and negative values of x. For any other point in the complex plane not in RealRange(-infinity, 0) these expressions have the same value as suggested by the 3-D plots. For instance, the following two pairs of curves show that for purely imaginary values of $x$ the two functions agree.

 > $\mathrm{plotcompare}\left(\frac{1}{\mathrm{sqrt}\left(x\right)}=\mathrm{sqrt}\left(\frac{1}{x}\right),\mathrm{same_box}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{assuming}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{imaginary}$

In the two input lines above you can also use the option scale_range = N together with assuming, so that the range for the plots will respectively be x = -N..N and x = -N*I..N*I.

The following suggests that $\mathrm{ln}\left(z\left(1-z\right)\right)=\mathrm{ln}\left(z\right)+\mathrm{ln}\left(1-z\right)$

 > $\mathrm{plotcompare}\left(\mathrm{ln}\left(z\left(1-z\right)\right)=\mathrm{ln}\left(z\right)+\mathrm{ln}\left(1-z\right),z=-10-10I..10+10I\right)$

while this other plot shows that $\mathrm{ln}\left(z\left(1+z\right)\right)\ne \mathrm{ln}\left(z\right)+\mathrm{ln}\left(1+z\right)$:

 > $\mathrm{plotcompare}\left(\mathrm{ln}\left(z\left(1+z\right)\right)=\mathrm{ln}\left(z\right)+\mathrm{ln}\left(1+z\right),\mathrm{scale_range}=10\right)$

This is the plot of the "expression" (in this case a function) BesselK(1/2,z), illustrating the branch cut of BesselK for Re(z) < 0

 > $\mathrm{FunctionAdvisor}\left(\mathrm{branch_cuts},\mathrm{BesselK}\right)$
 $\left[{\mathrm{BesselK}}{}\left({a}{,}{z}\right){,}{z}{<}{0}\right]$ (1)
 > $\mathrm{plotcompare}\left(\mathrm{BesselK}\left(\frac{1}{2},z\right),\mathrm{expression_plot},3\right)$

The branch cut is visible in the second plot box for the imaginary part of the function and is located on the real axis, negative side. To manipulate each of the plots further enter ${\mathrm{_P}}_{1}$ for the real box plot or ${\mathrm{_P}}_{2}$ for the imaginary box plot, like in:

 > $\mathrm{_P}\left[2\right]$

The following example illustrates the use of a procedure to input the expressions being plotted. The real and imaginary parts of $f$ and $g$ are equal only for $0<\mathrm{\Re }\left(z\right)$.

 > $f≔z↦\mathrm{sqrt}\left({z}^{2}\cdot \left(1-z\right)\right)$
 ${f}{≔}{z}{↦}\sqrt{{{z}}^{{2}}{\cdot }\left({1}{-}{z}\right)}$ (2)
 > $g≔z↦z\cdot \mathrm{sqrt}\left(1-z\right)$
 ${g}{≔}{z}{↦}{z}{\cdot }\sqrt{{1}{-}{z}}$ (3)
 > $\mathrm{plotcompare}\left(f,g,-1-I..1+I\right)$

To see where $f\left(z\right)$ differs from $g\left(z\right)$ input:

 > $\mathrm{plot3d}\left(\mathrm{abs}\left(f\left(x+Iy\right)-g\left(x+Iy\right)\right),x=-1..1,y=-1..1,\mathrm{axes}=\mathrm{FRAMED}\right)$

By constructing a piecewise function $h$, the plots become the same. This uses the same_box option to superimpose the plots for $f$ and $h$.

The default range of the plots below is -1-I..1+I.

 > $h≔z↦\mathrm{piecewise}\left(0<\mathrm{\Re }\left(z\right),g\left(z\right),\mathrm{\Re }\left(z\right)\le 0,f\left(z\right)\right)$
 ${h}{≔}{z}{↦}\left\{\begin{array}{cc}{g}{}\left({z}\right)& {0}{<}{\mathrm{\Re }}{}\left({z}\right)\\ {f}{}\left({z}\right)& {\mathrm{\Re }}{}\left({z}\right){\le }{0}\end{array}\right\$ (4)
 > $\mathrm{plotcompare}\left(f,h,\mathrm{same_box}\right)$

Consider the following identity relating the Airy and Bessel functions, taken from the Handbook of Mathematical Functions by Abramowitz and Stegun. The following plots show that this identity is, in fact, not valid over the whole complex plane.  This is the command to create the plot from the Plotting Guide.

 > $\mathrm{id}≔\mathrm{AiryAi}\left(z\right)=\frac{\mathrm{sqrt}\left(3z\right)\mathrm{BesselK}\left(\frac{1}{3},\frac{2}{3}{z}^{\frac{3}{2}}\right)}{3\mathrm{\pi }}$
 ${\mathrm{id}}{≔}{\mathrm{AiryAi}}{}\left({z}\right){=}\frac{\sqrt{{3}}{}\sqrt{{z}}{}{\mathrm{BesselK}}{}\left(\frac{{1}}{{3}}{,}\frac{{2}{}{{z}}^{{3}}{{2}}}}{{3}}\right)}{{3}{}{\mathrm{\pi }}}$ (5)
 > $\mathrm{plotcompare}\left(\mathrm{id},\mathrm{grid}=\left[15,15\right]\right)$

The region of validity can be explored visually by superimposing the plots of the real parts and then the imaginary parts of the two functions. You can do this using the same_box option or without recomputing the plots taking advantage of the $\mathrm{_P}$ variable to which the array of plots was assigned when calling plotcompare.

 > $\mathrm{display}\left(\left[\mathrm{_P}\left[1,1\right],\mathrm{_P}\left[1,2\right]\right],\mathrm{orientation}=\left[-60,75\right]\right)$
 > $\mathrm{display}\left(\left[\mathrm{_P}\left[2,1\right],\mathrm{_P}\left[2,2\right]\right],\mathrm{orientation}=\left[-60,75\right]\right)$

The region of validity of the identity can also be explored by plotting for z real or imaginary.

 > $\mathrm{plotcompare}\left(\mathrm{id}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{assuming}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{real}$

 > $\mathrm{plotcompare}\left(\mathrm{id}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{assuming}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathrm{imaginary}$

Consider the following identity.

 > $\mathrm{id}≔\mathrm{hypergeom}\left(\left[a\right],\left[\frac{1}{2}\right],\frac{1}{2}{z}^{2}\right)=\frac{{2}^{a-1}\mathrm{exp}\left(\frac{1}{4}{z}^{2}\right)\mathrm{\Gamma }\left(a+\frac{1}{2}\right)}{{\mathrm{\pi }}^{\frac{1}{2}}}\left(\mathrm{CylinderU}\left(2a-\frac{1}{2},z\right)+\mathrm{CylinderU}\left(2a-\frac{1}{2},-z\right)\right)$
 ${\mathrm{id}}{≔}{\mathrm{hypergeom}}{}\left(\left[{a}\right]{,}\left[\frac{{1}}{{2}}\right]{,}\frac{{{z}}^{{2}}}{{2}}\right){=}\frac{{{2}}^{{a}{-}{1}}{}{{ⅇ}}^{\frac{{{z}}^{{2}}}{{4}}}{}{\mathrm{\Gamma }}{}\left({a}{+}\frac{{1}}{{2}}\right){}\left({\mathrm{CylinderU}}{}\left({2}{}{a}{-}\frac{{1}}{{2}}{,}{z}\right){+}{\mathrm{CylinderU}}{}\left({2}{}{a}{-}\frac{{1}}{{2}}{,}{-}{z}\right)\right)}{\sqrt{{\mathrm{\pi }}}}$ (6)

The Maple simplify command is not able to prove that this identity is valid for arbitrary values of a.

 > $\mathrm{simplify}\left(\left(\mathrm{lhs}-\mathrm{rhs}\right)\left(\mathrm{id}\right)\right)$
 ${-}\frac{{-}{\mathrm{hypergeom}}{}\left(\left[{a}\right]{,}\left[\frac{{1}}{{2}}\right]{,}\frac{{{z}}^{{2}}}{{2}}\right){}\sqrt{{\mathrm{\pi }}}{+}{{2}}^{{a}{-}{1}}{}{{ⅇ}}^{\frac{{{z}}^{{2}}}{{4}}}{}{\mathrm{\Gamma }}{}\left({a}{+}\frac{{1}}{{2}}\right){}\left({\mathrm{CylinderU}}{}\left({2}{}{a}{-}\frac{{1}}{{2}}{,}{z}\right){+}{\mathrm{CylinderU}}{}\left({2}{}{a}{-}\frac{{1}}{{2}}{,}{-}{z}\right)\right)}{\sqrt{{\mathrm{\pi }}}}$ (7)

Although you cannot prove this identity using plotcompare, it does provide a quick indication that the identity is true for different values of the parameter $a$.

 > $\mathbf{for}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}a\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{in}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\left[-2,-\frac{1}{3},1\right]\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{__________________________________};\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}'a'=a;\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{2.0em}{0.0ex}}\mathrm{plotcompare}\left(\mathrm{id},\mathrm{grid}=\left[5,5\right]\right)\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\mathbf{end}\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}\mathbf{do}$
 ${\mathrm{__________________________________}}$
 ${a}{=}{-2}$

 ${\mathrm{__________________________________}}$
 ${a}{=}{-}\frac{{1}}{{3}}$

 ${\mathrm{__________________________________}}$
 ${a}{=}{1}$

This identity can in fact be verified exactly for arbitrary values of $a$, if you first convert to hypergeom.

 > $\mathrm{simplify}\left(\left(\mathrm{lhs}-\mathrm{rhs}\right)\left(\mathrm{convert}\left(\mathrm{id},\mathrm{hypergeom}\right)\right)\right)$
 ${0}$ (8)

The command to create the 2-D plot from the Plotting Guide is

 > $\mathrm{plotcompare}\left(-{\mathrm{sin}\left(x\right)}^{2}-2+2I,-{\mathrm{sin}\left(2x\right)}^{2}-2+2I,x=-\mathrm{\pi }..\mathrm{\pi }\right)$