<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] 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.  
</s>