[Date Index]
[Thread Index]
[Author Index]
Re: Derivatives D[ ] as Functions: Summary (LONGISH)
*To*: mathgroup at smc.vnet.net
*Subject*: [mg13253] Re: Derivatives D[ ] as Functions: Summary (LONGISH)
*From*: siegman at ee.stanford.edu (AES)
*Date*: Fri, 17 Jul 1998 03:17:47 -0400
*Organization*: Stanford University
*References*: <6nsil0$f32@smc.vnet.net>
*Sender*: owner-wri-mathgroup at wolfram.com
I recently posed the question of defining a function f2 as the
derivative of another function f1, e.g.
Remove["Global`*"];
f1[a_,x_] :=a Cos[x] + a^2 Sin[x];
f2[a_,x_] := D[f1[a,x],x];
and then finding that f2 would not behave "as expected" in a subsequent
Plot[ ] or Table[ ].
Thanks to several people for their responses, and apologies for my
several typos in earlier postings. It's clear what my difficulty was.
However, let's look at the general issue a little further. If for
example we do the above and then look at the FullForm version of the
two functions, they look very similar:
f1[a,x]//FullForm
Plus[Times[a,Cos[x]],Times[Power[a,2],Sin[x]]]
f2[a,x] // FullForm
Plus[Times[Power[a,2],Cos[x]],Times[-1,a,Sin[x]]]
Note that these FullForm results, if not misleading, certainly give no
clue that f2 has a different character than f1 and will behave totally
differently if you try to use it in Plot[ ] or Table[ ] .
A general and frequent and serious problem with Mathematica, in fact,
is that it often does things that may be totally logical and even
necessary by its rules, but that can be mysterious, non-intuitive, and
frustrating to ordinary users; and the nature of the beast often makes
these problems obscure and hard to find.
[As another example of Mathematica's penchant for obscure difficulties,
note that if you follow the usual practice that you are allowed to use
for (all? most?) other compound expressions and include the "Remove[
]" expression in the same cell as the two function definitions above,
the two definitions will be Removed, even though they come AFTER the
Remove[ ]. Tell me why this makes sense? -- not what combination of
rules makes Remove[ ] function this way, but why setting it up in this
way makes sense? Also, tell me where you're warned about this in the
Mathematica Book?]
[Another general observation is that when problems -- or anyway
"apparent problems" -- with Mathematica's ways of doing things are
pointed out, I find it's much more common to get a detailed explanation
of why Mathematica does what it does, rather than any discussion of the
design choices, or design necessities, that make it necessary -- if it
is necessary -- for Mathematica to function in this way.]
In any event one way to avoid the derivative problem above is to use an
immediate definition, i.e., use " = " instead of " := "in defining f2:
f1[a_,x_] :=a Cos[x] + a^2 Sin[x];
f2[a_,x_] = D[f1[a,x],x];
That's fine -- except, suppose you do this deep in a notebook in which
"a" has already been given a value; you may not get what you want, or
think you're getting..
Several people also suggested using Evaluate[f2[a,x]], or more complex
tricks, every time you use f2 subsequently in a Plot[ ] or Table[ ],
and that of course works.
However, so far as I can see the preferred way to accomplish this
would seem to be to do it globally, i.e., to write:
f1[a_,x_] := a Cos[x] + a^2 Sin[x];
f2[a_,x_] := Evaluate[ D[f1[a,x],x]];
That's reasonably easy to remember, somewhat intuitive (to me anyway),
seems to meet the need, and what I propose to do from now on. But, are
there still hidden pitfalls in this? Further comments welcome.
--AES
Prev by Date:
**Re: Problems with numerical precision**
Next by Date:
**Re: 3DGraphics color problem**
Previous by thread:
**Comparison of Mathematica on Various Computers**
Next by thread:
**Re: Re: Derivatives D[ ] as Functions: Summary (LONGISH)**
| |