# How to plot the derivative of a piecewise discontinuous function (by excluding discontinuity points)?

With the following code I am trying to get a plot for the derivative of the ftot1 function, which is a piecewise discontinuous function defined as the sum of other two piecewise discontinuous functions (fd and fo), but I receive an error and a blank plot.

td = 2; to = 2; T = 10; sld = 1; slo = 1;
fd[tstar_, x_ ] :=
If[tstar < td, 0,
Piecewise[{{0, 0 <= x < td}, {sld*x, td <= x < tstar} , {sld*tstar,
tstar <= x <= T}}]];
fo[tstar_ , x_] :=
If[T - tstar < to, 0,
Piecewise[{{0, 0 <= x < tstar + to}, {slo*(x - tstar),
tstar + to <= x <= T}}]];
ftot1[tstar_, x_] = fd[tstar, x] + fo[tstar , x];
Plot[ftot1[tstar, x] /. x -> T, {tstar, 0, T},
Exclusions -> {tstar == td, tstar == T - to},
ExclusionsStyle -> Directive[Red, Dashed]];
D[PiecewiseExpand[ftot1[tstar, x] /. x -> T], tstar]
Limit[D[PiecewiseExpand[ftot1[tstar, x] /. x -> T], tstar],
tstar -> td]
Limit[D[PiecewiseExpand[ftot1[tstar, x] /. x -> T], tstar],
tstar -> to]
Limit[D[PiecewiseExpand[ftot1[tstar, x] /. x -> T], tstar], tstar -> T]
Plot[D[ftot1[tstar, x] /. x -> T, tstar], {tstar, 0, T},
Exclusions -> {tstar == td, tstar == T - to},
ExclusionsStyle -> Directive[Red, Dashed]]



I am trying to deal with the discontinuity points, which are responsible for the failure of the Plot function I think. I have three discontinuity points (at td, to and T). I have tried to play with the range of the discontinuities (changing the equality sign) but nothing changes.

• Add Evaluate. Plot[D[ftot1[tstar, x] /. x -> T, tstar] // Evaluate, {tstar, 0, T}, Exclusions -> {tstar == td, tstar == T - to}, ExclusionsStyle -> Directive[Red, Dashed]] Aug 12, 2022 at 14:22

I have just seen that @cvgmt has given you a solution. Here is mine which is along the same lines.

td = 2; to = 2; T = 10; sld = 1; slo = 1;
fd[tstar_, x_] :=
If[tstar < td, 0,
Piecewise[{{0, 0 <= x < td}, {sld*x, td <= x < tstar}, {sld*tstar,
tstar <= x <= T}}]];

fo[tstar_, x_] :=
If[T - tstar < to, 0,
Piecewise[{{0, 0 <= x < tstar + to}, {slo*(x - tstar),
tstar + to <= x <= T}}]];

ftot1[tstar_, x_] = fd[tstar, x] + fo[tstar, x];

Plot[ftot1[tstar, x] /. x -> T, {tstar, -1, T + 1},
Exclusions -> {tstar == td, tstar == T - to},
ExclusionsStyle -> Directive[Red, Dashed]] When you take the derivative you need to define a new function

    ClearAll[dt];
dt[tstar_] :=
Evaluate[D[PiecewiseExpand[ftot1[tstar, x] /. x -> T], tstar]]
Plot[dt[t], {t, -1, 12},
Exclusions -> {tstar == td, tstar == T - to},
ExclusionsStyle -> Directive[Red, Dashed]] This does not give you the nice exclusions probably because a point is not evaluated at the discontinuities. If you wish to show the exclusions something like the following will work

Show[
Plot[dt[t], {t, -1, 2}],
Plot[dt[t], {t, 2, 8}],
Plot[dt[t], {t, 8, 10}],
Plot[dt[t], {t, 10, 12}],
Graphics[{Red, Dashing[{0.01, 0.01}], Line[{{2, -1}, {2, 0}}],
Line[{{8, 0}, {8, 1}}],
Line[{{10, 1}, {10, 0}}]}],
PlotRange -> {{-1, 12}, All}
] However, I am not clear if it is just a plot you want. Also, the above could be automated but it was easier to use numbers rather than symbols.