3
$\begingroup$

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?

$\endgroup$
4
  • 1
    $\begingroup$ Both FindRoot and FindInstance work in place of NSolve with the extra conditions. Of course, they won't find all solutions automatically, so this is still interesting. $\endgroup$
    – march
    Commented Sep 1, 2015 at 19:41
  • 1
    $\begingroup$ Honestly I am surprised that 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 function f, whose structure is entirely unknown. I suspect that NSolve switches solution method when constraints are present, and those methods simply don't know how to deal with the f "black box". $\endgroup$
    – MarcoB
    Commented Sep 1, 2015 at 23:06
  • $\begingroup$ @MarcoB. Yes it's strange: if you 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 reason NSolve continues on the first one. $\endgroup$
    – march
    Commented Sep 1, 2015 at 23:46
  • 2
    $\begingroup$ @MarcoB, march, try InverseFunction[f][1.]. Apparently NSolve will try inverse functions, but only for (presumed) analytic functions. It does seem like an fairly elementary step. $\endgroup$
    – Michael E2
    Commented Sep 2, 2015 at 1:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.