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!
 A: First of all, congrats on going to all the extra effort to check your work, comparing your answer to the book's, and questioning the discrepancy among the graphs.  That's truly commendable.
As others have noted, your answer is correct and equivalent to the book's.  The remaining, unanswered, question concerns the fact that the graphs begin to separate when you zoom in on them.  I was able to duplicate this on my Mac in its built-in "Grapher" program (which the OP identified in comments below Jaideep Khare's answer as the graphing program used in the question).
If you go into Grapher's preferences and select "Advanced," you'll see a numerical accuracy slider for derivatives.  As you slide it to the right, you need to zoom in more and more to get the graphs to separate.  I have no idea how the program works, really, but I suspect it's computing derivatives with a numerical approximation, e.g., $(f(x+h)-f(x))/h$ with some very small $h$, rather than doing any symbolic algebra.  If you slide the derivative accuracy slider all the way to the right and continue to zoom in, you'll see some rather spectacular effects.
In short, what you've encountered is a limitation of Grapher's accuracy. There is interesting mathematics in the things that can go wrong when working numerically, so you'll do well to remember what you observed in this problem as you continue to study.  
A: When you are differentiating his answer, you took the derivative of $\frac{\sec^4x}{4}$ wrong.
What you need to do is -
$$\left(\frac{\sec^4 x}{4}\right)'=\left(\frac{\cos^{-4} x}{4}\right)'=\frac{-1}{\cos^5 x} \color{red}{\cdot (\cos x)'}=\frac{\sin x}{\cos^5 x}$$
Your answer is absolutely correct. Even Wolfram Alpha says that.
Your and the books answer are both the same, but seem to be different because they differ by a constant. $$-\sec^2 x=-1-\tan^2 x$$
A: If you're uncertain about a function being the correct antiderivative, you can always differentiate.
Alternatively, the difference between two antiderivatives should be constant. Taking the difference between yours and Stewart’s:
$$
\left(
  \frac{-1}{\cos^2(x)} - \ln\lvert \cos(x) \rvert + \frac{1}{4\cos^4(x)} \right)
-
\left(
\frac{1}{4} \sec^4(x)-\tan^2(x)+\ln\lvert\sec(x)\rvert
\right)
$$
we see that the terms $-\ln\lvert\cos(x)\rvert$ and $-\ln\lvert\sec(x)\rvert$ cancel out, because $\sec(x)=(\cos(x))^{-1}$; for the same reason, $\frac{1}{4\cos^4(x)}$ and $-\frac{1}{4}\sec^4(x)$ cancel out and we remain with
$$
-\frac{1}{\cos^2(x)}+\tan^2(x)=
-\frac{1}{\cos^2(x)}+\frac{\sin^2(x)}{\cos^2x}=
-\frac{1-\sin^2(x)}{\cos^2(x)}=-1
$$
Since the difference is constant, either both are antiderivatives of $\tan^5(x)$ or neither is.
Thus the two functions you get by setting the constant to $0$ differ by $-1$, which accounts for the graph you show.

You can check more quickly that your function is an antiderivative by recalling that $\frac{1}{\cos^2(x)}=1+\tan^2(x)$, so your function is
$$
-1-\tan^2(x)-\ln\lvert\cos(x)\rvert+\frac{1}{4}(1+2\tan^2(x)+\tan^4(x))
$$
and the derivative is therefore
\begin{align}
-2&\,\tan(x)(1+\tan^2(x))+\tan(x)+\frac{1}{4}(4\tan(x)+4\tan^3(x))(1+\tan^2(x))\\
&=-2\tan(x)-2\tan^3(x)+\tan(x)+\tan(x)(1+2\tan^2(x)+\tan^4(x))\\
&=\tan^5(x)
\end{align}
A: 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$.
