Re: Release question
- To: mathgroup at smc.vnet.net
- Subject: [mg9410] Re: [mg9366] Release question
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Wed, 5 Nov 1997 01:56:28 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Sat, 1 Nov 1997 03:33:52 -0500 Peter <psalzman at landau.ucdavis.edu> [mg9366] Release question writes > I was trying to Plot: > > Plot[ {V[x], Phi[x] /. min}, {x,-5,5} ] > > where min was a set of rules: > > min = {a->.5, b->.005} > > Mathematica was literally trying to plot "Phi /. min", which of > course is meaningless. > > Thumbing through a book I have, I stumbled across the Release > command. > This did work: > > Plot[ Release[{V[x], Phi[x] /. min}], {x,-5,5}] > > I couldn't quite understand the explanation why Release is > necessary.Even though my problem is solved, can someone explain > precisely why Release is necessary here and what it does? Peter: It looks to me as if Plot[ {V[x], Phi[x] /. min}, {x,-5,5} ] this should compute OK -- see PROBLEM 1 below and its solution. I'll try to explain what is going on. Define In[1]:= f= x^3; g=-x^3; (1) Evaluation of Plot[f,{x,0,1}] Plot has the attribute HoldAll, it does not evaluate its elements f and {x,a,b} in the standard way but according to its own special rules: each point on the line that is displayed is calculated by assigning a value xv to x and then constructing {xv, f}. If f, with x = xv, is not a real number then this fails, and generates a message. (2) Evaluation of Plot[{f,g},{x,0,1}] It looks as if this would fail because , with x = xv, {f,g} would be a list, not a real number. However, before evaluating f and g, the list {f,g} is recognised and Mathematica essentially plots Plot[f,{x,0,1}] and Plot[g,{x,0,1}] without showing them, and then shows them together. (Actually, the computations takes place with the variable x localised, so that any existing value for x does not affect the result and the calculation does not give any outside x a value). Here are some problems: PROBLEM 1: In[2]:= Plot[D[f,x],{x,0,1}]; (*fails*) EXPLANATION In[3]:= x=.2;D[f,x] Out[3]= D[0.008, 0.2] does not evaluate to a real number. In[4] x=. (*clear x*) SOLUTION In[5]:= Plot[Evaluate[D[f,x]],{x,0,1}]; EXPLANATION: D[f,x] is forced to evaluate immediately so that we are computing Plot[3x^2,{x,0,1}] PROBLEM 2: In[6]:= rl = a->2; In[7]:= Plot[{f,a g}/.rl,{x,0,1}]; (*fails*) EXPLANATION: The unevaluated {f,a g}/.a->2 is not a list and so is not split In[8]:= x=.2; {f, a g}/.rl Out[8]= {0.008, -0.016} is not a real number. In[9]:= x=. SOLUTION In[10]:= Plot[{f,a g/.a->2},{x,0,1}]; PROBLEM 3 In[11]:= fg := {f,g}; In[12]:= Plot[fg,{x,0,1}]; (*fails*) EXPLANATION Unevaluated fg is not a list so no splitting occurs In[13]:= x=.2; fg Out[13]= {0.008, -0.008} is not a real number In[14]:= x=. SOLUTION In[15]:= Plot[Evaluate[fg],{x,0,1}]; EXPLANATION: fg is forced to evaluate immediately, so that we are computing Plot[{f,g},{x,0,1}] **** Why have this HoldAll feature? **** Here is an example in its favour (from Cameron Smith & Nancy Blachman, The Mathematica Graphics Guidebook) In[16]:= h[x_/;x<0]:= -1 In[17]:= h[x_]:= 1 In[18]:= Plot[h[x],{x,-1,1}]; If we force h[x] to evaluate immediately then it becomes simply 1 In[19]:= Plot[Evaluate[h[x]],{x,-1,1}]; Allan Hayes hay at haystack.demon.co.uk http://www.haystack.demon.co.uk/training.html voice:+44 (0)116 2714198 fax: +44 (0)116 2718642 Leicester, UK