I am solving a nonlinear ODE using NDSolve, and one of the terms is an implicit nonlinear function of the dependent variable, for which I am using FindRoot. Here's a trivial example:

y[x_] := y /. FindRoot[Exp[Log[y]] == x, {y, 1}];

I can plot this function, and it gives a straight line y= x, but when I put the function into NDSolve, I get an error message.

s = NDSolve[{h'[t] == -y[h[t]], h[0] == 1}, h, {t, 0, 2}]

and then


If the function y[h[t]] were working, the solution would be h[t]=Exp[-t]. Does every term in NDSolve have to be an explicit function? Is there a way around this?

  • 1
    $\begingroup$ Functions that make use of numeric techniques should have their arguments restricted to numeric values: y[x_?NumericQ] := ... $\endgroup$
    – Bob Hanlon
    Jan 21, 2018 at 21:44
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Jan 21, 2018 at 22:31
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/18393/… $\endgroup$
    – Michael E2
    Jan 21, 2018 at 22:45
  • $\begingroup$ Why not use NDSolve[{Exp[Log[-h'[t]]] == h[t], h[0]==1}, h, {t, 0, 2}] instead? $\endgroup$
    – Carl Woll
    Jan 22, 2018 at 16:29

1 Answer 1


Simply use ?NumericQ an it works

y[x_?NumericQ] := y /. FindRoot[Exp[Log[y]] == x, {y, 1}];

s = First@NDSolve[{h'[t] == -y[h[t]], h[0] == 1}, h, {t, 0, 2}]

Plot[Evaluate[h[t] /. s], {t, 0, 2}]

Oh, I just see, @Bob Hanlon had the same advice before.


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