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?
Answer
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]
Comments
Post a Comment