piecewise
piecewise-continuous functions
Calling Sequence
Parameters
Description
Examples
piecewise(cond_1, f_1, cond_2, f_2, ..., cond_n, f_n, f_otherwise)
f_i
-
expression
cond_i
relation or boolean combination of inequalities
f_otherwise
(optional) default expression
With the piecewise function it is possible to express piecewise-continuous functions. The semantics are as in a case statement: if cond_1 is true then f_1, else if cond_2 is true then f_2, and so on. f_otherwise gives a default case which corresponds to all conditions being false. The default for f_otherwise is 0.
A condition can be a single equality, inequality, or inequation, or a boolean combination thereof, such as x<3 or And(x>0, x<=Pi) (which is the same as 0<x∧x≤π). The conditions can contain relations with polynomials, abs, signum, or piecewise functions, such as 0<x2−4∧0<x or x<4. The left and right-hand sides of inequalities are assumed to be real quantities.
When using boolean combinations of relations, the use of boolean functions (such as And and Or) in piecewise functions is recommended over using boolean operators (such as and and or). While it is possible to use the operators if all relations involved are inequalities, or if all variables have been assigned values, using operators with equations or inequations involving symbolic parameters will lead to unexpected results. For more details, see the documentation of the evalb command.
The piecewise function can be differentiated, integrated, simplified, plotted, and used in the following types of differential equations: constant coefficients and discontinuous perturbation function, general first-order linear, Riccati, and some other classes which are handled by integration or variation of parameter. See dsolve/piecewise for more details. series, limit, abs, and signum can handle the piecewise function.
If parameters are involved in the conditions, the system requires useful assumptions in order to perform computations. For example, piecewise(a*x<1,f(x)) cannot be manipulated unless Maple can determine whether a is positive or negative using the assume system. See "What Assumptions" in the Context Panel.
There exist convert procedures to convert a piecewise function to an expression containing the Heaviside function and vice versa. A piecewise function can also be converted to a list representation, called pwlist. Expressions containing abs or signum can be converted to piecewise functions.
To enter a piecewise function in 2-D Math notation, you can use either the palettes or command completion. To add an additional line to this piecewise function, press Ctrl + Shift + R. See 2-D Math Shortcut Keys and Hints for more information. Alternatively, with your cursor in a row of the piecewise function, from the Context Panel select Insert Row Above or Insert Row Below.
The piecewise function evaluates its arguments on an as-needed basis. For example, if the first argument of a 3-argument call evaluates to true, the second argument is then evaluated and returned; the third argument is never evaluated.
An exception to the above is if piecewise is passed a single argument. That argument is evaluated, and if it yields an expression sequence, the members of that sequence are used as the actual arguments to piecewise. No further evaluation of the arguments takes place.
piecewise⁡0<x,x
x0<x0otherwise
The plot of a piecewise function.
plot⁡piecewise⁡x<2,x2,2≤x,−3⁢x+6,x=0..6
eq1≔piecewise⁡And⁡4<x2,x<8,f⁡x
eq1≔f⁡x4<x2∧x<80otherwise
simplify⁡eq1
f⁡xx<−20x≤2f⁡xx<808≤x
Piecewise functions can have parameters in the conditions.
assume⁡a<b,b<c
eq2≔piecewise⁡And⁡a<x,x<b,1,And⁡b<x,x<c,2
eq2≔1a~<x∧x<b~2b~<x∧x<c~
convert⁡eq2,piecewise,x
0x≤a~1x<b~0x=b~2x<c~0c~≤x
assume⁡d<0:
eq3≔piecewise⁡0<x⁢d−2,1,2
eq3≔10<d~⁢x−22otherwise
convert⁡eq3,piecewise,x
1x<2d~22d~≤x
Using boolean operators in piecewise conditions can lead to unexpected results, because the conditions are evaluated according to the rules used by the evalb command.
piecewise⁡x=0orx=1,a,b
b~
piecewise⁡x≠0and0<y,a,b
a~0<yb~otherwise
piecewise⁡n::integerand3<n,n2,n3
n3
x=0orx=1
false
x≠0and0<y
0<y
n::integerand3<n
Using the boolean functions And, Or, and Not instead is recommended. This delays evaluation of the conditions until their truth or falsehood can be determined definitively.
f≔piecewise⁡Or⁡x=0,x=1,a,b
f≔a~x=0∨x=1b~otherwise
fassuming0<x
a~x=1b~otherwise
g≔piecewise⁡And⁡x≠0,0<y,a,b
g≔a~x≠0∧0<yb~otherwise
gassuming0<y
a~x≠0b~otherwise
h≔piecewise⁡And⁡n::integer,3<n,n2,n3
h≔n2n::ℤ∧3<nn3otherwise
hassuming3<n
n2n::ℤn3otherwise
Piecewise functions can be simplified.
p≔piecewise⁡x<0,−x,0<x,x
p≔−xx<0x0<x
eq4≔p2+5
eq4≔−xx<0x0<x2+5
simplify⁡eq4
x2+5
l≔piecewise⁡x<0,x2,0<x,x
l≔x2x<0x0<x
eq5≔l2+5
eq5≔x2x<0x0<x2+5
simplify⁡eq5
x4x≤020<x+5
m≔piecewise⁡x<a,x,b<x,x2
m≔xx<a~x2b~<x
eq6≔simplify⁡m2
eq6≔x2⁢1x<a~xb~<x2
However, if the intent is to convert to a single piecewise function, convert should be used instead:
convert⁡eq6,piecewise,x
x2x<a~0x≤b~x4b~<x
Other operations involving piecewise functions.
k≔x↦piecewise⁡x<0,−1,x<1,0,1
k≔x↦−1x<00x<11otherwise
k⁡12
0
j≔piecewise⁡x<0,−x2,0<x,x2
j≔−x2x<0x20<x
diff⁡j,x
−2⁢xx≤02⁢x0<x
int⁡j,x
−x33x≤0x330<x
eval⁡j,x=2
4
eval⁡j,x=−2
−4
See Also
combine/piecewise
convert/Heaviside
convert/piecewise
convert/pwlist
dsolve/piecewise
evalb
Heaviside
simplify/piecewise
Download Help Document