Definite integral of a real function outputs complex number

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.

=================

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. :/
– Michael E2
Sep 29 at 13:17

@MichaelE2. The integration produces a real result (-2101.56) in the domain [1/2, 8] where the integrand is real.
– m_goldberg
Sep 29 at 13:23

1

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.
– m_goldberg
Sep 29 at 13:24

Yes, I think you are right @m_goldberg. I’ll just close the question myself.
– Mirko Aveta
Sep 29 at 13:30

2

FunctionDomain[integrand, x] will show that the integration interval extends outside the interval where the integrand is real.
– Bob Hanlon
Sep 29 at 13:33

=================

2

=================

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:

Simplify@Rationalize[int]
Integrate[%, {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”]

How to fix it

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.

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?
– Mirko Aveta
Sep 29 at 19:02

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

{x, 0, 1}, PlotRange -> All… Why doesn’t it display the full curve, but starts at x≲0.5x \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 xx:

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}]

which is

N @ out

-2101.56

So, the function in the interval (0,8)(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.