Integration of a piecewise function with parameters

Here is a simple piece-wise and continuous function:

f[t_,a_,b_]:=Piecewise[{{a t,t<1},{a,t<1+b},{a(2+b-t),t<2+b}}]


When I integrate f after defining a and b I get the desired result where integration constants are chosen such that the integral is continuous:

Integrate[f[t,1,1],t]


When I substitute a and b after integration I get a non-continuous function:

Integrate[f[t,a,b],t]/.{a->1,b->1}


I need the integral of f for any a and b with "correct" integration constants. How can I achieve this?

Ultimately I want to for example solve Integral[f[t,a,b],t]==c for a and b and in my actual use case f has considerably more cases and variables.

The following works in version 12.0:

f[t_, a_, b_] := Piecewise[{{a t, t < 1}, {a, t < 1 + b}, {a (2 + b - t), t < 2 + b}}]
j = Integrate[f[t, a, b], {t, 0, x}, Assumptions -> x \[Element] Reals]//FullSimplify


$$\begin{cases} \frac{a}{2} & x>1\land b\leq -1 \\ a (b+1) & b+20 \\ \frac{1}{2} a b (b+2)+a & b+20\land b+1\geq x)\lor b\leq -1)\land (01\land b+1\geq x \\ \frac{1}{2} a (2 b (x-1)-(x-4) x-2) & x>1\land b+2\geq x\land b\leq 0 \\ -\frac{1}{2} a \left(b^2-2 (b+2) x+2 b+x^2+2\right) & x\leq b+2\land b+10 \end{cases}$$

j /. {a -> 1, b -> 1}


$$\begin{cases} 2 & 31\land 2\geq x \\ \frac{1}{2} \left(-x^2+6 x-5\right) & x\leq 3\land 2

Plot[ j /. {a -> 1, b -> 1}, {x, -3, 3}]


• Thank you. Using Assumption in Integrate solved my problem – Danvil Jan 12 at 1:18

Since an offset by a constant (on each interval) is still a valid antiderivative. The real test is to differentiate the antiderivative and see if you get back the integrand, which it does here. So strictly speaking Mathematica's result is not wrong.

f[t_, a_, b_] := Piecewise[{{a t, t < 1}, {a, t < 1 + b}, {a (2 + b - t), t < 2 + b}}]
Plot[f[t, 1, 1], {t, -3, 3}]


sol2 = Integrate[f[t, a, b], t] /. {a -> 1, b -> 1};
integrand = D[sol2, t];
Plot[integrand, {t, -3, 3}, PlotStyle -> Red]


Which is the same as the original integrand. So I think there is no bug here. I also do not know how to make Mathematica gives continuous anti-derivative for a continuous integrand. I would be better if this was the case as with Maple's result below. But this is something internal to how Integrate works inside the kernel.

Below is Maple result.

restart;
f:=(t,a,b)-> piecewise(t<1,a*t,t<(1+b),a,t<(2+b),a*(2+b-t));
sol:=int(f(t,a,b),t):
sol:=subs([a=1,b=1],sol):
p1:=plot(sol,t=-3..3)

p2:=plot(int(f(t,1,1),t),t=-3..3)


Both give same graph

This is what Maple gives for int(f(t,a,b),t)

$$\cases{\cases{1/2\,a{t}^{2}&t\leq 1\cr at-a/2&t\leq 1+b\cr a \left( 2\,t+bt-1/2\,{t}^{2} \right) +ab+a/2-1/2\,a \left( {b}^{2}+4\,b+3 \right) &t\leq 2+b\cr a \left( 1+b \right) &2+b

Which when replacing a->1,b->1 becomes

$$\cases{1/2\,{t}^{2}&t\leq 1\cr t-1/2&t\leq 2\cr 3\,t-1/2\,{t}^{2}-5/2&t\leq 3\cr 2&3

Compare to what Mathematica gives

$$\begin{cases} \frac{a t^2}{2} & t\leq 1 \\ a t & b-t\geq -1 \\ a \left(b t-\frac{t^2}{2}+2 t\right) & b-t\geq -2 \end{cases}$$

Which when replacing a->1,b->1 becomes

$$\begin{cases} \frac{t^2}{2} & t\leq 1 \\ t & 1-t\geq -1 \\ 3 t-\frac{t^2}{2} & 1-t\geq -2 \end{cases}$$

This is plot of both solutions, one is continuous, and the other is not.

sol1 = Integrate[f[t, 1, 1], t]
sol2 = Integrate[f[t, a, b], t] /. {a -> 1, b -> 1};
Plot[{sol1, sol2}, {t, -3, 3}]


• Yes, that is what I am observing.. The equations you list in the middle of your reply are what I am looking for, just that my actual f has 7 cases and 9 variables. Unfortunately I do not have Maple. Is there no way to get the integral with correct integration constants in Mathematica? – Danvil Jan 10 at 8:32
• By the way I believe the wrong solutions only start appearing when the parameters are also in the time intervals. f[t, a, 1] is integrating fine in Mathematica. – Danvil Jan 10 at 8:35