Message Boards Message Boards

0
|
7712 Views
|
4 Replies
|
4 Total Likes
View groups...
Share
Share this post:

obscure bug using FindRoot[ Integrate[...

Posted 10 years ago

There is a minor bug in Mathematica 10.0.2 when using FindRoot to find a limit of integration yielding a given integral value when the integrand contains a Dirac delta function. In this case the syntax FindRoot[f[x]==a,{x,x0}] does not work but FindRoot[f[x]-a,{x,x0}] does. The code below is an illustration of the bug.

Clear["Global`*"]
f[x_] = DiracDelta[x] + Exp[-x];

t = FindRoot[Integrate[f[x], {x, z, \[Infinity]}] == .5, {z, 1}]

FindRoot::nlnum: The function value {False} is not a list of numbers with dimensions {1} at {z} = {1.}. >>

FindRoot::nlnum: The function value {False} is not a list of numbers with dimensions {1} at {z} = {1.}. >>

FindRoot[\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(z\), \(\[Infinity]\)]\(f[
     x] \[DifferentialD]x\)\) == 0.5, {z, 1}]

t = FindRoot[Integrate[f[x], {x, z, \[Infinity]}] - .5, {z, 1}]

{z -> 0.693147}

t = FindRoot[Integrate[f[x], {x, 0, z}] == 1.5, {z, 1}]

FindRoot::nlnum: The function value {False} is not a list of numbers with dimensions {1} at {z} = {1.}. >>

FindRoot::nlnum: The function value {False} is not a list of numbers with dimensions {1} at {z} = {1.}. >>

FindRoot[\!\(
\*SubsuperscriptBox[\(\[Integral]\), \(0\), \(z\)]\(f[
     x] \[DifferentialD]x\)\) == 1.5, {z, 1}]

t = FindRoot[Integrate[f[x], {x, 0, z}] - 1.5, {z, 1}]

{z -> 0.693147}
POSTED BY: Mike Luntz
4 Replies
Posted 10 years ago

Many thanks to all who replied. There were a number of great suggestions in the replies.

POSTED BY: Mike Luntz

At one of the technologies conferences, Stephen Wolfram talked about programming style, pointing out that breaking up code into small pieces can make it much easier to work with and debug. I think this problem is an example of that. Defining the integral as a function and testing that would have made the problem more easily understandable.

POSTED BY: Frank Kampas

Hello, Adding to these comments, this also illustrates those cases where NIntegrate needs to have a function with numeric argument:

expr[z_?NumericQ] := Integrate[f[x], {x, z, \[Infinity]}]
FindRoot[expr[z] == 0.5, {z, 1}]

However, if this was the problem that the original poster had in mind, then why not?

g = Integrate[DiracDelta[x] + Exp[-x], {x, z, \[Infinity]}, Assumptions -> Element[z, Reals]]
FindRoot[g == 0.5, {z, 1}

This avoids repeated calls to Integrate.

POSTED BY: W. Craig Carter

Not really a bug, notice Integrate returns a ConditionalExpression, and it is documented that ConditionalExpression propagates, i.e. ConditionalExpression[g[z], ...] == 0.5 becomes ConditionalExpression[g[z] == 0.5, ...] which returns False when z is 1 and of course False is not a number as the message says.

There are many ways around this, one could use Normal to remove the ConditionalExpression, or use Assumptions:

FindRoot[Integrate[f[x], {x, z, Infinity}, Assumptions -> Element[z, Reals]] == 0.5, {z, 1}]
POSTED BY: Ilian Gachevski
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract