Ask Your Question

# Integration of multivariable piecewise function in SageMath

I have a problem that can be simplified as below:

g(w) = piecewise([[[0, 3], (w-3)^2], [(3, infinity), 0]])
f(w,t) = g*cos(w*t)  # t >= 0
#assume(t, 'real')
h = integral(f, w, 0, 10); h


Out: t |--> undef

If I do not use piecewise, and integrate only up to w = 3 (where I know g becomes zero), then I obtain the wanted solution:

g2(w) = (w-3)^2
f2(w, t) = g2*cos(w*t)
h2 = integral(f2, w, 0, 3); h2


Out: t |--> 6/t^2 - 2sin(3t)/t^3

How can I integrate f(w, t) defining g(w) piece-wisely (or equivalent)?

edit retag close merge delete

## Comments

Welcome to Ask Sage! Thank you for your question!

( 2021-01-16 21:54:32 +0200 )edit

## 1 Answer

Sort by » oldest newest most voted

### Short answer

You can rewrite $g$ as $$g(w)=(w-3)^2(H(w)-H(w-3)),$$ where $H$ stands for the Heaviside function, given by $$H(x)=\begin{cases} 0, & x<0, \\\\ 1, & x>0. \end{cases}$$

Then, the following code solves the question:

sage: g(w) = (w-3)^2*(heaviside(w) - heaviside(w-3))
sage: f(w,t) = g(w)*cos(w*t)
sage: integral(f(w,t), w, 0, 10, algorithm="giac")
6/t^2 - 2*sin(3*t)/t^3


It seems that Maxima and Sympy cannot integrate functions involving translates of $H$, but Giac can do it. This is why I added the option algorithm="giac".

### Explanation

Any piecewice function $g$ can be written in terms of translates of the Heavide function. Assuming that $g(x)=g_0(x)$ in $(-\infty,x_0)$, $g(x)=g_i(x)$ in $(x_{i-1},x_i)$ for $i=1,\ldots,k$, and $g(x)=g_{k+1}(x)$ in $(x_k,+\infty)$, we have: $$g(x) =g_0(x) + \sum_{i=0}^k \bigl(g_{i+1}(x)-g_i(x)\bigr) H(x-x_i).$$ The values of $g$ at $x_0,...,x_k$ are not relevant, since we are dealing with integration.

In the original question, we have $k=1$, $x_0=0$, $x_1=3$, $g_0(x)=0$, $g_1(x)=(x-3)^2$ and $g_2(x)=0$. Therefore, g(x)\begin{aligned}[t] &=g_0(x)+(g_1(x)-g_0(x))H(x-x_0) + (g_2(x)-g_1(x))H(x-x_1) \\ &= (x-3)^2H(x)-(x-3)^2H(x-3) \\ &=(x-3)^2(H(x)-H(x-3)). \end{aligned} This justifies the code given above.

more

## Comments

Thanks @Juanjo, this is a good alternative definition and solves my problem. Would be good to understand what is wrong with piecewise

( 2021-01-18 16:55:39 +0200 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2021-01-16 15:57:53 +0200

Seen: 66 times

Last updated: Jan 17