Message Boards Message Boards

0
|
4051 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to integrate a piecewise function?

Posted 2 years ago

I keep getting funny errors when trying to do this:

g[t_] := -9.8;
a[t_] := Piecewise[{{0, t < 0}, {12, 0 <= t < 1}, {14, 
     1 <= t < 3}, {11, 3 <= t < 8}, {0, t >= 8}}];
Plot[a[t], {t, 0, 10}]
v[t_] := Integrate[a[t], t];
v[t]
Integrate[a[t], t]
Plot[v[t], {t, 0, 10}]
Plot[g[t], {t, 0, 10}]
Attachment

Attachments:
POSTED BY: Orest Gogosha
4 Replies
Posted 2 years ago

I tried this first and was getting errors. Obviously, you don't. Wonder why. I'll try again. Thank you!

GOGO

POSTED BY: Orest Gogosha
Posted 2 years ago

Hi Orest,

An alternative to Martijn's answer

(* Acceleration *)
a[t_] := 
 Piecewise[{{0, t < 0}, {12, 0 <= t < 1}, {25, 1 <= t < 3}, {11, 3 <= t < 8}, {0, t >= 8}}]

(* Velocity *)
v[t_] := Integrate[a[t], t];

(* Distance *)
s[t_] := Integrate[v[t], t];

Plot[Evaluate[{a[t], v[t], s[t]}], {t, 0, 10},
 PlotTheme -> "Detailed",
 PlotRange -> All,
 ImageSize -> 600]

enter image description here

The 117 True in the Piecewise means the value is 117 everywhere else in the domain t > 8.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thank you very much!! I don't know how I could have divined this from the documentation. What is the last line (117 Ture) trying to tell me? I would now like to integrate v[t]. Do I proceed in the same way?`

g[t_] := -9.8;
a[t_] := Piecewise[{{0, t < 0}, {12, 0 <= t < 1}, {25, 
     1 <= t < 3}, {11, 3 <= t < 8}, {0, t >= 8}}];
Plot[a[t], {t, 0, 10}]
v1 = Integrate[a[t], t];
v2[tj_] := v1 /. t -> tj
v2[t]
Plot[v2[t], {t, -1, 10}]

vt = Integrate[a[t], t];
v[ti_] := vt /. t -> ti;
v[t]
Plot [v[t], {t, 0, 10}]
Plot[g[t], {t, 0, 10}]
Attachment

POSTED BY: Orest Gogosha

When you definve v[t_]:=Integrate [f[t],t] and you try to plot it you are basically solving for each t

Plot[{
    Integrate[f[0],0] 
    Integrate[f[1],1] 
    Integrate[f[2],2]
    ...
}]

That won't work, so first calculate the integral and then define a function that replaces after integration.

vt = Integrate[a[t], t] 
v[ti_] := vt /. t -> ti
Plot[v[t], {t, 0, 10}]
POSTED BY: Martijn Froeling
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract