0
$\begingroup$

Given a piecewise function of v[t], how to plot its displacement x[t]?

ClearAll[v, x];
v[t_] := 2 t /; t < 10;
v[t_] := 20 /; 10 <= t < 25;
v[t_] := t - 5 /; 25 <= t < 35;
v[t_] := 30 /; 35 <= t < 50;
v[t_] := -7 t + 380 /; 50 <= t < 55;
v[t_] := -5 /; 55 <= t < 65;
x[t_] := Integrate[v[t], {t, 0, 10}] /; t <= 10;
x[t_] := Integrate[v[t], {t, 10, 25}] + x[10] /; 10 < t <= 25;
x[t_] := Integrate[v[t], {t, 25, 35}] + x[25] /; 25 < t <= 35;
x[t_] := Integrate[v[t], {t, 35, 50}] + x[35] /; 35 < t <= 50;
x[t_] := Integrate[v[t], {t, 50, 55}] + x[50] /; 50 < t <= 55;
x[t_] := Integrate[v[t], {t, 55, 65}] + x[55] /; 55 < t <= 65;
Plot[{v[t], x[t]}, {t, 0, 65}]

The above code produces a lot of errors:

Integrate::ilim: Invalid integration variable or limit(s) in {0.00132786,0,10}.

NIntegrate::itraw: Raw object 0.0013278571428571428` cannot be used as an iterator.

etc...

$\endgroup$
2
  • 1
    $\begingroup$ you can't integrate with respect to a number. When you do x[10] for example, then what do you expected the call to x[t_] to be? t will be a number, and then you are doing integrate with respect to this t which is a number. $\endgroup$
    – Nasser
    Sep 3, 2021 at 6:03
  • $\begingroup$ @Nasser: Thank your very much. Your comment is very helpful. $\endgroup$ Sep 3, 2021 at 15:22

2 Answers 2

1
$\begingroup$

Try this:

v[t_] := Which[0 <= t < 10, 2 t, 10 < t <= 25, 20, 25 < t <= 35, 
   t - 5];
x[t_] := Integrate[v[t1], {t1, 0, t}];
Plot[x[t], {t, 0, 35}]

returning the plot:

enter image description here

Comments:

  1. I intentionally did not include all your details in the definition of v[t]. First, it is not difficult to make it based on my solution. Second, the scale of x[t] is such that details of the behavior will become badly visible on it.

  2. I do not recommend showing v[t] and x[t] on the same plot due to their very different scales.

Have fun!

$\endgroup$
0
$\begingroup$

Only for future purposes.

ClearAll[v, x, scale];
scale = 1/50;
v[t_] := 2 t /; t < 10;
v[t_] := 20 /; 10 <= t < 25;
v[t_] := t - 5 /; 25 <= t < 35;
v[t_] := 30 /; 35 <= t < 50;
v[t_] := -7 t + 380 /; 50 <= t < 55;
v[t_] := -5 /; 55 <= t < 65;
x[t_] := scale Integrate[v[u], {u, 0, t}] /; t <= 10;
x[t_] := scale Integrate[v[u], {u, 10, t}] + x[10] /; 10 < t <= 25;
x[t_] := scale Integrate[v[u], {u, 25, t}] + x[25] /; 25 < t <= 35;
x[t_] := scale Integrate[v[u], {u, 35, t}] + x[35] /; 35 < t <= 50;
x[t_] := scale Integrate[v[u], {u, 50, t}] + x[50] /; 50 < t <= 55;
x[t_] := scale Integrate[v[u], {u, 55, t}] + x[55] /; 55 < t <= 65;
Plot[{v[t], x[t]}, {t, 0, 65}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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