1
$\begingroup$

I was just wondering how such an integral:

Integrate[(1.2 - 0.05 x)^2 x^4 ((1 - 0.85 (1.2 - 0.05 x))^0.5 (2 + 
  0.85 (1.2 - 0.05 x)) - (1 + 1.7 (1.2 - 0.05 x)) ArcCos[
 0.85 (1.2 - 0.05 x)]),{x,0,8}]

would output a complex number. How is so? Is this some sort of error? Should I add some sort of assumption? Or suggest an integration method (numerical)? I've tried some yet a still get a complex result.

$\endgroup$
5
  • $\begingroup$ I suppose it's because you take the square root of a negative number, e.g. of (1 - 0.85 (1.2 - 0.05 x)) /. x -> 0.2. -- I still think there's an issue, though. Probably due to the approximate real coefficients. With exact coefficients, I get a memory allocation error. Too bad. :/ $\endgroup$
    – Michael E2
    Commented Sep 29, 2016 at 13:17
  • $\begingroup$ @MichaelE2. The integration produces a real result (-2101.56) in the domain [1/2, 8] where the integrand is real. $\endgroup$
    – m_goldberg
    Commented Sep 29, 2016 at 13:23
  • 1
    $\begingroup$ I'm voting to close this question as off-topic because the issue it raises is not really a Mathematica issue but a matter of the OP not having grasped the mathematics involved. $\endgroup$
    – m_goldberg
    Commented Sep 29, 2016 at 13:24
  • $\begingroup$ Yes, I think you are right @m_goldberg. I'll just close the question myself. $\endgroup$ Commented Sep 29, 2016 at 13:30
  • 2
    $\begingroup$ FunctionDomain[integrand, x] will show that the integration interval extends outside the interval where the integrand is real. $\endgroup$
    – Bob Hanlon
    Commented Sep 29, 2016 at 13:33

2 Answers 2

2
$\begingroup$

There is in fact a problem with evaluating the integral

Let's save the integrand:

int = (1.2 - 0.05 x)^2 x^4 ((1 - 0.85 (1.2 - 0.05 x))^(1/2) *
       (2 + 0.85 (1.2 - 0.05 x)) - (1 + 1.7 (1.2 - 0.05 x)) ArcCos[0.85 (1.2 - 0.05 x)]);

It seems there is more to the OP's integral than just why does it return complex values:

Integrate[int, {x, 0, 8}]
(*  -1833.02 - 268.546 I  *)

Integrate[int, {x, 8/17, 8}]
Integrate[int, {x, 0, 8/17}]
% + %%
(*
  -2101.56 + 2.15407*10^-25 I
  0. + 849215. I
  -2101.56 + 849215. I
*)

NIntegrate[int, {x, 0, 8}]
(*  -2101.56 - 0.0000559972 I  *)

These are inconsistent results.

Rationalizing doesn't help:

ratint = Simplify@Rationalize[int]
Integrate[ratint, {x, 0, 8}]
(* 
  -(1/3200000)(-24 + x)^2 x^4 ((-1208 + 17 x) Sqrt[-8 + 17 x] - 
      40 (-608 + 17 x) ArcCos[-(17/400) (-24 + x)])
*)

Throw::sysexc: Uncaught SystemException returned to top level. Can be caught with Catch[[Ellipsis], _SystemException].

SystemException["MemoryAllocationFailure"]

[Added 6/20/2017.] However, if we split the integral at the singularity at x == 8/17 where the square root (1 - 0.85 (1.2 - 0.05 x))^(1/2) goes from imaginary to real, rationalizing the integrand does work:

Integrate[ratint, {x, 0, 8/17}] + Integrate[ratint, {x, 8/17, 8}]
N[%]
(*
  <long expression for the value of the integral>
  -2101.56 - 0.000420871 I
*)

How to fix it

[Added 6/20/2017.] An easier way than the original below is to pass NIntegrate[] the singular point. Then it integrates quite easily and stably (as the precision is increased):

NIntegrate[int, {x, 0, 8/17, 8}]
NIntegrate[ratint, {x, 0, 8/17, 8}, WorkingPrecision -> 24] (* need high prec. integrand *)
NIntegrate[ratint, {x, 0, 8/17, 8}, WorkingPrecision -> 50]
(*
  -2101.56 - 0.000421068 I
  -2101.56245616445471106361 - 0.00042087103162815865 I
  -2101.5624561644547110636118139788605647565220617959 - 
     0.0004208710316281586476679535473306338243068408 I
*)

