# Integrating $\int \frac{1}{1+e^x} dx$

I wish to integrate $$\int_{-a}^a \frac{dx}{1+e^x}.$$ By symmetry, the above is equal to $$\int_{-a}^a \frac{dx}{1+e^{-x}}$$ Now multiply by $e^x/e^x$ to get $$\int_{-a}^a \frac{e^x}{1+e^x} dx$$ which integrates to $$\log(1+e^x) |^a_{-a} = \log((1+e^a)/(1+e^{-a})),$$ which is not correct. According to Wolfram, we should get $$2a + \log((1+e^{-a})/(1+e^a)).$$ Where is the mistake?

EDIT: Mistake found: was using log on calculator, which is base 10.

-
My guess is that you missed something in getting from $1+e^x$ to $1+e^{-x}$ because you later have $\frac{1}{1+e^x} = \frac{e^x}{1+e^x}$... –  El'endia Starman Oct 2 '11 at 2:55
As a sidenote, this problem is a special case of math.stackexchange.com/questions/60045/… (but the core question here is different). –  anon Oct 2 '11 at 3:02
@El'endia: No, he has $\frac1{1+e^{-x}}\left(\frac{e^x}{e^x}\right)=\frac{e^x}{1+e^x}$, after using symmetry to replace the original integrand by $\frac1{1+e^{-x}}$. –  Brian M. Scott Oct 2 '11 at 3:05
A direct route to the Wolfram solution is via the substitution $u=1+e^x$, $du=e^x dx=(u-1)dx$, followed by integration of $\frac1{u(u-1)}$ by partial fractions. Your solution is nicer, though. –  Brian M. Scott Oct 2 '11 at 3:08
@Brian: I meant that he started with $\frac{dx}{1+e^x}$ and after using symmetry and multiplying by 1, got $\frac{e^x}{1+e^x} dx$. That just doesn't seem right... o.O? Of course, I have to mention that I have no idea what this symmetry mentioned is and that I was wrong in the first place. :P –  El'endia Starman Oct 2 '11 at 3:11

All of these answers are correct, but there is a more correct answer: $$\int_{-a}^a \frac{dx}{1+e^x} = a.$$ (You should check that $\log((1+e^a)/(1+e^{-a})) = \log(e^a) = a$....) And you've basically found the proof already: set $$I_1 = \int_{-a}^a \frac{dx}{1+e^x} \text{ and } I_2 = \int_{-a}^a \frac{e^x\, dx}{1+e^x}.$$ Then $I_1=I_2$ as you saw, but also $$I_1 + I_2 = \int_{-a}^a \bigg( \frac{1}{1+e^x} + \frac{e^x}{1+e^x} \bigg) \,dx = \int_{-a}^a 1\,dx = 2a.$$ Therefore $I_1=I_2=a$. "anon" commented to this effect on the original question.

-
Or, vamping on Neil G.'s answer: the difference between the integrand $f(x) = 1/(1+e^x)$ and the function $g(x) = 1/2$ is the odd function $1/(1+e^x)-1/2 = (1-e^x)/2(1+e^x)$, and therefore the integral of $f$ over any symmetric interval $[-a,a]$ is the same as the integral of $g$ over that interval. –  Greg Martin Oct 2 '11 at 19:37

Both answers are equal. Split your answer into $\log(1+e^a)-\log(1+e^{-a})$, and write this as \begin{align*}\log(e^a(1+e^{-a}))-\log(e^{-a}(1+e^a))&=\log e^a+\log(1+e^{-a})-\log e^{-a}-\log(1+e^a)\\ &=2a+\log((1+e^{-a})/(1+e^a))\end{align*}

-

Your solution is absolutely correct. $\log(\frac{1+e^a}{1+e^{-a}})=2a+\log(\frac{1+e^{-a}}{1+e^a})$

The authors might have arrived at the solution like this. Let $I_1=\int^{a}_{-a} \frac{1}{1+e^x}dx$ and $I_2=\int^{a}_{-a} \frac{e^x}{1+e^x}dx$

Adding $I_1$ and $I_2$ , $I_1+I_2=\int^{a}_{-a}dx=2a$ As you have calculated $I_2=\log\left(\frac{1+e^a}{1+e^{-a}}\right)$

Therefore $I_1=2a-I_2=2a-\log\left(\frac{1+e^a}{1+e^{-a}}\right)=2a+\log\left(\frac{1+e^{-a}}{1+e^{a}}\right)$

-

Another way to solve this is to notice that

$$f(x) = \frac{1}{1+e^{-x}}$$

is the logistic sigmoid, which is the conversion from log-odds to probability. So,

$$f(x) + f(-x) = 1$$

Then, $$\int_{-a}^a f(x)dx=\int_0^a (f(x) + f(-x))dx=a.$$

-

Another way to evaluate this integral is by substitution. In fact, let $u=e^x$. Then \begin{eqnarray*} \int\frac{1}{1+e^x}dx&=&\int\frac{1}{u(u+1)}du=\int\left(\frac{1}{u}-\frac{1}{u+1}\right)du\\ &=&\ln u-\ln(u+1)+C=x-\ln(e^x+1)+C \end{eqnarray*}

-