Main Content

Conditionally defined expression or function

Define the following piecewise expression by
using `piecewise`

.

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

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

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

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 = [ -1, NaN, 1]

Define the following function symbolically.

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

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

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

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 = [ -1, NaN, 1]

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

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

y(x) = piecewise(x < -2, -2, x in Dom::Interval(-2, 0), 0, 1)

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) yvalues = y(xvalues)

xvalues = -3 -2 -1 0 1 yvalues = [ -2, 1, 0, 1, 1]

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 x
y = piecewise(x<-2, -2, -2<x<2, x, x>2, 2);
fplot(y)
```

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=\{\begin{array}{cc}1/x& x<-1\\ \mathrm{sin}(x)/x& x\ge -1\end{array}$$

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

diffy = piecewise(x < -1, -1/x^2, -1 < x, cos(x)/x - sin(x)/x^2)

Integrate `y`

by using `int`

.

inty = int(y, x)

inty = piecewise(x < -1, log(x), -1 <= x, sinint(x))

Find the limits of `y`

at `0`

and `-1`

by
using `limit`

. Because `limit`

finds
the double-sided limit, the piecewise expression must be defined from
both sides. Alternatively, you can find the right- or left-sided limit.
For details, see `limit`

.

limit(y, x, 0) limit(y, x, -1)

ans = 1 ans = limit(piecewise(x < -1, 1/x, -1 < x, sin(x)/x), x, -1)

Because the two conditions meet at `-1`

, the
limits from both sides differ and `limit`

cannot
find a double-sided limit.

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 sub = pw1 - pw2 mul = pw1 * pw2 div = pw1 / pw2

add = piecewise(x < -1, -3, x in Dom::Interval([-1], 0), -1, 0 <= x, 3) sub = piecewise(x < -1, 1, x in Dom::Interval([-1], 0), 3, 0 <= x, -1) mul = piecewise(x < -1, 2, x in Dom::Interval([-1], 0), -2, 0 <= x, 2) div = piecewise(x < -1, 1/2, x in Dom::Interval([-1], 0), -1/2, 0 <= x, 1/2)

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 = piecewise(x < 0, -1, 0 < x, 1)

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 = piecewise(5 < x, 1/x, x < 0, -1, 0 < x, 1)

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

`and`

| `assume`

| `assumeAlso`

| `assumptions`

| `if`

| `in`

| `isAlways`

| `not`

| `or`