0

Please excuse the beginner question. I couldn't find an appropriate answer in any Mathematica tutorial.

I am confused why a definition as a function or a definition in terms of a simple replacement produce different results. Consider this example (Mathematica 9 code):

In[397]:= ClearAll["Global`*"]

In[398]:= Test := 3 c^2 + d^4

In[399]:= v[f_] := D[f, c]

In[400]:= v[Test]

Out[400]= 6 c

The first definition of this simple derivative function "v" acting on a variable is fine. Defining a replacement Test = ... to replace the variable produces the expected result (It derives 3c^2+d^4 with respect to c and answers 6c).

However if I define a function instead of a simple replacement this does not work:

In[401]:= TestFunction[a_, b_] := 3 a^2 + b^4

In[403]:= vFunction[f_[a_, b_]] := D[f[a, b], a]

In[405]:= vFunction[TestFunction[a, b]]

Out[405]= \!\(
\*SubscriptBox[\(\[PartialD]\), \(3\ 
\*SuperscriptBox[\(a\), \(2\)]\)]\((3\ 
\*SuperscriptBox[\(a\), \(2\)] + 
\*SuperscriptBox[\(b\), \(4\)])\)\)

Why is that? I am risking to look like a moron here, but please enlighten me!

For your convenience, I uploaded a copy of my workbook here

Thanks a lot,

Michael

hivert
  • 10,579
  • 3
  • 31
  • 56
Michael
  • 11
  • 4
  • 1
    `TestFunction` gets evaluated to `3 a ... ` first which doesn't match the pattern `f_[a_ ..` Look up `HoldAll`, and take this to mathematica.stackexchange.com – agentp Feb 13 '14 at 13:04

1 Answers1

0

Do this instead

vFunction[f_,a_,b_]:=D[f[a,b],a];

and when you need derivatives simply use vFunction[TestFunction,a,b] to get it.

When you write down f[x], it means the evaluated value of f with argument value x. So, f[x] is technically not a function anymore. What you want as the argument of vFunction[] is the function TestFunction, not the evaluated value.

Xiaolei Zhu
  • 507
  • 4
  • 10