# Where to insert the definition of a function in mathematica:

The whole procedure is much more complicated but the heart of the problem is the following. The variables are h and x. I define the function m containing h,x and the derivative of another function p:

m[h_, x_] := u[h, x, D[p[h, x], x]] + x


I need the derivatives of m:

mh[h_, x_] := D[m[h, x], h];
mx[h_, x_] := D[m[h, x], x];


From the derivatives of m I solve for the second derivatives of p:

solution = Solve[{mh[h, x] == 0, mx[h, x] == 0}, {D[p[h, x], h, x], D[p[h, x], x, x]}];
phx[h_, x_] := solution[[1]][[1, 2]]
pxx[h_, x_] := solution[[1]][[2, 2]]


Finally I need the expression:

expression[h_, x_] := phx[h, x] pxx[h, x]


AT THIS POINT I write explicitly the dependence of u in terms of h, x and the derivative of p:

u[h, x, D[p[h, x], x]] = h D[p[h, x], x]^2;


mh[h,x] gives the expected result:

mh[h, x] p^(0,1)(h,x)^2+2 h p^(1,1)(h,x) p^(0,1)(h,x)


However, the expression[h, x] gives me an unexpected result:

 -(((-u^(0,1,0)(h,x,p^(0,1)(h,x))-1) u^(1,0,0)(h,x,p^(0,1)(h,x)))/u^(0,0,1)(h,x,p^(0,1)(h,x))^2)


I do not see the reason. Thanks for any help!

Clear["Global*"]

m[h_, x_] := u[h, x, D[p[h, x], x]] + x
mh[h_, x_] := D[m[h, x], h];
mx[h_, x_] := D[m[h, x], x];

solution =
Solve[{mh[h, x] == 0, mx[h, x] == 0}, {D[p[h, x], h, x],
D[p[h, x], x, x]}];

phx[h_, x_] := solution[[1]][[1, 2]]
pxx[h_, x_] := solution[[1]][[2, 2]]

expression[h_, x_] := phx[h, x] pxx[h, x]


Replace u with a pure function

repl = u -> (#1*#3^2 &);

mh[h, x] /. repl

(* Derivative[0, 1][p][h, x]^2 +
2*h*Derivative[0, 1][p][h, x]*Derivative[1, 1][p][h, x] *)

expression[h, x] /. repl

(* 1/(4 h^2) *)
`