5
$\begingroup$

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]

enter image description here

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

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

incorrect

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.

$\endgroup$

2 Answers 2

2
$\begingroup$

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+2<x\land b>0 \\ \frac{1}{2} a b (b+2)+a & b+2<x\land -1<b\leq 0 \\ \frac{a x^2}{2} & ((b+2\geq x\land b\leq 0)\lor (b>0\land b+1\geq x)\lor b\leq -1)\land (0<x\leq 1\lor x<0) \\ a \left(x-\frac{1}{2}\right) & x>1\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+1<x\land b>0 \end{cases} $$

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

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

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

enter image description here

$\endgroup$
1
  • $\begingroup$ Thank you. Using Assumption in Integrate solved my problem $\endgroup$
    – Danvil
    Jan 12, 2020 at 1:18
2
$\begingroup$

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}]

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

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<t$\cr}&$0<b$\cr \cases{\cases{1/2\,a{t}^{2}&$t\leq 1$\cr a \left( 2\,t+bt-1/2\,{t}^{2} \right) -ab-a&$t\leq 2+b$\cr 1/2\,a \left( {b}^{2}+2\,b+2 \right) &$2+b<t$\cr}&$0<1+b$\cr \cases{1/2\,a{t}^{2}&$t\leq 1$\cr a/2&$1<t$\cr}&otherwise\cr}&otherwise\cr} $$

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<t$\cr} $$

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}]

Mathematica graphics

$\endgroup$
2
  • $\begingroup$ 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? $\endgroup$
    – Danvil
    Jan 10, 2020 at 8:32
  • $\begingroup$ 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. $\endgroup$
    – Danvil
    Jan 10, 2020 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.