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

MathGroup Archive 1998

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

Search the Archive

Re: Derivatives D[ ] as Functions inside Tables; Need Help!

  • To: mathgroup at smc.vnet.net
  • Subject: [mg13210] Re: Derivatives D[ ] as Functions inside Tables; Need Help!
  • From: Tobias Oed <tobias at physics.odu.edu>
  • Date: Mon, 13 Jul 1998 07:42:58 -0400
  • Organization: Old Dominion University
  • References: <6nsil0$f32@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

AES wrote:
> 
> I'm trying to define a function f1[a,x] and its derivative f2[a,x],
> where from my point of view x is the independent variable and   "a" is
> a parameter I'll change on different runs.  So I write, as a simple
> example:
> 
>    Remove["Global`*"]
> 
>    f1[a_,x_] :=a Cos[x] + a^2 Sin[x]
> 
>    f1[a,x]
> 
>    f2[a_,x_] := D[f1[a,x],x]
> 
>    f2[a,x]
> 
>    a=2
> 
>    f1[a,x]
> 
>    f2[a,x]
> 
>    Table[{x, f1[a,x], f2[a.x]} // N, {x,0,4}] // TableForm
> 
> and everything looks fine -- except the function f2[a,x] will not
> evaluate inside the Table[ ].  I don't understand this -- help in
> understanding will be much appreciated.
> 
>      siegman at ee.stanford.edu

You defined your f1 and f2 functions using using :=. This means  that
the right hand side of the definition is evaluated only when it is
definition is used. This is no problem for f1, but the  derivation of
f2 is not done:

In[1]:= f1[a_,x_] :=a Cos[x] + a^2 Sin[x]

In[2]:= f2[a_,x_] := D[f1[a,x],x]

In[3]:= ??f2
Global`f2

f2[a_, x_] := D[f1[a, x], x]

Everything looks nice when you call f2 in such a way that the 
derivation can be done once the arguments are substituted:
  
In[4]:= f2[b,z] //InputForm

Out[4]//InputForm= b^2*Cos[z] - b*Sin[z]

But when you give numerical arguments to your f2,  the derivation cannot
be done:

In[5]:= f2[3,7]

General::ivar: 7 is not a valid variable.

Out[5]= D[3 Cos[7] + 9 Sin[7], 7]

The solution is to use = instead of := in your definition of f2

In[6]:= f3[a_,x_] = D[f1[a,x],x] // InputForm

Out[6]//InputForm= a^2*Cos[x] - a*Sin[x]

so that

In[7]:= f3[3,7]

Out[7]//InputForm= 9*Cos[7] - 3*Sin[7]

and it also works in tables:

In[8]:= Table[{x, f1[2,x], f3[2,x]} // N, {x,0,4}] // TableForm

Out[8]//TableForm= 0    2.        4.

                   1.   4.44649   0.478267253856766

                   2.   2.8049    -3.483182199839933

                   3.   -1.4155   -4.242210002521516

                   4.   -4.3345   -1.100969492838591

Hop this helps Tobias.


  • Prev by Date: Re: Protection against some Front end problems and crashes
  • Next by Date: Re: coordinate transformation
  • Previous by thread: Re: Derivatives D[ ] as Functions inside Tables; Need Help!
  • Next by thread: Re: Derivatives D[ ] as Functions inside Tables; Need Help!