# Define a graph with segments or boundaries

This question is quite simple relative to the normal content on this site. (and please rename the title as I don't know what the proper definition is)

Is it possible to have a function such as:

$y = x^2$

but to also state that from negative infinite 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

-
 I've removed the graph theory tag, because it refers to this kind of graph theory. – Zev Chonoles Jul 6 '11 at 21:50 @Chris: While you can definitely define this function $y$, note that it is not continuous at $x=\pm 10$, as $(\pm 10)^2 \neq 7$. More specifically, $y$ has a jump discontinuity at the points $x=\pm 10$. – Shai Covo Jul 6 '11 at 21:59

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}$$

-
 That definitely is what I'm looking for. – Chris Jul 6 '11 at 22:23

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.

-

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

-
 It's funny you mention computer graphics - thats the intent. I'm wandering now if it's possible to flatten out that function Zev posted into a 'flat' function that I can use in a programming language - without doing the conditional checks seperately – Chris Jul 7 '11 at 1:14 @Chris: Answer is a conditional yes, depends substantially on the primitives available in your language. I have seen similar questions, fairly recently, on this site. Do post a specific question close to your needs. Chances are excellent you will get a very well-informed answer. – André Nicolas Jul 7 '11 at 1:46