# How to define a function

How can I define a function which depends on a function of x? Something like this

f[u[x]_,u[x]']= u[x]' + a [x] u[x] +b[x]


I think I wasn't enaugh clear. If I define this function:

f[x_]=x^2


and I put x = a + b I obtain:

f[a + b] // Expand

a^2 + b^2 + 2ab


Now I want to define something similar for a function:

f[u[x]_, x] = D[u[x], x] + a[x]u[x]


And for I put this in input

 f[z[x]u[x], x]


I want to obtain for the output

z[x]'u[x]+z[x]u[x]'+a[x]z[x]u[x]


Is it possible? Or I can only define the differential operator?:

diff1 = D[#, x] + a[x]# &

• something like f[u_[x_]] := x, but defining a function that takes as a second argument the derivative of the first is more involved. – acl Feb 28 '15 at 19:12
• a more detailed example of what you are trying to do might be helpful – george2079 Feb 28 '15 at 19:47
• @ame_math Consider the definition f[g_, x_] := g[x] + g'[x]. On calling f you need to use the function g as a pure function. Expample: f[Cos[#]&,x] (* Cos[x] - Sin[x] *) – Dr. Wolfgang Hintze Feb 28 '15 at 21:16
• If you define f as f[u_, x_] := D[u[x], x] + a[x] u[x] and evaluate f[u[#] z[#] &, x], you will get a[x] u[x] z[x] + z[x] u′[x] + u[x] z′[x], which I believe is what you are looking for. – m_goldberg Mar 1 '15 at 0:36

This works:

f[u_, x_] := D[u, x] + a[x] u


By way of explanation, everything is an expression, and there is nothing particularly special about functions. You and I know that this definition doesn't have lot of meaning for objects "u" that aren't functions, but Mathematica doesn't need to know that u is a function.

On the assumption that you have defined the functions u[x], a[x] and b[x] elsewhere, you can define a function as follows:

f[x_] := u'[x] + a[x] u[x] + b[x]


However, I recommend you read through the documentation on defining functions.

• And if I defined only a[x] and not u[x] and u[x]', I suppose that u[x] is an rbitrary function, it could be also a product of functions u[x]=z[x]u[x]. – ame_math Feb 28 '15 at 18:00
• The question is not entirely clear to me yet, either, but perhaps this is what you meant: f[u_, x_] := D[u, {x, 1}] + a[x] D[u, {x, 0}] + b[x] – TransferOrbit Feb 28 '15 at 20:16

I am still not sure of what you want but attempting to be helpful:

f[u[x] p_, x] := D[u[x] p, x] + a[x] u[x] p

f[z[x] u[x], x]

a[x] u[x] z[x] + z[x] Derivative[1][u][x] + u[x] Derivative[1][z][x]


Which formats as:

$a(x) u(x) z(x)+z(x) u'(x)+u(x) z'(x)$