Discussion:
Integrating abs(x)?
Alasdair McAndrew
2008-07-14 10:06:17 UTC
Permalink
Is there a method of coercing Maxima to integrate abs(x); that is, producing
an answer to integrate(abs(x),x,-1,1)?

Thanks,
Alasdair
Stavros Macrakis
2008-07-14 13:46:19 UTC
Permalink
On Mon, Jul 14, 2008 at 6:06 AM, Alasdair McAndrew <***@gmail.com> wrote:

> Is there a method of coercing Maxima to integrate abs(x); that is,
> producing an answer to integrate(abs(x),x,-1,1)?
>

The simplest way is probably to calculate integrate(abs(x),x,-1,0) +
integrate(abs(x),x,0,1). Maxima should be able to do this automatically,
but it doesn't....

Unfortunately, this approach does not generalize to all discontinuous
functions. Thus, though integrate(floor(x),x,0,1) correctly gives 0,
integrate(floor(x),x,1,2) gives a noun form.

-s
Barton Willis
2008-07-14 15:19:28 UTC
Permalink
You can try pmint. Last I checked, you can download pmint from
http://wxmaxima.sourceforge.net/pmint.mac.
I don't know enough about how pmint works to know if my value for the
darboux polynomial of the absolute
value function is correct. So be careful!

Examples:

(%i8) load("pmint.mac")$
(%i9) put(abs, [lambda([u], u), true], 'darboux_poly)$
gradef(abs(x), abs(x) / x)$

(%i11) pmint(abs(x),x);
(%o11) (x*abs(x))/2

(%i12) pmint(abs(6*x),x);
(%o12) 3*x*abs(x)

(%i13) pmint(x * abs(x/5),x);
(%o13) (x^2*abs(x))/15

(%i14) pmint(x^2 * abs(x^3),x);
Solution check failes!
(%o14) (x^3*abs(x)^3)/6

(%i15) ratsimp(diff(%,x));
(%o15) (x^2*abs(x)^3+x^4*abs(x))/2

Barton
Alasdair McAndrew
2008-07-14 21:11:24 UTC
Permalink
Thanks for your replies. What I really want to do is calculate the total
variation of some functions f; this being defined as the integral of the
absolute value of the derivative of f (see
http://en.wikipedia.org/wiki/Total_variation). The integral of abs was just
a test case.

I can probably use pmint, but the results it produces aren't really useful.
For example:

(%i1) pmint(abs(sin(x)),x);
(%o1) -cos(x)*abs(sin(x))/sin(x)

which is true, but doesn't allow me to enter values for which sin(x)=0.
This is too restrictive.

I could probably do all this numerically, but I would rather obtain
closed-form solutions than numerical approximations.

I'll keep fiddling.

cheers,
Alasdair

On Tue, Jul 15, 2008 at 1:19 AM, Barton Willis <***@unk.edu> wrote:

> You can try pmint. Last I checked, you can download pmint from
> http://wxmaxima.sourceforge.net/pmint.mac.
> I don't know enough about how pmint works to know if my value for the
> darboux polynomial of the absolute
> value function is correct. So be careful!
>
> Examples:
>
> (%i8) load("pmint.mac")$
> (%i9) put(abs, [lambda([u], u), true], 'darboux_poly)$
> gradef(abs(x), abs(x) / x)$
>
> (%i11) pmint(abs(x),x);
> (%o11) (x*abs(x))/2
>
> (%i12) pmint(abs(6*x),x);
> (%o12) 3*x*abs(x)
>
> (%i13) pmint(x * abs(x/5),x);
> (%o13) (x^2*abs(x))/15
>
> (%i14) pmint(x^2 * abs(x^3),x);
> Solution check failes!
> (%o14) (x^3*abs(x)^3)/6
>
> (%i15) ratsimp(diff(%,x));
> (%o15) (x^2*abs(x)^3+x^4*abs(x))/2
>
> Barton
>
>


--
Blog: http://amca01.wordpress.com
Stavros Macrakis
2008-07-14 21:47:24 UTC
Permalink
On Mon, Jul 14, 2008 at 5:11 PM, Alasdair McAndrew <***@gmail.com> wrote:

> I can probably use pmint, but the results it produces aren't really
> useful. For example:
>
> (%i1) pmint(abs(sin(x)),x);
> (%o1) -cos(x)*abs(sin(x))/sin(x)
>
> which is true, but doesn't allow me to enter values for which sin(x)=0.
> This is too restrictive.
>

Actually it isn't true. integrate(abs(sin(x)),x) should be monotonically
increasing, but the given expression is periodic, "resetting" to -1 at
n*%pi. And its derivative, sin(x)^2/abs(sin(x)), is undefined at n*%pi for
that reason.

-s
Barton Willis
2008-07-14 22:21:28 UTC
Permalink
-----maxima-***@math.utexas.edu wrote: -----

>On Mon, Jul 14, 2008 at 5:11 PM, Alasdair McAndrew <***@gmail.com>
>wrote:
>
>I can probably use pmint, but the results it produces aren't really
>useful. For example:
>
>(%i1) pmint(abs(sin(x)),x);
>(%o1) -cos(x)*abs(sin(x))/sin(x)
>
>which is true, but doesn't allow me to enter values for which sin(x)=0.
>This is too restrictive.

If you are looking for a fun summer project, write a function that
*tries* to determine if an expression is continuous on a given
interval (or at a given point). It would have to give up (return
unknown) for many expressions, but Maxima could at least do as well as
an average UNK undergraduate student. Maybe Maxima already has such
logic, (for definite integrals and limits) but I don't think it is a
user-level function.

Barton
Raymond Toy (RT/EUS)
2008-07-15 13:30:21 UTC
Permalink
Alasdair McAndrew wrote:
> Thanks for your replies. What I really want to do is calculate the
> total variation of some functions f; this being defined as the integral
> of the absolute value of the derivative of f (see
> http://en.wikipedia.org/wiki/Total_variation). The integral of abs was
> just a test case.
>
> I can probably use pmint, but the results it produces aren't really
> useful. For example:
>
> (%i1) pmint(abs(sin(x)),x);
> (%o1) -cos(x)*abs(sin(x))/sin(x)
>
> which is true, but doesn't allow me to enter values for which sin(x)=0.
> This is too restrictive.
>
> I could probably do all this numerically, but I would rather obtain
> closed-form solutions than numerical approximations.
>
> I'll keep fiddling.

Here's one possible way, which is how I would probably do it by hand,
for the total variation problem.

Find all the points where f'(x) = 0. Maxima can do this for some
functions, but not all. Let the roots be called x[n]. Then the
integral you want becomes the sum of integrals of the form

abs(integrate(f'(x),x, x[n],x[n+1))

Maxima might be able to compute this definite integral.

Hope I didn't make any mistakes!

Ray
Loading...