Finding a tricky composition of two piecewise functions I have a question about finding the formula for a composition of two piecewise functions. The functions are defined as follows:
$$f(x) =
\begin{cases}
2x+1,  & \text{if $x \le 0$} \\
x^2, & \text{if $x > 0$}
\end{cases}$$
$$g(x) =
\begin{cases}
-x,  & \text{if $x < 2$} \\
5, & \text{if $x \ge 2$}
\end{cases}$$
My main question lies in how to approach finding the formula for the composition $g(f(x))$. I have seen a couple of other examples of questions like this online, but the domains of each piecewise function were the same, so the compositions weren't difficult to determine. 
In this case, I have assumed that, in finding $g(f(x))$, one must consider only the domain of $f(x)$. Thus, I think it would make sense to test for individual cases: for example, I would try to find $g(f(x))$ when $x <= 0$. $g(f(x))$ when $x <= 0$ would thus be $-2x-1$, right? However, I feel like I'm missing something critical, because I'm just assuming that the condition $x < 2$ for $g(x)$ can just be treated as $x <= 0$ in this case. Sorry for my rambling, and many thanks for anyone who can help lead me to the solution.
 A: The first branch point is associated with $f$, and happens at $x=0$.  We note that:  $$g\circ f(x) =
\begin{cases}
g(2x+1),  & \text{if $x≤ 0$} \\
g(x^2), & \text{if $x$ > 0}
\end{cases}$$
Let's just consider each case separately.  First, $g(2x+1)$ when $x≤0$.  We remark that $x≤0\implies 2x≤0\implies 2x+1≤1$ so we see that $x≤0\implies g(2x+1)=-2x-1$.
Now consider $g(x^2)$ when $x>0$.  We see that we hit a branch point at $x=\sqrt 2$.  Specifically, $x<\sqrt 2\implies x^2<2$ so $x<\sqrt 2\implies g(x^2)=-x^2$.  Of course $x≥\sqrt 2 \implies x^2≥2$ so $x≥\sqrt 2\implies g(x^2)= 5$.  Combining all this we see that $$g\circ f(x) =
\begin{cases}
-2x-1,  & \text{if $x$ ≤ 0} \\
-x^2, & \text{if $0< x < \sqrt 2$}\\
5 & \text{if $x≥\sqrt 2$}
\end{cases}$$
As a sanity check, we try the special case $x=1$.  We note that $g\circ f(1)=g(f(1))=g(1)=-1$ as desired.  I advise picking a few other special values just to check.
A: $$g(x) = \begin{cases} -x, & x < 2 \\ 5, & x \ge 2 \end{cases} $$
Therefore
$$g(f(x)) = \begin{cases} -f(x), & f(x) < 2 \\ 5, & f(x) \ge 2 \end{cases} $$
So now we need to know when $f(x) < 2$ and when $f(x) \ge 2$.
$$f(x) = \begin{cases} 2x + 1, & x \le 0 \\ x^2, & x > 0 \end{cases} $$
Let's look at one piece of $f$ at a time.  On the first piece, $2x + 1 \ge 2$ means $x \ge 1/2$.  But this is impossible because $x \le 0$ on the first piece.  Also on the first piece, $2x + 1 < 2$ means $x < 1/2$.  Well, on the first piece we have $x \le 0 < 1/2$, therefore $f(x) < 2$ on the entire first piece, i.e., $f(x) < 2$ if $x \le 0$.
On the second piece, $x^2 \ge 2$ means $x \ge \sqrt{2}$ or $x \le -\sqrt{2}$.  On the second piece we always have $x > 0$, therefore we have $x^2 \ge 2$ when $x \ge \sqrt{2}$.  Also on the second piece, $x^2 < 2$ means $-\sqrt{2} < x < \sqrt{2}$.  And since on the second piece we always have $x > 0$, then we must have $0 < x < \sqrt{2}$ in order to have $x^2 < 2$.
Putting it all together so far, we have the following:
$$ f(x) \ge 2 \text{ if and only if } x \ge \sqrt{2}$$
$$ f(x) < 2 \text{ if and only if } x \le 0 \text{ or } 0 < x < \sqrt{2} $$
Notice that this last one can be simplified but we need to keep them separate.  Why is this?  We'll see as we continue.  Recall:
$$g(f(x)) = \begin{cases} -f(x), & f(x) < 2 \\ 5, & f(x) \ge 2 \end{cases} $$
Therefore:
$$g(f(x)) = \begin{cases} -f(x), & x \le 0 \text{ or } 0 < x < \sqrt{2} \\ 5, & x \ge \sqrt{2} \end{cases} $$
Separating the conditions gives us:
$$g(f(x)) = \begin{cases} -f(x), & x \le 0\\ -f(x), & 0 < x < \sqrt{2} \\ 5, & x \ge \sqrt{2} \end{cases} $$
And we need to do this because $f(x)$ itself is different for $x \le 0$ and $0 < x < \sqrt{2}$.  Finally, we end up with:
$$ h(x) := g(f(x)) = \begin{cases} -(2x+1), & x \le 0 \\ -x^2, & 0 < x < \sqrt{2} \\ 5, & x \ge \sqrt{2} \end{cases} $$
A: You're correct about the value of $g(f(x))$ when $x\leq 0$; since $f(x)$ will be at most $2\cdot0+1=1$, $g$ is only going to evaluate $f(x)$ according to the definition for $x<2$. Testing for cases here is a good approach, and you've just resolved the $x\leq0$ case. When $x>0$, consider the values of $f(x)$: when will they be less than $2$, and when will they be greater? This will determine where $g(f(x))$ takes on its values.
A: $$
g(x) =
\begin{cases}
-x,  & \text{if $x < 2$} \\
5, & \text{if $x \ge 2$}
\end{cases}
$$
In finding $g(f(x))$ with $g$ defined as above, one should remember that
$$
g(f(x)) =
\begin{cases}
-x,  & \text{if $f(x) < 2$} \\
5, & \text{if $f(x) \ge 2$}
\end{cases}
$$
with $f(x)$, not $x$, being the thing about which one asks when it is $<2$ and when it is $\ge 2$.
(The calculus text by Salas and Hille had a problem like this that used to get assigned in every academic term by vast numbers of professors who taught the course to thousands of students.  And every time an hour would be spent answering the students questions about it, which in every case said something like "I didn't even know where to start", and that's an hour that could have been spent on something that would help the students understand calculus. It was an even worse waste of time and effort than what usually goes on in the kind of math courses where the students don't want to learn math.)
