<s>[INST] Why this Taylor expansion of $ \dfrac{1}{1+\cos x}$ is wrong? I want to expand the function $$ \dfrac{1}{1+\cos x} $$ at $x = 0$
The way I did this is first regard $\cos x $ as a whole and denote it by $y$
The expansion will become $$ \dfrac{1}{1+y}=1-y+y^2+o(y^2)$$
and then return $y=\cos x$ back and I want to expand each $\cos x$ at $ x=0$
Since $$ \cos x = 1-\dfrac{x^2}{2} + o(x^2) $$
$ o(y^2) = o(\big(1-\dfrac{x^2}{2} + o(x^2)\big)^2)=o(x^2)$
and the result is $$ \dfrac{1}{1+\cos x}=1-1+\dfrac{x^2}{2}+1-x^2+o(x^2)=1-\dfrac{x^2}{2}+o(x^2)$$
but this is wrong
Thus, my question is why this is wrong.
Also by the same way, I can correctly get the expansion of $ \tan(\tan x)$ and $\sin(\sin x)$
For example, $ \tan(\tan x)$. I want to expand this at $ x=0$
I regard the inner $\tan x$ as $y$, so the expansion will be $$ \tan(y)=y+\dfrac{1}{3}y^3+o(y^3)$$
Then by some calculations, I can get $ o(y^3)= o(x^3),\ \ y=\tan x=x+\dfrac{1}{3}x^3+o(x^3), \ \ y^3=\tan^3 x=(x+o(x))^3 =x^3+o(x^3)$
Then the result is $$ x+\dfrac{2}{3}x^3+o(x^3)$$ which is right
 [/INST] This is a good method, and it will work if applied correctly! As Jean-Claude is pointing out in the comments, what you need to do is expand $\frac{1}{1 + y}$ centered at $y = 1$, because $\cos 0 = 1$.
In other words, you need to use the Taylor series in terms $a_0 + a_1 (y - 1) + a_2 (y-1)^2 + \cdots$. Currently you are using the Taylor series at $y = 0$, which is not valid at $y = 1$. It converges with radius $1$, so it's valid only in the interval $(-1, 1)$.
This may be easier to think about if you let $z = 1 - y = 1 - \cos x$. This way, all of the Taylor series stay centered at zero because when $x = 0$, $z = 0$ also. Then we need to expand as follows:
\begin{align*}
\frac{1}{1 + \cos x}
&= \frac{1}{2 - (1 - \cos x)} \\
&= \frac{1}{2 - z} \\
&= \frac{\tfrac12}{{1 - \tfrac12z}} \\
&= \frac12 + \frac14 z + \frac18 z^2 + \frac{1}{16} z^3 + \cdots
\end{align*}
Now, as you note, $\cos x = 1 - \frac{x^2}{2} + o(x^2)$, so $z = 1 - \cos x = \frac12 x^2 + o(x^2)$, and the above becomes
$$
\frac12 + \frac14 \left( \frac12 x^2 + o(x^2) \right) + o(x^4) = \boxed{\frac12 + \frac18 x^2 + o(x^2).}
$$
</s>