Re: Evaluation of functions inside Plot

*To*: mathgroup at smc.vnet.net*Subject*: [mg22103] Re: Evaluation of functions inside Plot*From*: "Allan Hayes" <hay at haystack.demon.co.uk>*Date*: Mon, 14 Feb 2000 02:03:54 -0500 (EST)*References*: <87tpki$5iq@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

Hans, Several people have already been pointed out that your input works after some typing errors hve been corrected to give Plot3D[ { Abs[x+y I], Hue[ N[(Pi+Arg[x + y I]) /(2 Pi)]]}, {x,-2,2},{y,-2,2}, AxesLabel->{"Re","Im","Abs[z]"},PlotPoints->15 ]; So I shall try to deal with some aspects or the evaluation ol Plot - at least those concerning the first entry and wrapping Evaluate around it: Plot has the attributes HoldAll, this lets it evaluate in its own way Plot[expr, {x, xmin, xmax}] first looks at the form of the *unevaluated* expr: if this is not a list, then it tries to find points on the line to be drawn by successively evaluating expr with x assigned constructed real numbera as values; it expects that these evaluations will all give real numbers but it does its best with what it finds if it is a list, then it effectivley flattens this to, say, {e1, e2, ...}; evaluates the Plot[ ei, {x,xmin, xmax}] without showing them ; then it shows results together. 1. The following "fails" because, for example, when x =2, Mathematica finds that D[Sin[2],2] makes no sense. Plot[D[Sin[x],x], {x,0,7}]; But if D[Sin[x],x] is made to evaluate to Cos[x] before x receives numerical values, then all is OK Plot[Evaluate[D[Sin[x], x]], {x, 0, 7}]; 2. The following "fails" because, Plot examines the unevaluated expr, finds that it is not a list, and then finds no real values for expr (for example, when x = -2.0 , whereas it evalutes to {0, 1.73205]} - not a real number) In[134]:= expr = {x,Sqrt[x^2-1]}; In[135]:= Plot[expr,{x,-2,2}]; But if expr is made to evaluate to {x,Sqrt[x^2-1]} before the form is checked then Plot finds that it is dealing with a list and behaves as expected - but, of course, it cannot plot Sqrt[x^2-1] for x between -1 and 1) In[136]:= Plot[Evaluate[expr],{x,-2,2}]; Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565 "Hans-Peter Kunzle" <HP.Kunzle at ualberta.ca> wrote in message news:87tpki$5iq at smc.vnet.net... > I still do not understand well enough how expressions get > evaluated inside a Plot or similar functions (NDSolve etc.). > > This time I copied the code directly from the (quite old) > book by T.W. Gray and J. Glynn, Exploring Mathematics with > Mathematica (p.142): > > Plot3D[ > { Abs[x+y I], Hue[ N[(Pi+Arg[x + yI]) /(2 Pi)]]}, > {x,-2,2},{y,-2,2}], > AxesLabel->{"Re","Im","Abs[z]"},PlotPoints->15 > ]; > > This produces several error messages > > Plot3D::plnc: > {Abs[x + y I], Hue[(N[([Pi] + Arg[x + yI])\(2 Pi)]]} is neither a > machine-size real number at {x,y}={-2.,-2.} nor a list of a real number > and a \alid color directive. > > This book was written for Mathematica 2, of course, and presumably > the code must have worked then. Has anything changed since then? > > I have tried to wrap 'Evaluate' around some of the expressions, but > this had no effect. Since Abs and Arg only work for numerical > arguments I also tried to replace them by explicit expressions in > x and y. But this did not help either. > > Any help is appreciated. > > Hans > -- > H.P. Künzle | Office: (780)492-3798,492-3396 > Dept. of Mathematical Sciences | Fax: (780)492-6826 > University of Alberta | E-mail: HP.Kunzle at UAlberta.ca > Edmonton, Canada T6G 2G1 | WWW:http://www.math.ualberta.ca/~hpk >