# Piecewise Integration

I am trying to integrate a piecewise defined function twice. It represents the acceleration of a projectile. I am obtaining it's position as a function of time if it starts from rest at the ground, accelerates upward with a constant acceleration of 2.25 and then falls freely after 21.6 seconds. The code I am using to represent the scenario,

a[t_] := Piecewise[{{2.25, t < 21.6}, {-9.8, t > 21.60}}]
v[t_] := Integrate[a[s], {s, 0, t}]
x[t_] := Integrate[v[x], {x, 0, t}]
Plot[a[t], {t, 0, 30}]
Plot[v[t], {t, 0, 30}]
Plot[x[t], {t, 0, 30}]


Sometimes it will give me all three graphs, but usually it will just give me the first two, without the third. It then tells me,

Integrate::pwrl: Unable to prove that integration limits {0,x} are real. Adding assumptions may help. >>

I noticed that if I usually let Mathematica run long enough it will eventually spit out the last graph. I am just curious what is going on. I imagine it has something to do with an integration constant and Mathematica not able to tell what interval the piecewise function is on, but I am not quite sure if I can pint it down.

Any ideas? Or is there a better way to do what I am trying to do?

• Integrate[v[x], {x,0,t}, Assumptions->{t \[Element] Reals}] to make it assume the variable argument is a real number. – IPoiler Sep 22 '15 at 1:05

In this case using Set, so that the integrals are evaluated at the time that v and x are defined, instead of SetDelayed helps with the speed, assumming t has no value. If t has a value, one can use Block as shown below to temporarily block the value while a, v, and x are defined. The increase in speed is due to the ability of Mathematica to evaluate the integrals symbolically in the OP's case. The two integrals are evaluated only once, at the time the functions are defined. Using SetDelayed means the integrals will be recalculated at each value of t, so that the integral is done many times.

One still needs to add an assumption that t is real in the integral to prevent the error Integrate::pwrl.

Clear[a, v, x];
Block[{t},
a[t_] = Piecewise[{{2.25, t < 21.6}, {-9.8, t > 21.60}}];
v[t_] = Integrate[a[s], {s, 0, t}, Assumptions -> t \[Element] Reals];
x[t_] = Integrate[v[x], {x, 0, t}, Assumptions -> t \[Element] Reals];
];
Plot[a[t], {t, 0, 30}]
Plot[v[t], {t, 0, 30}]
Plot[x[t], {t, 0, 30}, Exclusions -> None]

• This is exactly what I was looking for. Thank you. Does that mean by default the Integrate function tries to integrate over the complex plane? – Grant Moore Sep 22 '15 at 1:40
• @GrantMoore Yes. In fact, in most instances the default for Mathematica symbolic functions is to assume the variables and parameters are complex variables. – Michael E2 Sep 22 '15 at 1:44

Personally, whenever I do piecewise functions I like for them to be just one function. What I mean by that is that I use HeavisideTheta[] functions to multiply the piecewise portions by either one or zero depending on the t value.

If you aren't familiar with Heaviside functions, HeavisideTheta[t] would be zero before 0 and 1 for all values after it. HeavisideTheta[t]-HeavisideTheta[t-5] would be 1 from 0 to 5 but 0 everywhere else.

For your function, I would write it as:

a[t_] := 2.25*(HeavisideTheta[t] - HeavisideTheta[t - 21.6]) +
HeavisideTheta[t - 21.6]*-9.8
v[t_] := Integrate[a[s], {s, 0, t}]
x[t_] := Integrate[v[x], {x, 0, t}]
Plot[a[t], {t, 0, 30}]
Plot[v[t], {t, 0, 30}]
Plot[x[t], {t, 0, 30}]


The resulting plots worked fine for me, but for some reason the last one took a while to generate. Doing the integration separately was fast though, so I'm not sure what caused this.

This method can sometimes lead to issues in certain circumstances because there technically is a discontinuity when the Heaviside function is exactly the value at which is switches--in the past I've had to find local min and max with numeric methods because the built in functions didn't like the discontinuity. • Please, Post your Code als copyable, not as picture! – user9660 Sep 22 '15 at 4:42
• It was op's code I just changed the first line. I'll add the rest of his code for others. Thanks for the feedback. – Brian G Sep 22 '15 at 4:46
• Well done, thank you! +1 – user9660 Sep 22 '15 at 8:53