3
$\begingroup$

When I solve the following integral analytically and numerically the answers are different. Why? how can I get similar answers?

a = 10^-6; t = 10000; NIntegrate [E^(-w/ a) t, {w, 0, ∞}, MaxRecursion -> 300, AccuracyGoal -> 10] 

here the answer is $0$. But when I solve it analytically, as follows, the answer is $0.01$.

Integrate[E^(-w/a) t , {w, 0, ∞}] 
$\endgroup$
2
  • 1
    $\begingroup$ With[{a = 1*^-6, t = 10000}, NIntegrate[E^(-w/a) t, {w, 0, ∞}, WorkingPrecision -> 20]] works, tho. $\endgroup$ Commented Jul 13, 2016 at 15:38
  • $\begingroup$ NIntegrate[E^(-w/a) t, {w, 0, \[Infinity]}, Method -> "LocalAdaptive"] correct answer, no messages. $\endgroup$ Commented Jul 13, 2016 at 15:40

2 Answers 2

2
$\begingroup$

Of course in this case you can trust the symbolic result of Integrate, but the point raised by the question becomes especially important when there is no analytical solution.

Here is an example suffering from an even more rapid decay that causes the same numerical problems. Finding the right options for NIntegrate isn't so obvious:

a = 10^6; integrand = E^(- a w^w ); Integrate[integrand, {w, 0, Infinity}] (* returns unevaluated because no symbolic solution exists *) NIntegrate[integrand, {w, 0, Infinity}] (* ==> 0., with warning message *) NIntegrate[integrand, {w, 0, Infinity}, Method -> "LocalAdaptive"] (* ==> 0. *) 

None of these results are what we want.

What I would suggest is to try symbolic integration first, but then automatically fall back to numerical integration if that fails. As explained in the documentation for Integrate (under "Scope > Basic Usage"), this can be done by simply wrapping Integrate in N. The advantage for our problem is then that N allows a second argument for the desired precision:

N[Integrate[integrand, {w, 0, Infinity}], 10] (* ==> 2.233104982*10^-300622 *) 

Here, I get a numerical result without having to think about the choice of integration method.

$\endgroup$
3
$\begingroup$

Most of the value of the integral occurs before $w\le 0.00002$.

NIntegrate[Exp[-w/a] t, {w, 0, 0.00002}, WorkingPrecision -> 16] 

0.009999999979388476

At MachinePrecision, the total value of the integral can be found at $w\le 0.1$.

NIntegrate[Exp[-w/a] t , {w, 0, 0.1}] 

0.01

It is therefore not just a question of MaxRecursion:

NIntegrate[Exp[-w/a] t , {w, 0, \[Infinity]}, MinRecursion -> 10, MaxRecursion -> 30] 

Provides the answer as well, but Mathematica needs to know that it is not just integrating a horizontal line at zero, after all: $e^{-0.1/a}\approx3.563*10^{-43430}$. Hence the error message.

Integral and error estimates are 0 on all integration subregions. Try increasing the value of the MinRecursion option. If value of integral may be 0, specify a finite value for the AccuracyGoal option. >>

$\endgroup$
2
  • $\begingroup$ Using numerical integration all the way to infinity is suspect anyway. Why do it at all? $\endgroup$ Commented Jul 13, 2016 at 16:09
  • $\begingroup$ @rhomboid, because there are in fact numerical methods for dealing with improper integrals; this is an active area of research. $\endgroup$ Commented Jul 13, 2016 at 16:18

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.