Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem where I am using FindRoot a gazillion times over a grid of parameters. I need to allow the starting values to vary a bit with the parameters to get it to converge. Here is a simple example in the spirit of my much bigger problem:

f[a_] := x /. FindRoot[x^2 - 1 == a, {x, a - 1, a + 1}]
NIntegrate[f[z], {z, 0, 5}]

NIntegrate actually gives an answer (I think it's even right), but it also gives the following errors:

FindRoot::srect: Value -1.+z in search specification {x,z-1,z+1} is not a number or array of numbers. >>

ReplaceAll::reps: {FindRoot[x^2-1==z,{x,z-1,z+1}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

It looks like it doesn't like treating the starting values for x as variables. For some reason, the actual numbers between 0 and 5 in the integral are not being passed to the starting values in FindRoot. Any suggestions?

share|improve this question

Not sure about what you're trying to do, but this doesn't bring up errors:

f[a_?NumericQ] := x /. FindRoot[x^2 - 1 == a, {x, a - 1, a + 1}]
NIntegrate[f[z], {z, 0, 5}]

-> 9.13129


The result is (of course) equivalent to

Integrate[Sqrt[1 + z], {z, 0, 5}]

-> -(2/3) + 4 Sqrt[6]
share|improve this answer
Thanks for the suggestion. I don't know the syntax a_?NumericQ, so I'm showing my limited knowledge of the program, but I'll look it up and give it a try. My problem involves integrating over functions of the normal PDF, which has no closed form, so I definitely need the numerical routine. – Dan May 6 '13 at 0:35
belisarius, This worked. I've got a few hundred thousand solutions to FindRoot with starting values that a funciton of a subset of the parameters. This ensures that they are in regions that generate solutions. Then I've got a few hundred thousand numerical integrals of functions with the FindRoot solutions embeded, all solving nicely. Thanks. – Dan May 6 '13 at 17:06
@Dan Glad to hear that – Dr. belisarius May 6 '13 at 17:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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