Piecewise Function

## Description

Piecewise functions in a single variable can be defined in Sage with the command piecewise. For example, suppose that we wish to define the function $f$ by

(1)
\begin{align} f(x) = \begin{cases} \sin x + 1, & x > 0 \\ x^2, & -1 \leq x \leq 0. \end{cases} \end{align}

## Sage Cell

#### Code

f = piecewise([((0,oo), sin(x) + 1), ([-1,0], x^2)]);  f


## Options

#### Option

We can plot piecewise functions. Since plot uses a line plot, notice that the vertical line from $(0,0)$ to $(0,1)$ is plotted.

#### Code

f = piecewise([((0,oo), sin(x) + 1), ([-1,0], x^2)])
plot(f, (x,-1,3))


#### Option

We can find the domain of a function.

#### Code

f = piecewise([((0,oo), sin(x) + 1), ([-1,0], x^2)])
f.domain()


#### Option

We can evaluate functions at points on the domain.

#### Code

f = piecewise([((0,oo), sin(x) + 1), ([-1,0], x^2)])
f(0)


## Tags

Primary Tags—Precalculus:Functions.

Secondary Tags—Functions: Piecewise functions.

None.

## Attribute

Author: T. W. Judson

Date: 03 Jan 2019 22:32

Submitted by: Tom Judson

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License