Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Differentiation and evaluation of function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75061] Re: Differentiation and evaluation of function
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Mon, 16 Apr 2007 04:08:32 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <evsqb8$qf$1@smc.vnet.net>

Apostolos E. A. S. Evangelopoulos wrote:
> In spite of this problem's apparent simplicity, I cannot find an answer to what goes wrong:
> I begin by declaring a function f[x_] and defining its derivative function y[x_], then ask for an evaluation of 'y' for two simple arguments, one being the variable 'x,' the other a number. Evaluation of y['number'] is impossible and implies that there is something inherently different about the functions 'f' and 'y,' since f['number'] is not a problem, while y['number'] is! My inputs/outputs, in particular, are the following:
> 
> In[1]:= f[x_]:=3 x^2
> In[2]:= ?f
>         Global`f
>         f[x_]:=3 x^2
> In[3]:= y[x_]:= D[f[x], x]
> In[4]:= ?y
>         Global`y
>         y[x_]:='partial d sub[x]' f[x]
> In[5]:= f[3]
> Out[5]:= 27
> In[6]:= y[x]
> Out[6]:= 6 x
> In[7]:= y[3]
>         General::ivar : 3 is not a valid variable
> 
> Everything above seems reasonable with the exception of the last input. This feels even more confusing if one thinks that the general method has worked for me, yet with much more complicated functions, for which Mathematica has no problem perfoming both symbolic and numerical evaluations!
> 
> I hope someone can enlighten this mystery!
> 
> Many thanks,
> Apostolos
> 

The definition of y is evaluated each time you call it since you defined 
it with *SetDelayed*. Mathematica evaluation process (in a very short 
summary) proceeds as follows. Say you call y[t]. Mathematica looks in 
its internal tables and see that there is a match for such an expression 
with y[x_] (x_ means any pattern). Then it looks at the associated 
definition for y, which is D[y[x], x] and replaces the pattern (formal 
argument) x by the actual argument t, returning D[y[t], t]. Only then 
the actual differentiation is performed, yielding 6*t as final result.

Now if you feed y with a numeric argument, say 3, the same process is 
followed. Mathematica finds a match for y[3] with y[x_] and replace x by 
3 in the right-hand side of y, which results in the expression D[y[3], 
3]. The next step is to evaluate this expression, expression that does 
not make sense since any symbolic variables have been discarded.

To solve the issue, you could either use an immediate *Set* to defined y 
or a transformation rule to guarantee than when fed with a numeric value 
y performed first the symbolic differentiation and then replace the 
formal symbolic argument by its actual numeric value.

In[1]:=
f[x_] := 3*x^2

In[2]:=
y[x_] = D[f[x], x]

Out[2]=
6*x

In[3]:=
y[x]

Out[3]=
6*x

In[4]:=
y[3]

Out[4]=
18

In[5]:=
z[mydummyargument_] := D[f[x], x] /. x -> mydummyargument

In[6]:=
z[x]

Out[6]=
6*x

In[7]:=
z[3]

Out[7]=
18

Regards,
Jean-Marc


  • Prev by Date: Re: Differentiation and evaluation of function
  • Next by Date: neat way to program minimum of sum
  • Previous by thread: Re: Differentiation and evaluation of function
  • Next by thread: Re: Differentiation and evaluation of function