Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating a piecewise defined function in maxima

Tags:

maxima

Say that I have the following function:

f(x) := if x<=0 then 0 else if x<=1 then 1 else -1;

Or any other piecewise defined function.

The function definition seems to work:

(%i9) f(-11);
f(1/2);
f(2);
(%o7) 0
(%o8) 1
(%o9) -1

However integration doesn't evaluate here. It is somehow possible to get the value of the integral here in Maxima? And if not, could it be done numerically in maxima?

like image 692
Kasper Avatar asked Nov 15 '25 09:11

Kasper


1 Answers

load(abs_integrate) to get the abs_integrate package, which enables integrate to handle unit_step. You'll have to write your piecewise function in terms of unit_step. E.g.:

(%i1) load (abs_integrate) $
(%i2) e : unit_step(t) - 2*unit_step(t - 1) $
(%i3) integrate (e, t, a, b);
             abs(b) - b - 2 abs(b - 1) - abs(a) + a + 2 abs(a - 1)
(%o3)        -----------------------------------------------------
                                       2

For numerical integration, quad_qags (and other quadpack functions) can handle both unit_step and if expressions. quad_qags doesn't need abs_integrate.

like image 75
Robert Dodier Avatar answered Nov 17 '25 08:11

Robert Dodier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!