Method on @sym: piecewise (cond1, val1, cond2, val2, …)
Method on @sym: piecewise (cond1, val1, cond2, val2, …, else_val)

Construct piecewise function.

The returned piecewise function evaluates to val1 if cond1 holds, val2 if cond2 holds, … etc. In the case where none of the conditions hold, it evaluates to else_val if provided. If else_val is not provided, it evaluates to nan.

Examples:

syms x real
f = piecewise (abs (x) < 1, exp (- 1 / (1 - x^2)), abs (x) >= 1, 0)
  ⇒ f = (sym)
      ⎧  -1
      ⎪ ──────
      ⎪      2
      ⎨ 1 - x
      ⎪ℯ        for │x│ < 1
      ⎪
      ⎩   0      otherwise

For this piecewise function, we can omit the redundant condition at the end:

syms x real
f = piecewise (abs (x) < 1, exp (- 1 / (1 - x^2)), 0)
  ⇒ f = (sym)
      ⎧  -1
      ⎪ ──────
      ⎪      2
      ⎨ 1 - x
      ⎪ℯ        for │x│ < 1
      ⎪
      ⎩   0      otherwise

See also: if.

Package: symbolic