<s>[INST] Find $f \circ g$, $g \circ f$, $f \circ f$, $g \circ g$ where $f(x)=-1+|x-2|$ and $g(x)=2-|x|$. If $f(x)=-1+|x-2|;0\leq x\leq4$ and 
$g(x)=2-|x|;-1\leq x\leq3$
Then find $f \circ g(x),g \circ f(x),f \circ f(x),g \circ g(x)$


In the above figure the red graph shows $g(x)$ and the blue graph shows $f(x)$.
The domain of $f(x)$ is $0\leq x\leq4$ and the domain of $g(x)$ is $-1\leq x\leq3$.
I found the domain of $f \circ g(x)$,it is the intersection of $-1\leq x\leq3$ and $0\leq g(x)\leq 4$ i.e. the intersection of $-1\leq x\leq3$ and $-2\leq x\leq 2$.So its domain is $-1\leq x\leq 2$.
then  found the domain of $g \circ f(x)$,it is the intersection of $0\leq x\leq4$ and $-1\leq f(x)\leq 3$ i.e. the intersection of $0\leq x\leq4$ and $-2\leq x\leq 6$.So its domain is $0\leq x\leq 4$.
then i found the domain of $f \circ f(x)$,it is the intersection of $0\leq x\leq4$ and $0\leq f(x)\leq 4$ i.e. the intersection of $0\leq x\leq4$ and $-3\leq x\leq 1\cup 3\leq x\leq 7$.So its domain is $0\leq x\leq 1\cup 3\leq x\leq 4$.
then i found the domain of $g \circ g(x)$,it is the intersection of $-1\leq x\leq3$ and $-1\leq g(x)\leq 3$ i.e. the intersection of $-1\leq x\leq3$ and $-3\leq x\leq 3$.So its domain is $-1\leq x\leq 3$.
Now i found $f \circ g(x)=\left\{ \begin{array}{lcc}
             -1-x  & -1 \leq x \leq 0 \\
             \\ x-1, &0<x\leq2 \\
             \\ 
             \end{array}
   \right.$ which i got correct.
I found $g \circ f(x)=\left\{ \begin{array}{lcc}
             x+1  & 0 \leq x \leq 2 \\
             \\ -x+5, &2\leq x\leq 4 \\
             \\ 
             \end{array}
   \right.$.But my book says this is wrong and correct $g \circ f(x)$ is
$g \circ f(x)=\left\{ \begin{array}{lcc}
             x+1  & 0 \leq x <1 \\
             \\3-x&1\leq x\leq 2 \\
             \\x-1&2<x\leq 3  \\
             \\ -x+5, &3 <x\leq 4 \\
             \\ 
             \end{array}
   \right.$I dont know where have i gone wrong.
I found $f \circ f(x)=\left\{ \begin{array}{lcc}
             x  & 0 \leq x \leq 1 \\
             \\ x-6, &3\leq x\leq 4 \\
             \\ 
             \end{array}
   \right.$.But my books says this is wrong.And the correct $f \circ f(x)$ is $f \circ f(x)=\left\{ \begin{array}{lcc}
             x  & 0 \leq x \leq 1 \\
             \\ -x+4, &3\leq x\leq 4 \\
             \\ 
             \end{array}
   \right.$.I dont know where have i gone wrong.
I found $g \circ g(x)=\left\{ \begin{array}{lcc}
             x+4  & -1 \leq x \leq 0 \\
             \\ x, &0\leq x\leq 3 \\
             \\ 
             \end{array}
   \right.$ But my books says this is wrong and the correct gog(x) is
$g \circ g(x)=\left\{ \begin{array}{lcc}
             -x  & -1 \leq x \leq 0 \\
             \\ x, &0< x\leq 2 \\
             \\ 4-x,&2<x\leq 3\\
             \end{array}
   \right.$I dont know where have i gone wrong.
Please help me.Thanks.
 [/INST] I will look at $g\circ f$ for you only, maybe this would help you correcting the other parts.
In general when doing such exercises you should particular attention to when the functions or their derivatiaves change sign. For $f(x)$ these points on the domain are $x=1,2,3$. Here hence you should look at four different ranges: $0<x<1$, $1<x<2$, $2<x<3$ and $3<x<4$. When feeding $f(x)$, i.e. the range you should then care only about the rule of $g(x)$.
Let start with the $0<x<1$. Here $0<f(x)=1-x<1$ therefore $g(f(x))=2-|1-x|$ or that $g(f(x))=2-(1-x)=1+x$.
Now lets look at $1<x<2$, here we have $-1<f(x)=1-x<0$, therefore $g(f(x))=2-|1-x|$ or that $g(f(x))=2-(x-1)=3-x$.
also for $2<x<3$ we have $-1<f(x)=-3+x<0$ hence $g(f(x))=2-|-3+x|$ or that $g(f(x))=2-(3-x)=-1+x$.
and finally for $3<x<4$ we have $0<f(x)=-3+x<1$ hence $g(f(x))=2-|-3+x|$ or that $g(f(x))=2-(-3+x)=5-x$.
Doing similar steps for the rest of the functions you should be able to obtain the correct results.
</s>