<s>[INST] What is my mistake in this integral: $\int \tan^5(x) dx$? I'm doing an exercise from Stewart's Calculus textbook in which I have to evaluate the following integral:
$\int \tan^5(x) dx$
I start by rewriting the integral this way:
$\int \tan^5(x) dx = \int \frac{\sin^5(x)}{\cos^5(x)} dx = \int \frac{\sin^4(x)}{\cos^5(x)} \sin(x) dx$
And here I make a substitution:
$u = \cos(x)$
$-du = \sin(x)dx$
So the integral with the substitution becomes:
$\int \frac{(1-u^2)^2}{u^5} (-1) du$
$\int \frac{(1-2u^2+u^4)}{u^5} (-1) du$
$\int \frac{(2u^2-u^4-1)}{u^5}  du$
$\int \frac{(2u^2)}{u^5} - \frac{(u^4)}{u^5} - \frac{1}{u^5} du  = 2 \frac{u^{-2}}{(-2)} - \ln\vert u \vert - \frac{u^{-4}}{(-4)} + constant$
$= \frac{-1}{u^2} - \ln\vert u \vert + \frac{1}{4u^4} + constant $
$= \frac{-1}{\cos^2(x)} - \ln\vert \cos(x) \vert + \frac{1}{4\cos^4(x)} + constant $
This antiderivative seems to be correct, because if I differentiate it I get:
$\frac{d}{dx} [\frac{-1}{\cos^2(x)} - \ln\vert \cos(x) \vert + \frac{1}{4\cos^4(x)} + constant] $
$-(-2)\cos^{-3}(x)(-\sin(x))-\frac{1}{\cos(x)}(-\sin(x))+\frac{1}{4}(-4)\cos^{-5}(x)(-\sin(x))$
$ \frac{-2\sin(x)}{\cos^3(x)} +\tan(x)+\frac{\sin(x)}{\cos^5(x)}$
$\tan(x)[1-\frac{2}{\cos^2(x)} + \frac{1}{\cos^4(x)}]$
$\tan(x)[\frac{\cos^4(x)-2\cos^2(x)+1}{\cos^4(x)}]$
$\tan(x)[\frac{\cos^4(x)-\cos^2(x)+1-\cos^2(x)}{\cos^4(x)}]$
$\tan(x)[\frac{\cos^4(x)-\cos^2(x)+\sin^2(x)}{\cos^4(x)}]$
$\tan(x)[\frac{\cos^2(x)(\cos^2(x)-1)+\sin^2(x)}{\cos^4(x)}]$
$\tan(x)[\frac{\cos^2(x)(-\sin^2(x))+\sin^2(x)}{\cos^4(x)}]$
$\tan(x)[\frac{\sin^2(x)(1-\cos^2(x))}{\cos^4(x)}]$
$\tan(x)[\frac{\sin^2(x)(\sin^2(x))}{\cos^4(x)}]$
$\tan(x)[\frac{\sin^4(x)}{\cos^4(x)}] = \tan^5(x)$
However, when I graph $\frac{d}{dx} [\frac{-1}{\cos^2(x)} - \ln\vert \cos(x) \vert + \frac{1}{4\cos^4(x)} ] $ and $\tan^5(x)$ at a first glance they look like they are the same, but when I start to zoom in I notice that these curves are actually not equal:

The magenta curve is $\tan^5(x)$ and the black curve is $\frac{d}{dx} [\frac{-1}{\cos^2(x)} - \ln\vert \cos(x) \vert + \frac{1}{4\cos^4(x)} ] $ 
The answer that Stewart provides at the end of the book is the following:
$ \int \tan^5(x) dx = \frac{1}{4} \sec^4(x)-\tan^2(x)+\ln\vert\sec(x)\vert + C$
Which is confusing me even more, because if I try to differentiate his answer I get:
$\frac{d}{dx}[\frac{1}{4} \sec^4(x)-\tan^2(x)+\ln\vert\sec(x)\vert + C]$
$\frac{d}{dx}[\frac{1}{4} \cos^{-4}(x)-\tan^2(x)+\ln\vert1/\cos(x)\vert + C]$
$\frac{d}{dx}[\frac{1}{4} \cos^{-4}(x)-\tan^2(x)+\ln(1) - \ln\vert\cos(x)\vert+ C]$
$\frac{d}{dx}[\frac{1}{4} \cos^{-4}(x)-\tan^2(x)- \ln\vert\cos(x)\vert+ C]$
$= \frac{1}{4}(-4)\cos^{-5}(x)-2\tan(x)\sec^2(x)-\frac{1}{\cos(x)}(-\sin(x))$
$= \frac{-1}{\cos^5(x)}-2\tan(x)\sec^2(x)+\tan(x)$
$= \frac{-1}{\cos^5(x)}+\tan(x)(1-2\sec^2(x))$
$= \frac{-1}{\cos^5(x)}+\tan(x)(1-\sec^2(x) -\sec^2(x))$
$= \frac{-1}{\cos^5(x)}+\tan(x)(-\tan^2(x) -\sec^2(x))$
$= \frac{-1}{\cos^5(x)}-\tan^3(x)-\tan(x)\sec^2(x)$
$= \frac{-1}{\cos^5(x)}-\frac{\sin^3(x)}{\cos^3(x)}-\frac{\sin(x)}{\cos^3(x)}$
$=\frac{-1-\cos^2(x)\sin^3(x)-\cos^2(x)\sin(x)}{\cos^5(x)}$
$=\frac{-1-(1-\sin^2(x))\sin^3(x)-(1-\sin^2(x))\sin(x)}{\cos^5(x)}$
$=\frac{-1-(\sin^3(x)-\sin^5(x))-(\sin(x)-\sin^3(x))}{\cos^5(x)}$
$=\frac{-1-\sin^3(x)+\sin^5(x)-\sin(x)+\sin^3(x)}{\cos^5(x)}$
$=\frac{-1-\sin(x)+\sin^5(x)}{\cos^5(x)}$
$=\tan^5(x)-\frac{(1+\sin(x))}{\cos^5(x)}$
If I plot the derivative of Stewart's answer along the derivative of my answer and the original function $\tan^5(x)$ this is how the the graph looks like:



*

*Magenta curve: $\tan^5(x)$

*Black curve: $\frac{d}{dx}[\text{my answer}]$

*Blue curve: $\frac{d}{dx}[\text{Stewart's answer}]$


What's going on? Is my answer correct? If it isn't, what am I doing wrong? And what about Stewart's answer? Am I making a mistake when I differentiate it or is it correct? 
Thanks for your help!
 [/INST] To double check:
Notice that $$\tan'x=\tan^2x+1.$$
Then
$$\int\tan^5x\,dx=\int((\tan^2x+1)\tan^3x-(\tan^2x+1)\tan x+\tan x)dx\\
=\frac{\tan^4x}4-\frac{\tan^2x}2-\log|\cos x|.$$

In terms of the secant (and dropping the constant),
$$\frac{(\sec^2x-1)^2}4-\frac{\sec^2x-1}2+\log|\sec x|=\frac{\sec^4x}4-\sec^2x+\log|\sec x|.$$
You can also replace the second term $\sec^2x$ by $\tan^2x$.
</s>