Re: Differentiation and evaluation of function
- To: mathgroup at smc.vnet.net
- Subject: [mg75067] Re: Differentiation and evaluation of function
- From: "Norbert Marxer" <marxer at mec.li>
- Date: Mon, 16 Apr 2007 04:11:38 -0400 (EDT)
- References: <evsqb8$qf$1@smc.vnet.net>
On 15 Apr., 11:13, "Apostolos E. A. S. Evangelopoulos" <a.e.a.evangelopou... at sms.ed.ac.uk> 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 Hello The problem is with the expression y[x_]:= D[f[x], x]. The call y[3] leads to D[f[3], 3] and your error message. There are many ways to make things work. 1. You can use Set (=) instead of SetDelayed (:=): Clear[f, y, x]; f[x_] = 3 x^2 ; y[x_] = D[f[x], x]; 2. You can use a dummy variable for x in the definition of y: Clear[f, y, x, dummy]; f[x_] := 3 x^2 ; y[x_] := D[f[dummy], dummy] /. {dummy -> x}; 3. You can use pure functions Clear[f, y]; f = (3 #^2 ) &; y = Derivative[1][f]; In all these cases the expression {f[x], f[3], y[x], y[3]} will return: {3*x^2, 27, 6*x, 18} Best Regards Norbert Marxer