Composition of piecewise functions - Strange result I'm trying to get $f(g(x))$, where:
$$
f(x)=
\begin{cases}
\sqrt{1-x} &\text{if } x \leq 1 \\
x &\text{if } x > 1
\end{cases}
$$
$$
g(x)=
\begin{cases}
x + 1 &\text{if } x \geq 0 \\
x^2 &\text{if } x < 0
\end{cases}
$$
I
followed these steps:

*

*$g(x \geq 0) = x + 1$ hence:
$$
f(g(x \geq 0)) = 
\begin{cases}
\sqrt{1 - (x + 1)} = \sqrt{-x}  &\text{if } 0 \leq x \leq 1 \\
x + 1 &\text{if } x > 1
\end{cases}
$$
but $\sqrt{-x}$ is not real! Is it correct? if not, what is the right result?


*$g(x < 0) = x^2$ hence:

$$
f(g(x < 0)) = \sqrt{1 - x^2} \quad \text{if } x < 0
$$

EDIT: steps to get the @JoséCarlosSantos solution
$
\boldsymbol{g(x) \leq 1}
$
$$
\begin{cases}
x + 1 \leq 1 \\
x^2 \leq 1
\end{cases}
$$
$$
\begin{cases}
x \leq 0 \\
-1 \leq x \leq 1
\end{cases}
$$
so $-1 \leq x \leq 0$.
$
\boldsymbol{g(x) > 1}
$
$$
\begin{cases}
x + 1 > 1 \\
x^2 > 1
\end{cases}
$$
$$
\begin{cases}
x > 0 \\
x < - 1 \text{ or } x > 1
\end{cases}
$$
this means that I can split it in two subsystems:

*

*$$ \begin{cases} x > 0 \\ x < - 1 \end{cases} $$ this system does not have any solution;

*$$
\begin{cases}
x > 0 \\
x > 1
\end{cases}
$$ the solution is $x > 1$.

Putting together the results of the two subsystems: $x > 1$. I made a mistake, the right result should be $x < - 1$ or $x > 0$.

Since$$g(x)=\begin{cases}x+1&\text{ if }x\geqslant0\\x^2&\text{ if }x<0,\end{cases}$$you have$$g(x)\leqslant1\text{ if }x\in[-1,0]\quad\text{and}\quad g(x)>1\text{ if }x<-1\text{ or }x>0.$$So,\begin{align}f\bigl(g(x)\bigr)&=\begin{cases}\sqrt{1-g(x)}&\text{ if }x\in[-1,0]\\g(x)&\text{ if }x<-1\text{ or }x>0\end{cases}\\&=\begin{cases}\sqrt{1-x^2}&\text{ if }x\in[-1,0)\\0&\text{ if }x=0\\x^2&\text{ if }x<-1\\x+1&\text{ if }x>0.\end{cases}\end{align}