Note that in the machine precision answer, the PrecisionGoal will be around 8 digits relative to the magnitude of the answer, which is about 2000. Therefore one should expect the imaginary part to be accurate to 4 or so decimal places. If we compared these answers with the first NIntegrate[] result -2101.56 - 0.0000559972 I, the relative error is 2*^-7, just slightly worse than expected. Singularities tend to make the error estimator perform worse.

[Original answer -- there may be some value in its demonstration of determining an appropriate WorkingPrecision setting, even though it is not the best approach to this problem.] The common advice about avoiding approximate reals in exact solvers like Integrate does not appear to be helpful. But we can increase the precision. Note how much precision is lost, if we start with 50 digits (more than half). This is an ill-conditioned problem.

Integrate[SetPrecision[int, 50], {x, 0, 8}]
(*  -2101.56245616445443429289 - 0.00042087103162815114 I  *)

Precision[%]
(*  23.4808  *)

The imaginary part doesn't quite agree with the numerical result above. Perhaps we should verify the numerical result by increasing its WorkingPrecision a bit.

NIntegrate[SetPrecision[int, 24], {x, 0, 8},
 WorkingPrecision -> 24, MaxRecursion -> 20]
(*  -2101.56245616445443049217 - 0.00042087103155248696 I  *)

That seems better. I think we can have some confidence in the first several digits of the answer.

$\endgroup$
4
  • $\begingroup$ Thanks for this. I had noticed this inconsistency too. Would you suggest to have a go verifying the consistency of integrals over the closing set of it's real domains? $\endgroup$ Commented Sep 29, 2016 at 19:02
  • 2
    $\begingroup$ Integrate[ratint, x] does work however and appears to be continuous. So we can just apply the fundamental theorem of calculus to it. In[41]:= N[Subtract @@ (Integrate[ratint, x] /. {{x -> 0}, {x -> 8}}), 30] Out[41]= 2101.56245616445471106361181398 + 0.00042087103162815864766795 I $\endgroup$
    – Greg Hurst
    Commented Jun 20, 2017 at 20:36
  • $\begingroup$ Also it looks like the memory allocation failure is from simplifying the result from Integrate: In[43]:= FullSimplify[Subtract @@ (Integrate[ratint, x] /. {{x -> 0}, {x -> 8}})] Out[43]= SystemException["MemoryAllocationFailure"] $\endgroup$
    – Greg Hurst
    Commented Jun 20, 2017 at 20:37
  • $\begingroup$ Lastly, if we do some manual tweaking we can simplify the result. FullSimplify[Subtract @@ (Integrate[ratint, x] /. {{x -> 0}, {x -> 8}}) /. ArcSin[f_] :> π/2 - ArcCos[f]] (output omitted) $\endgroup$
    – Greg Hurst
    Commented Jun 20, 2017 at 21:06
2
$\begingroup$

Define for simplicity

f[x_] := (1.2 - 0.05 x)^2 x^4 ((1 - 0.85 (1.2 - 0.05 x))^0.5 (2 + 
       0.85 (1.2 - 0.05 x)) - (1 + 1.7 (1.2 - 0.05 x)) ArcCos[0.85 (1.2 - 0.05 x)])

and work on exact numbers

f[x] = Rationalize@f[x]

Its plot is

enter image description here

{x, 0, 1}, PlotRange -> All... Why doesn't it display the full curve, but starts at $x \lesssim 0.5$?

A closer inspection of f[x] reveals a term being a square root:

Sqrt[1 - 17/20 (6/5 - x/20)]

which is (i.e., the term under the root) negative for some $x$:

enter image description here

Let's find the root:

x0 = x /. Solve[1 - 17/20 (6/5 - x/20) == 0, x][[1]]

8/17

So taking it into account we can integrate:

Integrate[f[x], {x, x0, 8}]

enter image description here

which is

N @ out

-2101.56

So, the function in the interval $(0, 8)$ is complex, so no wonder the integral was complex too.


EDIT: As noted by Bob Hanlon, the domain of the function f can be obtained straightforward:

FunctionDomain[Rationalize@f[x], x]

8/17 <= x <= 808/17

Nevertheless, I consider my original answer to be mathematically instructive.

$\endgroup$

Your Answer

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

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