how to calculate integrate(abs(sin(x)), x, 0, %pi);

  • Flavio

    Flavio - 2013-11-13

    I would like wxmaxima to calculate integrate(abs(sin(x)), x, 0, %pi);
    could someone tell me how to get the value of the integral, please?

  • Albrecht Mueller

    What about integrate(sin(x), x, 0, %pi)?
    As sin(x) is not negative in the range 0 ... %pi, abs(sin(x)) is the same as sin(x). I think Maxima is not able to symbolically integrate terms containing the abs-function.

    If you are satisfied with numerical integration, you may try

    romberg(abs(sin(x)), x, 0, %pi);


    quad_qags(abs(sin(x)), x, 0, %pi);

  • Flavio

    Flavio - 2013-11-14

    Thank you Albrecht;
    of course sin(x) >= 0 when x is in the range [0,pi].
    but if I need to calculate more complex integrals containing the abs function, how can I do?
    You say Maxima is not able to cope with the abs function when symbolically calculating integrals;
    ok, this is a satisfying answer for me;
    however Maxima can calculate symbolically the integral of abs(x);
    but not the integral of abs(x^3);
    it seems a bit strange.
    Do you know if softwares such as Mathematica or Maple are able to calculate such integrals?
    I hope you will have good days;

    Last edit: Flavio 2013-11-14
  • Albrecht Mueller

    Hello Flavio,

    you could do a case-by-case analysis and consider the situations where the argument of abs is positive or negative separately. This way you can eliminate the abs function.

    I was not able to get a solution of abs(x) using the Wolfram Mathematica Online Integrator.

    The support of the abs function seems to be quite limited and buggy. For example Maxima may reduce terms containing the abs function such that they return complex instead of real numbers, leading to strange consequences. See plot2d: expression evaluates to non-numeric value everywhere in plotting range and tickets #2549 and #2550.

    I think implementing full support of the abs function is not a trivial thing - you have to automate the case-by-case analyis, which requires finding the locations where the sign of the arguments changes. Thus you have to analyze the zeros of functions, singularities and discontinuities. And there are the complex numbers ...



  • Flavio

    Flavio - 2013-11-14

    Thank you, Albrecht.
    You have been very kind to answer my questions.
    With kind regards.

  • Flavio

    Flavio - 2013-11-14

    Wolfram_Alpha, link:
    is able to cope with integrals containing the abs function.
    For instance it can calculate:
    integral from 0 to 2*pi of abs(sin(x)).
    It can also symbolically calculate
    integral from 1 to 2 of abs(x^2-2)
    integral from -1 to 2 of abs(x^3-2)
    and other similar integrals.

    Last edit: Flavio 2013-11-14
  • Albrecht Mueller

    Thank you for the link. I tried the indefinite integral of abs(sin(x)) and got -cos(x)*sgn(sin(x)) + constant. I consider this as wrong.

    Reason: If you use this result to calculate the definite integral for a range where cos(x) is zero at the beginning and at the end, then your result is zero which is wrong except for cases where the start and end are the same. The cause of this problem is that the formula is not contiuous at those locations where the sign of sin(x) changes. Nevertheless WolframAlpha's calculation of the corresponding definite integral seems to be correct.

    Maxima has a similar problem, but - as it seems to calculate definite integrals using the indefinite integrals without further corrections - results of the definite integrals are wrong too. See Integration error, problem with integrate and ticket #2575.

  • Flavio

    Flavio - 2013-11-16

    Yes, a periodic function can't be the primitive of a positive (sometimes equal to 0) function .
    The right primitive of abs(sin(x)) should be 2floor(x/pi)-cos(x)sgn(sin(x))-(something_equal_to_1_when_x_is_a_multiple_of_pi,equal_to_0_otherwise) =
    = 2floor(x/pi)-cos(x)sgn(sin(x))-(1-sgn((x/pi)-floor(x/pi)).

    Last edit: Flavio 2013-11-16
  • Joseph Cusumano

    Joseph Cusumano - 2013-11-16

    Ciao, Flavio. Hallo, Albrecht.

    I'm a long-time Maxima user, and I've never really understood why something as simple as abs(sin(x)) can't be integrated. But, then again, I am not a Lisp programmer! I suspect the integration routines don't "know" when sin(x) changes sign.

    You both would probably be interested in the pw package. This is not included with the standard release, so you have to install it manually.

    Unfortunately, pw doesn't actually solve this problem--it can't integrate abs(sin(x)), either--but it has many useful functions for creating, manipulating, differentiating, and integrating piecewise functions. Also, check out abs_integrate: it's included with the standard release and can be loaded by load(abs_integrate), but I believe it's automatically loaded with load(pw), as well.



  • Flavio

    Flavio - 2013-11-16

    Ciao Joe,
    it's kind from you to inform us of the existence of this interesting software.
    At the moment I not even know the most elementary features of Maxima, nor I really need to know, even if I would like to.
    However the pw package seems to be very interesting.
    The calculation of the integral of abs(sin(x)) was a mere curiosity to me.
    Thank you very much, and have good days.

    • Joseph Cusumano

      Joseph Cusumano - 2013-11-17

      Ciao tutti:

      I just had an email exchange with Richard Hennessy, the gentleman who
      wrote the pw package. It turns out I was making a syntax error with the
      function intperiodic.

      The following will work:

      After loading pw, you can obtain an indefinite integral of abs(sin(x))
      using the command:


      Note that I don't need the absolute value, and that the period of the
      function is %pi, not 2*%pi. Also note (and this was what was confusing
      me) that the arguments of intperiodic are actually defining the periodic
      function sin(x) on [0,%pi], not giving limits of integration as in the
      function integrate. Again, the output of intperiodic is an indefinite
      integral, so the definite integral is just:


      Which gives the correct answer of 4.

  • Flavio

    Flavio - 2013-11-17

    Thank you, Joe.

    I have loaded the package and tried your suggestions.

    Everything works fine.
    This is what I was searching for.

    I note the output of intperiodic(sin(x),x,0,%pi) may be simplified (ratsimp(%)) and the result is:

    I also note there are troubles trying to calculate intperiodic(abs(sin(x)),x,0,2*%pi).

    I have tried load(abs_integrate) and observed it enables maxima to calculate some simple integrals in which the integrand function contains the abs function. It fails, however, at calculating simple integrals such as that of f(x)=abs(x^3-2) (it can calculate the integral of abs(x^3)).

    There remains the problem of calculating indefinite integrals of non-periodic functions containing the abs function.

    Again, thank you for your suggestions and your kindness.

    Last edit: Flavio 2013-11-17
  • Albrecht Mueller

    Hello Flavio and Joe,

    thank you Joe for pointing to pw.mac and to the abs_integrate package. I tried integrate(abs(sin(x)), x) after loading the abs_integrate package alone. The result was a term too big to understand, and trying to simplify it did not give additional insight. A plot of the function showed that the result was wrong (periodic, undefined parts).

    I think the pw package could help to implement a better support of the abs function. Therefore I added a remark to ticket #2576. What remains to be done is to implement functions that are able to identify the points that separate continuous parts. The integrate function could use this information to call services of the pw package to get the picewise defined result.




Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks