Re: Errors While Creating Simple Graph
- To: mathgroup at smc.vnet.net
- Subject: [mg8356] Re: Errors While Creating Simple Graph
- From: tburton at cts.com (Tom Burton)
- Date: Tue, 26 Aug 1997 02:22:51 -0400
- Organization: Brahea Consulting
- Sender: owner-wri-mathgroup at wolfram.com
On 22 Aug 1997 02:49:16 -0400, in comp.soft-sys.math.mathematica you = wrote: >When I ask Mathematica (2.2) to perform the following plot, it works as >expected, drawing a plot of two intersecting lines: > > >In[1]:=3D > Plot[{x + 1, 2x - 1}, {x, 0, 5}] > >Out[1]=3D > -Graphics- > > >However, when I use substitution to provide the list of equations, >something goes wrong, and the resulting graph is empty... This is a genuine FAQ! The source of the symptom is the HoldAll attribute= of Plot. (Try Attributes[Plot].) This causes "%2" not to be evaluated as= the arguments of Plot are parsed. This parsing distinguishes between a = single ordinate and a list of ordinates. Since "%2" does not have the = form of a list, the overlay of Plot that is invoked processes only a = single argument. Later, when %2 is expanded (repeatedly and redundantly = for every abcissa!), the list is recovered, but too late. The solution (see the Mathematica Book or online help) is to Evaluate the= function to be plotted: Plot[Evaluate[%2],{x,0,5}] Evaluate[] is recommended for most plots. So is Mathematica trying to = make your life miserable with HoldAll? No, because pre-evaluation is not = always possible, and when pre-evaluation is erroneously applied, the = result can be the worst type of error: a wrong answer. An earlier post = of mine (or an attemped post--my ISP usually drops the ball on the way = from UseNet to the mathgroup) discusses what can happen if you reverse = the Hold* attribute or override it with Evaluate[] :-- -- begin earlier post (February 1997) -- When you override Hold*, you assert that the avalanche of transformations resulting from pre-evaluation will stop short of anything that should = wait until the local independent variable is set. Here are two examples where = the override fouls up: ClearAll[f,g,x]; f=3DSign[x]; g[x_]:=3DRandom[]; Plot[g[x],{x,-1,1}] x=3D1; Plot[f,{x,-1,1}]; Both graphs are straight lines without Hold* (or Unevaluated[]). The = function f pre-evaluates to 1 because the global variable x is set. The function = g[x_] pre-evaluates to THE NUMERICAL EVALUATION OF Random[] because it can: = Random[] is manifestly independent of x. Yet what I want is repeated evaluation of Random[]. I know that these examples are trivial (and what good is a "plot" of = noise?), but my experience with numerical analysis is that similar but less = trivial problems are common, so Hold is needed much of the time. -- (end of earlier post) -- Tom Burton