Wolfram Function Repository
Instantuse addon functions for the Wolfram Language
Function Repository Resource:
The derivative of a piecewise function with Indeterminate for points or regions where the function is not defined
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 k^{th} derivative. 
Compute the derivative of a piecewise function:
In[1]:= 

Out[2]= 

Plot a function together with its piecewise derivative:
In[3]:= 

Out[3]= 

Compute the first two derivatives of a function whose domain is not an interval:
In[4]:= 

Out[6]= 

Compute just the second derivative:
In[7]:= 

Out[7]= 

Plot the function together with its first two derivatives:
In[8]:= 

Out[8]= 

Find and plot the first and secondorder derivatives. The function and its firstorder derivative are continuous at x=0, but not the secondorder derivative:
In[9]:= 

Out[10]= 

Check that the first derivative is continuous:
In[11]:= 

Out[11]= 

Check that the second derivative is not continuous:
In[12]:= 

Out[12]= 

Plot the results:
In[13]:= 

Out[13]= 

The following function has a removable discontinuity at x=2 and an infinite discontinuity at x=4:
In[15]:= 

Out[16]= 

Extend the definition at x=2 to make the extended function continuous there:
In[17]:= 

Out[17]= 

In[18]:= 

Out[19]= 

The extended function is actually differentiable at x=2:
In[20]:= 

Out[20]= 

The resource function EnhancedPlot produces a correct plot:
In[21]:= 

Out[21]= 

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[22]:= 

Out[23]= 

In[24]:= 

Out[24]= 

However, the derivative is not continuous:
In[25]:= 

Out[25]= 

This function is differentiable at x=0 and its derivative is continuous there:
In[26]:= 

Out[28]= 

In[29]:= 

Out[29]= 

Plot the result using the resource function EnhancedPlot:
In[30]:= 

Out[30]= 

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[31]:= 

Out[31]= 

The resource function EnhancedPlot is able to produce a correct plot:
In[32]:= 

Out[32]= 

Extend the function so that it becomes continuous at 1 and 1:
In[33]:= 

Out[33]= 

In[34]:= 

The first and second derivatives are continuous at ±1:
In[35]:= 

Out[35]= 

In[36]:= 

Out[36]= 

Plot the extended function and its first two derivatives:
In[37]:= 

Out[37]= 

This work is licensed under a Creative Commons Attribution 4.0 International License