# Using FindRoot as a nonlinear function inside NDSolve [duplicate]

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

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


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?

• Functions that make use of numeric techniques should have their arguments restricted to numeric values: y[x_?NumericQ] := ... Jan 21, 2018 at 21:44
• 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! Jan 21, 2018 at 22:31
• Jan 21, 2018 at 22:45
• Why not use NDSolve[{Exp[Log[-h'[t]]] == h[t], h[0]==1}, h, {t, 0, 2}] instead? Jan 22, 2018 at 16:29

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}]