Let $g(x)= \mathrm{\lim_{n \rightarrow \infty}}\,f^{\circ n} (x) $, where $f^{\circ n} (x)$ is the repeated application of $f$ to $x$, $n$ times. Why doesn't the following procedure work for determining $g(x)$:

Since $g(x)=f(g(x))$, we have

$g'(x) = f'(g(x)) g'(x)$ [chain rule]

The $g'(x)$ functions cancel, leaving:

1=f'(g(x)).

For example, if $f(x)=\sin (x)$, this equation gives $1=\cos(g (x)) $. So $g(x)=2\pi m$, which is correct when m=0.

But if if $f(x)=\cos (x)$, this equation gives $-1=\sin(g (x)) $. So $g(x)=-\pi/2+2\pi m$, which is not correct for any integer $m$, since $g(x) \approx 0.739$

Can someone please help?