2
$\begingroup$

I have a simple problem. First I define the value of variables, then I calculate the expression as the following:

Code 1:

a = 10^-6; 
b = 10^-3;
c = 1;
d = 0.1;
s = -d Sqrt[b^2 + c^2];

Integrate[(w E^(-w/a))/(w - s), {w, 0, ∞}]
-3.744368975478385*10^43415

As you see the result is so large. But if I manually insert the value of s (-0.1) into the expression, I would get zero, which is is close to the correct answer:

Code 2:

a = 10^-6; 
b = 10^-3;
c = 1;
d = 0.1;
s = -d Sqrt[b^2 + c^2];

Integrate[(w E^(-w/a))/(w + 0.1), {w, 0, ∞}]
0.

What is the problem with the first code?

$\endgroup$
4
  • 1
    $\begingroup$ Have you tried using exact numbers instead of approximate ones? In other words, does the result of your first calculation change if you set d = 1/10 instead? Also, the exact value of s is very close to $-0.1$, but not exactly equal to that value. Why do you think yours is a valid substitution? $\endgroup$
    – MarcoB
    Jan 26, 2016 at 6:35
  • $\begingroup$ Yes. If I use d=1/10 instead, the result would be different. It is strange, because the result is 9.99979*10^-12, not zero as code 2. I know that 0.1 is different from 1/10, but it is improbable that their difference leads to a number of the order of 10^43415 (code 1). Moreover, I substitute the mathematica value of s into code 2. I still don't understand why the result of definition (code 1) is very different from the result of manual substitution (code 2). $\endgroup$ Jan 26, 2016 at 7:05
  • 2
    $\begingroup$ But the numerical zero does not have to be the right answer. The number you get for d = 1/10 is fine. In code 2 may be the integration to infinity gets a different value for s=-0.1, which is not the number for d=1/10 (as already pointed out by MarcoB). $\endgroup$ Jan 26, 2016 at 7:09
  • $\begingroup$ While the handling of inexact input by Mathematica's exact solvers has improved recently, I feel one should still use caution when doing it. Use NIntegrate, or if possible, solve exactly before plugging in inexact parameters (e.g., this works in the present case Block[{s}, Integrate[(w E^(-w/a))/(w - s), {w, 0, \[Infinity]}, Assumptions -> s < 0]]). $\endgroup$
    – Michael E2
    Jan 27, 2016 at 12:42

2 Answers 2

2
$\begingroup$

In your inputs

a = 10^-6; 
b = 10^-3;
c = 1;
d = 0.1;
s = -d Sqrt[b^2 + c^2]

-0.1

this result is approximated for display. You can see the complete result by placing your cursor in front of the -0.1 and pressing the space bar.

Alternatively

InputForm[s]

-0.1000000499999875

Edit

With s = -d Sqrt[b^2 + c^2] the integral calculation yields ...

in Mathematica 7.0.1.0 : 9.99979500615538*10^-12

in Mathematica 10.3.1 : -3.744368975619640*10^43415

$\endgroup$
6
  • $\begingroup$ Yes. That 's right. But this tiny difference would result in a difference of the order of 10^43415 in the final result? $\endgroup$ Jan 26, 2016 at 13:41
  • $\begingroup$ In Mathematica 7.0.1.0 the result is 9.99979500615538*10^-12. However, with s = -0.1 the result is 9.999800005898997*10^-12 which is 4.99974*10^-18 more. $\endgroup$ Jan 26, 2016 at 13:47
  • $\begingroup$ No. For d=0.1, I get -3.7*10^43415. Strangely, for d=1/10, I get your result, which is 9.9998*10^-12. Why such a difference in the result? $\endgroup$ Jan 26, 2016 at 14:04
  • $\begingroup$ On Mathematica 10.3.1 the result is -3.744368975619640*10^43415 but not on Mathematica 7.0.1.0 (both running on Windows 10). $\endgroup$ Jan 26, 2016 at 14:07
  • $\begingroup$ @ChirsDegnen Thanks. I have a bunch of expressions. I can't substitute the variables manually. It seems that I should go to Mathematica 7. $\endgroup$ Jan 26, 2016 at 14:23
2
$\begingroup$

To sum up what we have been saying in comments, it may sometimes be dangerous to use symbolic solvers (e.g. Integrate) with inexact input (e.g. $d=0.1$). It is better in your case to evaluate the integral symbolically, and then calculate the approximate numerical value:

a = 10^-6;
b = 10^-3;
c = 1;
s = -d Sqrt[b^2 + c^2];
d = 1/10;

Integrate[(w E^(-w/a))/(w - s), {w, 0, Infinity}]

Mathematica graphics

N[%]
(* Out: 9.99979*10^-12 *)

The result is of course very close to zero, but not exactly zero. The problem doesn't lie in how close $s$ is to $-0.1$, but in whether you use exact or approximate input to Integrate, which the function does not handle gracefully in this case.

$\endgroup$
1
  • $\begingroup$ +1 Also works with NIntegrate[(w E^(-w/a))/(w - s), {w, 0, Infinity}, AccuracyGoal -> 5] $\endgroup$ Jan 28, 2016 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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