Piecewise Functions
This worksheet contains a number of examples of the use of the piecewise function.
restart
with⁡plots:
Some Simple Examples
The piecewise function has a straightforward syntax. Every piece is specified by a Boolean condition followed by an expression. The next several Maple command lines make use of the following piecewise function:
f:=piecewise⁡x≤−1,−x,x≤1,x2,1<x,sin⁡x−1x−1
f≔−xx≤−1x2x≤1sin⁡x−1x−11<x
This can be evaluated at arbitrary points. For example,
fx=−2.|fx=−2.
2.
fx=3.|fx=3.
0.4546487134
Because of the division by zero, points such as x = 1 cannot be substituted. However, limx→1f can be determined.
limx→1f
1
Such functions can be plotted to determine their behavior.
plot⁡f,x=−2..2
Besides evaluating limits, you can do operations such as computing derivatives, integrating, and solving differential equations with piecewise functions. Examples of solving DEs will be illustrated later. Using the same function, f(x), find its piecewise derivative.
fprime≔ⅆⅆ x f
fprime≔−1x<−1undefinedx=−12⁢xx<1undefinedx=1cos⁡x−1x−1−sin⁡x−1x−121<x
Integrating this function gives:
∫fⅆx
−x22x≤−1x33−16x≤1Si⁡x−1+161<x
Evaluate the integral with values for the lower and upper bounds.
∫−23fⅆx
136+Si⁡2
where Si(x) is the Sine integral function.
Note: This works because discont is able to determine the potential discontinuities of piecewise functions. For example,
discont⁡f,x
∅
Consider a new function, g(x).
g≔f ⋅ piecewisex<0,x,ⅇ−x
g≔−xx≤−1x2x≤1sin⁡x−1x−11<x⁢xx<0ⅇ−xotherwise
The piecewise function allows for common manipulations, such as simplifications. The addition of the selector 'piecewise' indicates to simplify that it should only do simplifications as they apply to piecewise functions. This is more efficient, in general.
simplify⁡g,'piecewise'
−x2x≤−1x3x<0x2⁢ⅇ−xx≤1sin⁡x−1⁢ⅇ−xx−11<x
Piecewise functions can be converted to a number of other functions and distributions.
heavyf:=convert⁡f,'Heaviside'
heavyf≔x⁢Heaviside⁡x+1−x−x2⁢Heaviside⁡x−1+x2⁢Heaviside⁡x+1+sin⁡x−1⁢Heaviside⁡x−1x−1
Distributions can be converted back to piecewise functions.
normal⁡convert⁡heavyf,'piecewise'
−xx<−1undefinedx=−1x2x<1−x3⁢undefined+x2⁢undefined+x3+sin⁡x−1⁢undefined−x2x−1x=1sin⁡x−1x−11<x
The function piecewise lets us work with the CurveFitting[Spline] command. For example,
newcubic≔CurveFittingSpline0,1,2,3,0,1,4,3,x
newcubic≔45⁢x3+15⁢xx<1−2⁢x3+425⁢x2−415⁢x+145x<265⁢x3−545⁢x2+1515⁢x−1145otherwise
This spline can be graphed.
plot⁡newcubic,x=−1..3
But now we can also integrate it.
assume2<a
∫−1anewcubic ⅆx
595+310⁢a~4−185⁢a~3+15110⁢a~2−1145⁢a~
Other piecewise functions can also be converted to piecewise and be properly manipulated. For example,
convert⁡1−x,'piecewise'
−x−1x≤−1x+1x<01−xx<1x−11≤x
Another example of conversion, in this case with signum and abs.
convert⁡−signum⁡x⁢1−x,'piecewise'
−x−1x≤−1x+1x<00x=0x−1x<11−x1≤x
Note: The discontinuity at 0 in the above function.
Another simple example:
h:=convert⁡x2,'piecewise'
h≔x2
It turns out to be a well-behaved, non-piecewise function.
We can also determine the differentiability class of a piecewise continuous function. For example, in the case of our previous spline function, newcubic, we have
isdifferentiable⁡newcubic,x,2
true
This must be true for splines! However, when we check to see if it is C3, we obtain
isdifferentiable⁡newcubic,x,3,'badpoints'
false
To determine the highest order of continuity and the problem points, enter:
badpoints
2,1,2
Converting to Piecewise Functions
Define f(x) as:
f:=max⁡x2−2,x−1
f≔max⁡x−1,x2−2
Derivatives can be found, and piecewise functions are returned.
fprime≔2⁢xx<−52+12undefinedx=−52+121x<52+12undefinedx=52+122⁢x52+12<x
The graph of the derivative can be drawn as well.
plot⁡fprime,x=−2..2,'discont=true'
The straightforward calculation of the series of f around x=0 can be computed by using the series command.
series⁡f,x
−1+x+O⁡x6
This produces an answer with a superfluous order term. However, series can do better when using piecewise. Thus, it can be converted to a new function g(x).
g:=convert⁡f,'piecewise'
g≔x2−2x≤−52+12x−1x≤52+12x2−252+12<x
series⁡g,x
−1+x
We can then also plot this max function.
maxplot:=plot⁡g,x=−2..2,color=red:
maxplot
LaTeX output can also be generated.
latex⁡g
\left\{\begin{array}{cc} x^{2}-2 & x \le -\frac{\sqrt{5}}{2}+\frac{1}{2} \\ x -1 & x \le \frac{\sqrt{5}}{2}+\frac{1}{2} \\ x^{2}-2 & \frac{\sqrt{5}}{2}+\frac{1}{2}<x \end{array}\right.
Similarly, everything also works with min.
h:=convert⁡min⁡x2−2,x−1,'piecewise'
minplot:=plot⁡h,x=−2..2,color=green:
display⁡maxplot,minplot
h≔x−1x≤−52+12x2−2x≤52+12x−152+12<x
Converting to piecewise also works with _Envsignum0 := 1;
_Envsignum0:=1
_Envsignum0≔1
convert⁡signum⁡x,'piecewise'
−1x<010≤x
ⅆⅆ x⁡
undefinedx=00otherwise
Solving Differential Equations with Piecewise
We can solve differential equations with piecewise functions in the coefficients. The type of equations that one can solve include all first-order methods using integration, Riccati, and higher-order methods including linear, Bernoulli, and Euler. Some examples are included in the sections that follow.
First-order Linear with Piecewise (Nontrivial) Coefficients
Consider the following DE:
eq:=ⅆⅆx⁢y⁡x+piecewise⁡x<x2−4,ⅇx⁢y⁡x
eq≔ⅆⅆxy⁡x+ⅇxx<x2−40otherwise⁢y⁡x
Now solve this DE.
dsolve⁡eq,y⁡x
y⁡x=ⅇ−ⅇx⁢c__1x<12−172ⅇ−ⅇ12−172⁢c__1x<12+172ⅇ−ⅇ12−172−ⅇx+ⅇ12+172⁢c__112+172≤x
Now an example of a linear equation with polynomial coefficients and 0-1 perturbation.
eq:=ⅆ2ⅆx2⁢y⁡x+2⁢x⁢ⅆⅆx⁢y⁡x+2⁢y⁡x=piecewise⁡0<x,1
eq≔ⅆ2ⅆx2y⁡x+2⁢x⁢ⅆⅆxy⁡x+2⁢y⁡x=10<x0otherwise
Solving the equation gives:
y⁡x=ⅇ−x2⁢erfi⁡x⁢c__1+ⅇ−x2⁢c__2x<0ⅇ−x2⁢erfi⁡x⁢c__1+ⅇ−x2⁢c__2+ⅇ−x2⁢ⅇx22−ⅇ−x220≤x
Now we attempt to solve a DE with y(x) inside the piecewise function.
eq:=ⅆⅆx⁢y⁡x=piecewise⁡1<x,y⁡x,1
eq≔ⅆⅆxy⁡x=y⁡x1<x1otherwise
y⁡x=c__1+xx<1ⅇx−1⁢c__1+ⅇx−11≤x
And now solve a nonlinear differential equation with discontinuities depending on parameters. The assumption is needed because this function is discontinuous in 0 and a.
assumea>0:
g:=convert⁡−signumx ⋅⁢a−x,'piecewise',x
g≔−a~−xx≤−a~a~+xx<0−a~+xx<a~a~−xa~≤x
dsolve⁡ⅆⅆx⁢yx=g ⋅⁢yx2,yx
y⁡x=22⁢x⁢a~+x2+2⁢c__1x<−a~2−2⁢a~2−2⁢x⁢a~−x2+2⁢c__1x<02−2⁢a~2+2⁢x⁢a~−x2+2⁢c__1x<a~2−2⁢x⁢a~+x2+2⁢c__1a~≤x
Linear DE with Constant Coefficients and Piecewise Forcing Function
eq:=ⅆ2ⅆx2⁢y⁡x+y⁡x=piecewise⁡x<0,−x,0<x,x
eq≔ⅆ2ⅆx2y⁡x+y⁡x=−xx<0x0<x
The solution with given initial values:
dsolve⁡y⁡0=0,D⁡y⁡0=1,eq,y⁡x
y⁡x=2⁢sin⁡x−xx<0x0≤x
The general solution:
y⁡x=sin⁡x⁢c__2+cos⁡x⁢c__1−xx<0sin⁡x⁢c__2+cos⁡x⁢c__1+x−2⁢sin⁡x0≤x
This also works in the case of output = basis.
dsolve⁡eq,y⁡x,output=basis
cos⁡x,sin⁡x,−xx≤02⁢sin⁡x−x0<x
Nonlinear Riccati DE with Piecewise Continuous Coefficients
Consider the following differential equation.
eq:=ⅆⅆx⁢yx=piecewisex>0,x⋅yx2
eq≔ⅆⅆxy⁡x=x0<x0otherwise⁢y⁡x2
eq_sol:=dsolve⁡eq,y⁡x
eq_sol≔y⁡x=1c__1x<0−2x2−2⁢c__10≤x
For more information, see the piecewise help page. See also the following help pages: series, max, min, int, dsolve, discont, CurveFitting[Spline], signum, and abs.
Return to Index for Example worksheets
Download Help Document