Define a graph with segments or boundaries
Is it possible to have a function such as:
$y = x^2$
but to also state that from negative infinity to -10 and from 10 to positive infinite that
$y = 7$
This is a simple example, but I just want to find out if it's possible to combine different functions into one.
Cheers
Solution 1:
I'll leave the construction of an arbitrary piecewise function here for reference. (I used the construction here, but it's certainly useful for more than just constructing superhero logos. ;) )
The key is to use the Iverson bracket, denoted $[p]$, for some condition $p$; this evaluates to $1$ if $p$ is true or $0$ if $p$ is false. For instance, you can express the unit step function in terms of Iverson brackets:
$$[x\geq0]=\begin{cases}1&\text{if }x\geq0\\0&\text{if }x<0\end{cases}$$
The other property of Iverson brackets that we need here is $[\neg p]=1-[p]$.
Now, consider the piecewise-defined function
$$\begin{cases}f(x)&\text{if }x<a\\g(x)&\text{if }a\leq x\end{cases}$$
Since $x \geq a$ and $x - a \geq 0$ are the same thing, and $\neg(x < a)$ and $x \geq a$ are the same thing, we can use the properties of Iverson brackets to yield the equivalent function
$$\begin{align*}f(x)[x-a<0]+g(x)[x-a\geq 0]&=f(x)(1-[x-a\geq 0])+g(x)[x-a\geq 0]\\&=f(x)+(g(x)-f(x))[x-a\geq 0]\end{align*}$$
For a more complicated construction, consider the piecewise function
$$\begin{cases}f(x)&\text{if }x<a\\g(x)&\text{if }a\leq x < b\\h(x)&\text{if }b\leq x\end{cases}$$
This can be expressed in Iverson form as
$$f(x)[x<a]+g(x)[a\leq x][x < b]+h(x)[b\leq x]$$
(where we used $[p\text{ and }q]=[p][q]$), or
$$f(x)(1-[x-a\geq 0])+g(x)[x-a\geq 0](1-[x-b\geq 0])+h(x)[x-b\geq 0]$$
which can be simplified like so:
$$f(x)+(g(x)-f(x))[x-a\geq 0]-g(x)[x-a\geq 0][x-b\geq 0]+h(x)[x-b\geq 0]$$
and since $[x-a\geq 0][x-b\geq 0]=[x-b\geq 0]$ for $a \leq b$, we finally have the expression
$$f(x)+(g(x)-f(x))[x-a\geq 0]+(h(x)-g(x))[x-b\geq 0]$$
The extension to more than three pieces is straightforward.
We can apply all this to the function
$$f(x)=\begin{cases}7&\text{if }|x|\geq 10\\x^2&\text{if }|x|<10\end{cases}$$
which can also be expressed as
$$\begin{cases}7&\text{if }x\leq -10\\x^2&\text{if }-10 < x < 10\\7&\text{if }10\leq x\end{cases}$$
Applying the results of the previous section (for now being sloppy with the values at the points of discontinuity), we have
$$f(x)=7+(x^2-7)[x+10\geq 0]+(7-x^2)[x-10\geq 0]$$
(If you are really careful with putting in the Iverson brackets, and carefully pay attention to values at the joining points, the expression required is actually $x^2+(7-x^2)([x-10\geq 0]+[-x-10\geq 0])$, whose derivation I leave as an exercise (a matter of choosing whether to use $[x\geq 0]$ or $[x > 0]=1-[-x \geq 0]$). The sloppiness does not matter so much if the "pieces" mutually agree at the breakpoints, however.)
If you're working in an environment like C/++ where Iverson brackets are implicitly supported (TRUE
is 1
and FALSE
is 0
), then you should be content and happy with Iverson bracket expressions. If not, the unit step function implicit in the previous expression can be "replaced" (ignoring for the moment the case when $x=0$) like so:
$$[x\geq 0]=\frac{x+|x|}{2x}$$
Sometimes, the problem at the points of discontinuity resolves itself after simplification of the expression, post-substitution. Sometimes, it doesn't. Therefore, when replacing your Iverson brackets/unit step functions (or even in the construction proper), you always must be careful to check that your function behaves as it's supposed to behave at the joining points.
Solution 2:
Yes, this is called a piecewise function. You can define the function in your question as: $$y=f(x)=\begin{cases} 7\text{ if }|x|\geq 10\\ x^2\text{ if }|x|<10\end{cases}$$
Solution 3:
There is a widespread superstition (but not among mathematicians!) that "piecewise functions" are not "real" functions. They are indeed functions, and indispensable with. They come up naturally in many applications, from physics to probability theory to computer graphics (look in Wikipedia under splines).