Function Repository Resource:

PiecewiseD

Source Notebook

The derivative of a piecewise function with Indeterminate for points or regions where the function is not defined

Contributed by: Dennis M Schneider

ResourceFunction["PiecewiseD"][f,x]

returns the derivative of a piecewise function returning the value Indeterminate for points or regions where the function is not defined.

ResourceFunction["PiecewiseD"][f,x,k]

returns the function together with its first k derivatives.

ResourceFunction["PiecewiseD"][f,{x,k}]

returns the kth derivative.

Examples

Basic Examples (2) 

Compute the derivative of a piecewise function:

In[1]:=
f[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{
RowBox[{"-", "2"}], " ", "x"}], 
RowBox[{"x", "<", "0"}]},
{
RowBox[{
RowBox[{"2", " ", "x"}], "+", "1"}], 
RowBox[{"x", ">", "0"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)
ResourceFunction["PiecewiseD"][f[x], x]
Out[2]=

Compute the derivatives of a function whose domain is not an interval:

In[3]:=
f[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{
RowBox[{"-", "2"}], " ", 
RowBox[{"Sin", "[", "x", "]"}]}], 
RowBox[{
RowBox[{"-", "1"}], "<", "x", "<", "0"}]},
{
RowBox[{
SuperscriptBox["x", "3"], "+", "1"}], 
RowBox[{"x", ">", "1"}]},
{"0", 
RowBox[{"x", "<", 
RowBox[{"-", "1"}]}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "Rows" -> {{Baseline}}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "Rows" -> {{1.}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)
ResourceFunction["PiecewiseD"][f[x], x, 3]
Out[4]=

Compute just the third derivative:

In[5]:=
ResourceFunction["PiecewiseD"][f[x], {x, 3}]
Out[5]=

Plot the function together with its first three derivatives:

In[6]:=
ResourceFunction["EnhancedPlot"][
 ResourceFunction["PiecewiseD"][f[x], x, 3], {x, -1, 2}, "Exception" -> {0, 1}, PlotRange -> All]
Out[6]=

Scope (4) 

Find and plot the first- and second-order derivatives. The function and its first-order derivative are continuous at x=0, but not the second-order derivative:

In[7]:=
f[x_] := x Sin[Abs[x]]
ResourceFunction["PiecewiseD"][ f[x], x, 2]
Out[8]=

Check that the first derivative is continuous:

In[9]:=
Limit[ResourceFunction["PiecewiseD"][ f[x], x], x -> 0] == f[0]
Out[9]=

Check that the second derivative is not continuous:

In[10]:=
Limit[ResourceFunction["PiecewiseD"][ x Sin[Abs[x]], {x, 2}], x -> 0]
Out[10]=

Plot the results:

In[11]:=
ResourceFunction["EnhancedPlot"][
 ResourceFunction["PiecewiseD"][f[x], x, 2], {x, -\[Pi], \[Pi]}, "Exception" -> 0, PlotRange -> {-3.5, 3.5}]
Out[11]=

Properties and Relations (1) 

Show the difference between PiecewiseD and D:

In[12]:=
{ResourceFunction["PiecewiseD"][x RealAbs[x], x], D[x RealAbs[x], x]}
Out[12]=

Applications (5) 

The following function has a removable discontinuity at x=3 and an infinite discontinuity at x=4:

In[13]:=
f[x_] := (x^2 - x - 6)/((x - 3) (x - 4))
ResourceFunction["EnhancedPlot"][f[x], {x, 1, 6}, "Exception" -> 3, "Asymptote" -> 4]
Out[14]=

Extend the definition at x=3 to make the extended function continuous there:

In[15]:=
Limit[f[x], x -> 3]
Out[15]=
In[16]:=
g[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{"f", "[", "x", "]"}], 
RowBox[{
RowBox[{"x", "!=", "3"}], "&&", 
RowBox[{"x", "!=", "4"}]}]},
{
RowBox[{"-", "5"}], 
RowBox[{"x", "==", "3"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)
ResourceFunction["EnhancedPlot"][g[x], {x, 1, 6}, "Exception" -> 3, "Asymptote" -> 4]
Out[17]=

The extended function is actually differentiable at x=3:

In[18]:=
ResourceFunction["PiecewiseD"][g[x], x]
Out[18]=

The resource function EnhancedPlot produces a correct plot:

In[19]:=
ResourceFunction["EnhancedPlot"][
 ResourceFunction["PiecewiseD"][g[x], x, 1], {x, 1, 6}, "Exception" -> 3, "Asymptote" -> 4]
Out[19]=

The function g is differentiable at x=0 and PiecewiseD returns the correct value, 1. The function D, however, returns the value 0 for the derivative at x=0:

In[20]:=
Clear[g]
g[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{
RowBox[{"4", 
SuperscriptBox["x", "2"], 
RowBox[{"Sin", "[", 
FractionBox["1", "x"], "]"}]}], "+", "x"}], 
RowBox[{"x", "!=", "0"}]},
{"0", 
RowBox[{"x", "==", "0"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False]\)
{ResourceFunction["PiecewiseD"][g[x], x], D[g[x], x]}
Out[22]=
In[23]:=
ResourceFunction["EnhancedPlot"][g[x], {x, -.5, .5}, "Exception" -> 0]
Out[23]=

However, the derivative is not continuous:

In[24]:=
ResourceFunction["EnhancedPlot"][
 ResourceFunction["PiecewiseD"][g[x], x], {x, -.5, .5}, "Exception" -> 0, PlotStyle -> Opacity[.5], "PointStyle" -> AbsolutePointSize[12]]
Out[24]=

This function is differentiable at x=0 and its derivative is continuous there:

In[25]:=
Clear[g]
g[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{
RowBox[{
SuperscriptBox["x", "3"], 
RowBox[{"Sin", "[", 
FractionBox["1", "x"], "]"}]}], "+", "x"}], 
RowBox[{"x", "!=", "0"}]},
{"0", 
RowBox[{"x", "==", "0"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False]\)
dg[x_] = ResourceFunction["PiecewiseD"][g[x], x]
Out[27]=
In[28]:=
Limit[dg[x], x -> 0]
Out[28]=

Plot the result using the resource function EnhancedPlot:

In[29]:=
ResourceFunction["EnhancedPlot"][
 ResourceFunction["PiecewiseD"][g[x], x], {x, -.5, .5}, "Exception" -> 0, "PointStyle" -> AbsolutePointSize[10]]
Out[29]=

A function with a singularity at x=-1 and x=1; PiecewiseD returns the correct result. Note that if this expression is simplified, the singularity at x=1 will be lost:

In[30]:=
{ResourceFunction[
  "PiecewiseD"][(x^2 + 2 x - 3)/(x^2 - 1) Sin[\[Pi] x], x], Simplify[
  ResourceFunction[
   "PiecewiseD"][(x^2 + 2 x - 3)/(x^2 - 1) Sin[\[Pi] x], x]]}
Out[30]=

The resource function EnhancedPlot is able to produce a correct plot:

In[31]:=
ResourceFunction["EnhancedPlot"][
 ResourceFunction[
  "PiecewiseD"][(x^2 + 2 x - 3)/(x^2 - 1) Sin[\[Pi] x], x, 1], {x, -2,
   2}, "Exception" -> {-1, 1}, PlotRange -> {-10, 12}]
Out[31]=

Extend the function so that it becomes continuous at -1 and 1:

In[32]:=
Limit[(x^2 + 2 x - 3)/(x^2 - 1) Sin[\[Pi] x], x -> #] & /@ {-1, 1}
Out[32]=
In[33]:=
Clear[h]
h[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{
FractionBox[
RowBox[{
SuperscriptBox["x", "2"], "+", 
RowBox[{"2", "x"}], "-", "3"}], 
RowBox[{
SuperscriptBox["x", "2"], "-", "1"}]], 
RowBox[{"Sin", "[", 
RowBox[{"\[Pi]", " ", "x"}], "]"}]}], 
RowBox[{
RowBox[{"x", "!=", 
RowBox[{"-", "1"}]}], "&&", 
RowBox[{"x", "!=", "1"}]}]},
{
RowBox[{
RowBox[{"-", "2"}], "\[Pi]"}], 
RowBox[{"x", "==", 
RowBox[{"-", "1"}]}]},
{"0", 
RowBox[{"x", "==", "1"}]}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)

The first and second derivatives are continuous at ±1:

In[34]:=
Limit[ResourceFunction["PiecewiseD"][h[x], x, 2], x -> -1] == ResourceFunction["PiecewiseD"][h[x], x, 2] /. x -> -1
Out[34]=
In[35]:=
Limit[ResourceFunction["PiecewiseD"][h[x], x, 2], x -> 1] == ResourceFunction["PiecewiseD"][h[x], x, 2] /. x -> 1
Out[35]=

Plot the extended function and its first two derivatives:

In[36]:=
ResourceFunction["EnhancedPlot"][
 ResourceFunction["PiecewiseD"][h[x], x, 2], {x, -2, 2}, "Exception" -> {-1, 1}, PlotRange -> {-25, 25}]
Out[36]=

A classic example of a nonzero infinitely differentiable function all of whose derivatives at x=0 are 0 and hence all of whose Taylor polynomials based at 0 are the zero polynomial:

In[37]:=
Clear[f]
f[x_] := \!\(\*
TagBox[GridBox[{
{"\[Piecewise]", GridBox[{
{
RowBox[{"Exp", "[", 
RowBox[{"-", 
FractionBox["1", 
SuperscriptBox["x", "2"]]}], "]"}], 
RowBox[{"x", ">", "0"}]},
{"0", "True"}
},
AllowedDimensions->{2, Automatic},
Editable->True,
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.84]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}},
Selectable->True]}
},
GridBoxAlignment->{"Columns" -> {{Left}}, "ColumnsIndexed" -> {}, "Rows" -> {{Baseline}}, "RowsIndexed" -> {}},
GridBoxItemSize->{"Columns" -> {{Automatic}}, "ColumnsIndexed" -> {}, "Rows" -> {{1.}}, "RowsIndexed" -> {}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.35]}, 
Offset[0.27999999999999997`]}, "ColumnsIndexed" -> {}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}, "RowsIndexed" -> {}}],
"Piecewise",
DeleteWithContents->True,
Editable->False,
SelectWithContents->True,
Selectable->False,
StripWrapperBoxes->True]\)
ResourceFunction["PiecewiseD"][f[x], x, 3] // Simplify
Out[39]=

Illustrate with ResourceFunction["EnhancedPlot"]:

In[40]:=
ResourceFunction["EnhancedPlot"][
  ResourceFunction["PiecewiseD"][f[x], x, 2, "ZeroToNull" -> False], {x, -.5, 3}, PlotRange -> All] // Quiet
Out[40]=

Publisher

Dennis M Schneider

Version History

  • 2.0.1 – 16 August 2022
  • 2.0.0 – 09 August 2022
  • 1.0.0 – 23 September 2020

Related Resources

License Information