1
$\begingroup$

I am trying to get an analytic expression for this integral:

 Integrate[Sign[Cos[q]]/(q + 1), {q, 0, x}, Assumptions -> x > 0]

Mathematica gives the answer:

 Abs[Cos[x]] Log[1 + x] Sec[x]

However, when I compare it to the numerical integration, I see that the answer is (incorrectly) discontinuous:

 Plot[{Abs[Cos[x]] Log[1 + x] Sec[x], 
 NIntegrate[Sign[Cos[q]]/(q + 1), {q, 0, x}]}, {x, 0, 4 Pi}]

enter image description here

Is there a way to solve this issue and get the correct answer? Thank you

$\endgroup$
  • $\begingroup$ Well, the derivative of the discontinuous answer and the derivative of the continuous answer are the same, notice that both derivatives are not defined in the same points. Therefore, if Integrate has to find an antiderivative, its discontinuous answer is correct; this is equivalent to accept that the "integration constant" can be a "piecewise constant function", where the "constant" can change only at those points where the derivative is undefined. $\endgroup$ – MaTECmatica Jun 11 '17 at 21:53
  • $\begingroup$ Incidentally, the analytical solution is: int[x_] := (-1)^(1 + Floor[(x - Pi/2)/(Pi)]) Log[x + 1] + Sum[(-1)^(k) 2 Log[(2 k + 1) Pi/2 + 1], {k, 0, Floor[(x - Pi/2)/(Pi)]}] $\endgroup$ – Andrea Jun 12 '17 at 2:32
2
$\begingroup$

This integral has the same issue as one in Slow plot for integral involving FractionalPart and sine, but there seems to be a bug or limitation to DSolve that prevents using it straight out of the box to perform the integration.

The main problem is that until somewhat recently, Mathematica could not handle most discontinuous integrands at all. With improvements to WhenEvent, these can be handled by DSolve over a definite and finite interval. So even though it is possible (and not very difficult) to write a program in this special case to calculat the OP's indefinite integral in terms of an indefinite Sum, I don't believe there is a way to get Integrate or DSolve to do that for you. However, we can get it to solve the integral for all x in a (reasonably sized) finite interval.

To get around the bug/restriction, we have to tweak the settings for PiecewiseExpand to get an expansion of Sign[Cos[q]] that DSolve can deal with.

With[{q1 = 0, q2 = 20},
  Assuming[q1 <= q <= q2,
   With[{opts = Options@PiecewiseExpand},
    Internal`WithLocalSettings[
     SetOptions[PiecewiseExpand, 
      Method -> {"ConditionSimplifier" -> (Reduce[# && $Assumptions] &)}],
     ii = DSolveValue[
       {y'[q] == PiecewiseExpand@Sign[Cos[q]]/(q + 1), y[0] == 0},
       y[q], {q, q1, q2}],
     SetOptions[PiecewiseExpand, opts]
     ]]]];

Compare with numerical integration:

ff = NDSolveValue[{y'[q] == Sign[Cos[q]]/(q + 1), y[0] == 0}, y[q], {q, 0, 20}];
Plot[{ii, ff}, {q, 0, 20}, PlotStyle -> {Thickness[0.015], Thickness[0.006]}]

Mathematica graphics

$\endgroup$
2
$\begingroup$

Using Version 8.0 and giving definite limits for x, Mathematica does the integral easily

int2[x_] =Integrate[Sign[Cos[q]]/(q + 1), {q, 0, x}, Assumptions -> 0 < x < 20]

(*  Boole[\[Pi]/2 < x] Log[(2 + \[Pi])/2] + 
    Boole[(3 \[Pi])/2 <= x] Log[(2 + \[Pi])/(2 + 3 \[Pi])] + 
    Boole[(5 \[Pi])/2 <= x] Log[(2 + 5 \[Pi])/(2 + 3 \[Pi])] + 
    Boole[(7 \[Pi])/2 <= x] Log[(2 + 5 \[Pi])/(2 + 7 \[Pi])] + 
    Boole[(9 \[Pi])/2 <= x] Log[(2 + 9 \[Pi])/(2 + 7 \[Pi])] + 
    Boole[(11 \[Pi])/2 <= x] Log[(2 + 9 \[Pi])/(2 + 11 \[Pi])] + 
    Boole[x <= \[Pi]/2] Log[1 + x] - 
    Boole[\[Pi]/2 < x < (3 \[Pi])/2] Log[(2 (1 + x))/(2 + \[Pi])] + 
    Boole[(3 \[Pi])/2 < x < (5 \[Pi])/2] Log[(2 (1 + x))/(2 + 3 \[Pi])] -
    Boole[(5 \[Pi])/2 < x < (7 \[Pi])/2] Log[(2 (1 + x))/(
           2 + 5 \[Pi])] + 
    Boole[(7 \[Pi])/2 < x < (9 \[Pi])/2] Log[(2 (1 + x))/(2 + 7 \[Pi])] -
    Boole[(9 \[Pi])/2 < x < (11 \[Pi])/2] Log[(2 (1 + x))/(
           2 + 9 \[Pi])] + 
    Boole[(11 \[Pi])/2 < x] Log[(2 (1 + x))/(2 + 11 \[Pi])]   *)

 Plot[{int2[x], NIntegrate[Sign[Cos[q]]/(q + 1), {q, 0, x}]}, {x,0,20}]

enter image description here

$\endgroup$
  • $\begingroup$ Cool. This still works in V10, V11, too. (But easily?) $\endgroup$ – Michael E2 Jun 11 '17 at 20:39
  • $\begingroup$ int2[x_] = Integrate[Sign[Cos[q]]/(q + 1), {q, 0, x}, Assumptions -> 0 < x < 20] // PiecewiseExpand // FullSimplify $\endgroup$ – Bob Hanlon Jun 11 '17 at 23:40
  • $\begingroup$ This works, thanks. But would it be possible to have the solution for arbitrary finite x, instead of having to specify a finite range (e.g., [0,20])? $\endgroup$ – Andrea Jun 12 '17 at 1:33
  • $\begingroup$ @Andrea Yes, as I mentioned in my answer. It's not hard to figure out. But I don't think you can have a simple formula that does not use some sort of iterative process like Sum or Range or so forth. $\endgroup$ – Michael E2 Jun 12 '17 at 3:27
  • $\begingroup$ @MichaelE2, yes, the solution can be written as: int[x_] := (-1)^(1 + Floor[(x - Pi/2)/(Pi)]) Log[x + 1] + Sum[(-1)^(k) 2 Log[(2 k + 1) Pi/2 + 1], {k, 0, Floor[(x - Pi/2)/(Pi)]}] that has a finite sum in it. I wonder if there's a way in which Mathematica can find the solution $\endgroup$ – Andrea Jun 12 '17 at 3:30

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.