The order of the expressions matters. Defining`Equation2` before defining `g` yields Equation2 = f''[x] + 3*(a'[x]/a[x])*f'[x] + D[g[f[x]], f[x]] (* (3*Derivative[1][a][x]*Derivative[1][f][x])/a[x] + Derivative[1][g][f[x]] + Derivative[2][f][x] *} Note that `D[g[f[x]], f[x]]` has been converted to `Derivative[1][g][f[x]]`. So, while D[g[f[x]], f[x]] (* Piecewise[{{2*f[x], a2 - x < 0 || (a2 - x <= 0 && a1 - x > 0) || (a1 - x >= 0 && x - x0 >= 0)}, {-n, a1 - x <= 0 && a2 - x >= 0}}, 0] *) as desired, Derivative[1][g][f[x]] (* Derivative[1][g][f[x]] *) returns unevaluated, which looks like an additional function to `NDSolve`. To avoid this problem, define `g[x]` before defining `Equation2`. Also, remember to Clear[Equation1, Equation2] before starting.