I'm having trouble getting Mathematica to solve equations numerically. I know that its important to specify the type of variables for pattern testing (see e.g. here) but this doesn't seem to work. Consider the following example:
f[a_?NumericQ] := NIntegrate[a x, {x, 0, 1}]
NSolve[f[0.5 + b] == 1, b]
NSolve[f[0.5 + b] == 1, b, Reals]
NSolve[f[0.5 + b] == 1 && 0 < b < 3, b]
The first one is evaluated (according to a warning message using inverse functions) but the second and third are returned unevaluated. How can I get these to run?
FindRoot
andFindInstance
work in place ofNSolve
with the extra conditions. Of course, they won't find all solutions automatically, so this is still interesting. $\endgroup$NSolve
worked at all in any of those cases! In my understanding,NSolve
is tailored towards explicitly linear and polynomial equations. I don't really know what it does when confronted with the numerical functionf
, whose structure is entirely unknown. I suspect thatNSolve
switches solution method when constraints are present, and those methods simply don't know how to deal with thef
"black box". $\endgroup$Trace
them, you always see the sequence{f[0.5 +b], {NumericQ[0.5 +b], False}, f[0.5 +b]}, f[0.5 +b]==1
, but for some reasonNSolve
continues on the first one. $\endgroup$InverseFunction[f][1.]
. ApparentlyNSolve
will try inverse functions, but only for (presumed) analytic functions. It does seem like an fairly elementary step. $\endgroup$