Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I know a similar question is answered somewhere, but it doesn't work for my case for some reason. I have the following problem in Mathematica. I want to compute f[h[x]]= x^2 + D[h[x],x]. Here, h[x] can be some function of x, say Sin[x]. I have written the following code in Mathematica,

SetAttributes[{f, D}, HoldAll]
f[h_, x_] := x^2 + D[h[x], x]
h := Sin[x]

But then the output of

f[h, 10]

is an error "General::ivar: 10 is not a valid variable."

Any better way of doing the composition of functions in Mathematica?

  • Sarah
share|improve this question
    
@b.gatessucks, Thanks. But I was aiming for some generic function rather than only Sin. In particular, the actual h[x] in my case is a quite complicated function. –  John May 20 at 18:41
    
You mean, f[h[#] &, x_] := x^2 + D[h[x], x] h[x_] := Sin[x] ? In that case, f[h[10], 10] just returns f[h[10], 10]. –  John May 20 at 18:47

2 Answers 2

You can define :

f2[h_, x_] := x^2 + Derivative[1][h][x]

Then :

(* Specific function, generic x *)
h1[x_] = Sin[x];
f2[h1, x]
(* x^2 + Cos[x] *)     

(* Generic function, numeric x *)
f2[h, 10]
(* 100 + Derivative[1][h][10]*)

(* Generic function, generic x *)
f2[g, x]
(* x^2 + Derivative[1][g][x] *)
share|improve this answer

The advantage of Derivative is that it can derivative at a particular value or derivative in general. In most cases, you can define a function like this

myD[f_, lst_List, x0_List] := Derivative[Sequence @@ lst][f][Sequence @@ x0]

For example, if the function is defined as

f[x_, y_] := Sin[Cos[x + y]]

then myD[f, {1, 1}, {a, b}] gives

-Cos[a + b] Cos[Cos[a + b]] - Sin[a + b]^2 Sin[Cos[a + b]]

However, the cons is you can't put the whole function body into myD, i.e. Sin[Cos[x + y]] in this case. myD[Sin[Cos[x + y]], {1, 1}, {a, b}] will not work as expected. In other words, you have to define the function first, and then put this function without parameters into myD.

Another option is to use D with ReplaceAll.

myD2[f_, {x_Symbol, n_Integer /; n > 0}] := D[f, {x, n}];    
myD2[f_, {x_Symbol, a_, n_Integer /; n > 0}] := D[f, {x, n}] /. x -> a;

Conversely, you have to put the function with parameters into myD2:

myD2[f[x, y], {y, 1}]

returns

-Cos[Cos[x + y]] Sin[x + y]

as expected. While, myD2[f, {y, 1}] gives 0.

I myself prefer the latter option, because it can also differentiate the function body directly.

myD2[Sin[Cos[x + y]], {y, 1}]

will also give -Cos[Cos[x + y]] Sin[x + y].

To use Derivative, one have to extract the function without parameters first, then Derivative on it, then act them onto the parameters. That's too difficult and complicate for me.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.