Main Content

piecewise

Conditionally defined expression or function

Description

pw = piecewise(cond1,val1,cond2,val2,...) returns the piecewise expression or function pw whose value is val1 when condition cond1 is true, is val2 when cond2 is true, and so on. If no condition is true, the value of pw is NaN.

example

pw = piecewise(cond1,val1,cond2,val2,...,otherwiseVal) returns the piecewise expression or function pw that has the value otherwiseVal if no condition is true.

example

Examples

collapse all

Define the following piecewise expression by using piecewise.

y={-1x<01x>0

syms x
y = piecewise(x < 0,-1,x > 0,1)
y = 

{-1 if  x<01 if  0<x

Evaluate y at -2, 0, and 2 by using subs to substitute for x. Because y is undefined at x = 0, the value is NaN.

subs(y,x,[-2 0 2])
ans = (-1NaN1)

Define the following function symbolically.

y(x)={-1x<01x>0

syms y(x)
y(x) = piecewise(x < 0,-1,x > 0,1)
y(x) = 

{-1 if  x<01 if  0<x

Because y(x) is a symbolic function, you can directly evaluate it for values of x. Evaluate y(x) at -2, 0, and 2. Because y(x) is undefined at x = 0, the value is NaN. For details, see Create Symbolic Functions.

y([-2 0 2])
ans = (-1NaN1)

Set the value of a piecewise function when no condition is true (called otherwise value) by specifying an additional input argument. If an additional argument is not specified, the default otherwise value of the function is NaN.

Define the piecewise function

y={-2x<-20-2<x<01otherwise.

syms y(x)
y(x) = piecewise(x < -2,-2,(-2 < x) & (x < 0),0,1)
y(x) = 

{-2 if  x<-20 if  x(-2,0)1 otherwise

Evaluate y(x) between -3 and 1 by generating values of x using linspace. At -2 and 0, y(x) evaluates to 1 because the other conditions are not true.

xvalues = linspace(-3,1,5)
xvalues = 1×5

    -3    -2    -1     0     1

yvalues = y(xvalues)
yvalues = (-21011)

Plot the following piecewise expression by using fplot.

y={-2x<-2x-2<x<22x>2.

syms x
y = piecewise(x < -2,-2,-2 < x < 2,x,x > 2,2);
fplot(y)

Figure contains an axes object. The axes object contains an object of type functionline.

On creation, a piecewise expression applies existing assumptions. Apply assumptions set after creating the piecewise expression by using simplify on the expression.

Assume x > 0. Then define a piecewise expression with the same condition x > 0. piecewise automatically applies the assumption to simplify the condition.

syms x
assume(x > 0)
pw = piecewise(x < 0,-1,x > 0,1)
pw = 1

Clear the assumption on x for further computations.

assume(x,"clear")

Create a piecewise expression pw with the condition x > 0. Then set the assumption that x > 0. Apply the assumption to pw by using simplify.

pw = piecewise(x < 0,-1,x > 0,1);
assume(x > 0)
pw = simplify(pw)
pw = 1

Clear the assumption on x for further computations.

assume(x,"clear")

Differentiate, integrate, and find limits of a piecewise expression by using diff, int, and limit respectively.

Differentiate the following piecewise expression by using diff.

y={1/xx<-1sin(x)/xx-1

syms x
y = piecewise(x < -1,1/x,x >= -1,sin(x)/x);
diffy = diff(y,x)
diffy = 

{-1x2 if  x<-1cos(x)x-sin(x)x2 if  -1<x

Integrate y by using int.

inty = int(y,x)
inty = 

{log(x) if  x<-1sinint(x) if  -1x

Find the limits of y at 0 by using limit.

limit(y,x,0)
ans = 1

Find the right- and left-sided limits of y at -1. For details, see limit.

limit(y,x,-1,"right")
ans = sin(1)
limit(y,x,-1,"left")
ans = -1

Add, subtract, divide, and multiply two piecewise expressions. The resulting piecewise expression is only defined where the initial piecewise expressions are defined.

syms x
pw1 = piecewise(x < -1,-1,x >= -1,1);
pw2 = piecewise(x < 0,-2,x >= 0,2);
add = pw1+pw2
add = 

{-3 if  x<-1-1 if  x[-1,0)3 if  0x

sub = pw1-pw2
sub = 

{1 if  x<-13 if  x[-1,0)-1 if  0x

mul = pw1*pw2
mul = 

{2 if  x<-1-2 if  x[-1,0)2 if  0x

div = pw1/pw2
div = 

{12 if  x<-1-12 if  x[-1,0)12 if  0x

Extract conditions and values from a piecewise expression by using the children function.

syms x
pw = piecewise(x < -2,sin(x),(-2 < x) & (x < 2),x,2 < x,exp(-x))
pw = 

{sin(x) if  x<-2x if  x(-2,2)e-x if  2<x

c = children(pw);
pwExpr = [c{:,1}]
pwExpr = (sin(x)xe-x)
pwCond = [c{:,2}]
pwCond = (x<-2x(-2,2)2<x)

Modify a piecewise expression by replacing part of the expression using subs. Extend a piecewise expression by specifying the expression as the otherwise value of a new piecewise expression. This action combines the two piecewise expressions. piecewise does not check for overlapping or conflicting conditions. Instead, like an if-else ladder, piecewise returns the value for the first true condition.

Change the condition x < 2 in a piecewise expression to x < 0 by using subs.

syms x
pw = piecewise(x < 2,-1,x > 0,1);
pw = subs(pw,x < 2,x < 0)
pw = 

{-1 if  x<01 if  0<x

Add the condition x > 5 with the value 1/x to pw by creating a new piecewise expression with pw as the otherwise value.

pw = piecewise(x > 5,1/x,pw)
pw = 

{1x if  5<x-1 if  x<01 if  0<x

Since R2025a

Define a system of equations that includes piecewise expressions. The first equation defines the variable y for the two conditions 0<x<2 and 2<x<4. The second equation defines xy for the same two conditions.

syms x y
eq1 = y == piecewise((0 < x) & (x < 2),x^2 + x - 2,(2 < x) & (x < 4),3/2*x)
eq1 = 

{y=x2+x-2 if  x(0,2)y=3x2 if  x(2,4)

eq2 = y*x == piecewise((0 < x) & (x < 2),x - 1,(2 < x) & (x < 4),9)
eq2 = 

{xy=x-1 if  x(0,2)xy=9 if  x(2,4)

To solve this system of equations, first substitute the variable y in the second equation with the expression for y from the first equation. You can use the lhs and rhs functions to get the left and right sides of the first equation.

eq2 = subs(eq2,lhs(eq1),rhs(eq1))
eq2 = 

{xx2+x-2=x-1 if  x(0,2)3x22=9 if  x(2,4)

Then, solve the second equation for the variable x.

sols = solve(eq2,x)
sols = 

(162-1)

Here, x=1 and x=2-1 are solutions that satisfy the condition 0<x<2, while x=6 is a solution that satisfies the condition 2<x<4.

Input Arguments

collapse all

Condition, specified as a symbolic condition or variable. A symbolic variable represents an unknown condition.

Example: x > 2

Value when condition is satisfied, specified as a number, vector, matrix, or multidimensional array, or as a symbolic number, variable, vector, matrix, multidimensional array, function, or expression.

Value if no conditions are true, specified as a number, vector, matrix, or multidimensional array, or as a symbolic number, variable, vector, matrix, multidimensional array, function, or expression. If otherwiseVal is not specified, its value is NaN.

Output Arguments

collapse all

Piecewise expression or function, returned as a symbolic expression or function. The value of pw is the value val of the first condition cond that is true. To find the value of pw, use subs to substitute for variables in pw.

Tips

  • piecewise does not check for overlapping or conflicting conditions. A piecewise expression returns the value of the first true condition and disregards any following overlapping true expressions. Thus, piecewise follows the behavior of an if-else statement.

  • In R2025a: You can use the lhs and rhs functions to get the left and right sides of a symbolic equation that includes a piecewise expression.

Version History

Introduced in R2016b

See Also

| | | | | | | |