Conditionally defined expression or function

collapse all in page

## Syntax

`pw = piecewise(cond1,val1,cond2,val2,...)`

`pw = piecewise(cond1,val1,cond2,val2,...,otherwiseVal)`

## Description

example

`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.

## Examples

collapse all

### Define and Evaluate Piecewise Expression

Open Live Script

Define the following piecewise expression by using `piecewise`

.

$$\mathit{y}=\{\begin{array}{ll}-1& \mathit{x}<0\\ 1& \mathit{x}>0\end{array}$$

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

y =$$\{\begin{array}{cl}-1& \text{if}x<0\\ 1& \text{if}0<x\end{array}$$

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 =$$\left(\begin{array}{ccc}-1& \mathrm{NaN}& 1\end{array}\right)$$`

### Define Piecewise Function

Open Live Script

Define the following function symbolically.

$$\mathit{y}\left(\mathit{x}\right)=\{\begin{array}{ll}-1& \mathit{x}<0\\ 1& \mathit{x}>0\end{array}$$

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

y(x) =$$\{\begin{array}{cl}-1& \text{if}x<0\\ 1& \text{if}0<x\end{array}$$

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 =$$\left(\begin{array}{ccc}-1& \mathrm{NaN}& 1\end{array}\right)$$`

### Set Value When No Condition Is True

Open Live Script

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=\{\begin{array}{cc}-2& x<-2\\ 0& -2<x<0\\ 1& otherwise.\end{array}$$

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

y(x) =$$\{\begin{array}{cl}-2& \text{if}x<-2\\ 0& \text{if}x\in \left(-2,0\right)\\ 1& \mathrm{otherwise}\end{array}$$

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 =$$\left(\begin{array}{ccccc}-2& 1& 0& 1& 1\end{array}\right)$$`

### Plot Piecewise Expression

Open Live Script

Plot the following piecewise expression by using `fplot`

.

$$y=\{\begin{array}{cc}-2& x<-2\\ x& -2<x<2\\ 2& x>2\end{array}.$$

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

### Assumptions and Piecewise Expressions

Open Live Script

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 xassume(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 Piecewise Expression

Open Live Script

Differentiate, integrate, and find limits of a piecewise expression by using `diff`

, `int`

, and `limit`

respectively.

Differentiate the following piecewise expression by using `diff`

.

$$\mathit{y}=\{\begin{array}{ll}1/\mathit{x}& \mathit{x}<-1\\ \mathrm{sin}\left(\mathit{x}\right)/\mathit{x}& \mathit{x}\ge -1\end{array}$$

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

diffy =$$\{\begin{array}{cl}-\frac{1}{{x}^{2}}& \text{if}x<-1\\ \frac{\mathrm{cos}\left(x\right)}{x}-\frac{\mathrm{sin}\left(x\right)}{{x}^{2}}& \text{if}-1<x\end{array}$$

Integrate `y`

by using `int`

.

inty = int(y,x)

inty =$$\{\begin{array}{cl}\mathrm{log}\left(x\right)& \text{if}x<-1\\ \mathrm{sinint}\left(x\right)& \text{if}-1\le x\end{array}$$

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 =$$\mathrm{sin}\left(1\right)$$`

`limit(y,x,-1,'left')`

`ans =$$-1$$`

### Elementary Operations on Piecewise Expressions

Open Live Script

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

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

add =$$\{\begin{array}{cl}-3& \text{if}x<-1\\ -1& \text{if}x\in \left[-1,0\right)\\ 3& \text{if}0\le x\end{array}$$

sub = pw1-pw2

sub =$$\{\begin{array}{cl}1& \text{if}x<-1\\ 3& \text{if}x\in \left[-1,0\right)\\ -1& \text{if}0\le x\end{array}$$

mul = pw1*pw2

mul =$$\{\begin{array}{cl}2& \text{if}x<-1\\ -2& \text{if}x\in \left[-1,0\right)\\ 2& \text{if}0\le x\end{array}$$

div = pw1/pw2

div =$$\{\begin{array}{cl}\frac{1}{2}& \text{if}x<-1\\ -\frac{1}{2}& \text{if}x\in \left[-1,0\right)\\ \frac{1}{2}& \text{if}0\le x\end{array}$$

### Modify or Extend Piecewise Expression

Open Live Script

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 xpw = piecewise(x < 2,-1,x > 0,1);pw = subs(pw,x < 2,x < 0)`

pw =$$\{\begin{array}{cl}-1& \text{if}x<0\\ 1& \text{if}0<x\end{array}$$

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 =$$\{\begin{array}{cl}\frac{1}{x}& \text{if}5<x\\ -1& \text{if}x<0\\ 1& \text{if}0<x\end{array}$$

## Input Arguments

collapse all

`cond`

— Condition

symbolic condition | symbolic variable

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

**Example: **x > 2

`val`

— Value when condition is satisfied

number | vector | matrix | multidimensional array | symbolic number | symbolic variable | symbolic vector | symbolic matrix | symbolic multidimensional array | symbolic function | symbolic expression

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.

`otherwiseVal`

— Value if no conditions are true

number | vector | matrix | multidimensional array | symbolic number | symbolic variable | symbolic vector | symbolic matrix | symbolic multidimensional array | symbolic function | symbolic 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

`pw`

— Piecewise expression or function

symbolic expression | symbolic function

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 true expressions. Thus,`piecewise`

mimics an if-else ladder.

## Version History

**Introduced in R2016b**

## See Also

and | assume | assumeAlso | assumptions | if | in | isAlways | not | or

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français

- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本 (日本語)
- 한국 (한국어)

Contact your local office