Computing integral $\int_0^1 |x-a| \, {\rm d} x$ I have the following question for a seminar and cannot find the relevant information to solve it.


Solve the integral for all $a \in \mathbb R$ $$I(a)=\int_0^1 |x-a| \, {\rm d} x$$


I understand that I have to treat the absolute value of the function $f(x)=|x-a|$ depending on the when $x$ is less or more then $a$, if $a$ had a defined value I could have solved this, but now im stuck because I dont know how to relate its unspecified value to find a solution for the integral.
Any help is appreciated that'll point me in the right direction.
 A: Substitute $u=x-a$. This will give us $\mathrm{d}x=\mathrm{d}u$ and
$$\int\limits_{-a}^{1-a}\lvert u\rvert\,\mathrm{d}u.$$
Now integrate by parts. Choose $f'=1$ and $g=\lvert u \rvert$:
$$\int\limits_{-a}^{1-a}\lvert u\rvert\,\mathrm{d}u = u\lvert u\rvert-\int\limits_{-a}^{1-a}\lvert u\rvert\,\mathrm{d}u.$$
These two integrals are equal. That means that
$$\int\limits_{-a}^{1-a}\lvert u\rvert\,\mathrm{d}u=\frac{u\lvert u\rvert}{2}\Bigg\lvert^{1-a}_{-a}.$$
Now just plug in in the values for $u$ and you'll get the solution for $a\in\mathbb{R}$:
$$\frac{(1-a)\lvert 1-a\rvert+a\lvert a\rvert }{2}$$
A: Compute the integral for $a < 0$, $a \in [0,1]$ and $a > 1$. The solution is
$$I (a) = \begin{cases} \frac12 - a & \text{ if } a < 0\\\\ \frac12 - a + a^2 & \text{ if } a \in [0,1]\\\\ a - \frac12 & \text{ if } a > 1\end{cases}$$

A: welcome to MSE.
$$\int |x-a|dx=\dfrac{\left(x-a\right)\left|x-a\right|}{2}+C$$ also you can rewrite as
$$I(a)=\int_0^1 |x-a|dx=\dfrac{a\left|a\right|}{2}-\dfrac{\left|a-1\right|a-\left|a-1\right|}{2}$$
A: The integrand is an absolute value function and changing $a$ values correspond to the horizontal translation of the function $|x|$. It is best explained visually:

I think it's quite practical to visualize the integral for a variety of $a$ values. For all real values of $a$ in $(-\infty,0)\cup (1,\infty)$, the shape of the area given by the integral is trapezoidal. On the other hand, for all real values of $a$ in the closed interval $[0,1]$, the area is made up of triangles. In the light of these, we can evaluate the integral using area formulas.

*

*For the values of $a \in (-\infty,0)$, the area bounded by the integrand function and the vertical line $x=1$ is trapezoidal. It is given by: $$\begin{align}\frac{|-a|+|1-a|}{2}
&=\frac{(-a)+(1-a)}{2} \\
&=\frac{1-2a}{2} \\
&=\frac{1}{2}-a \\ \end{align}$$

*For the values of $a \in (1,\infty)$, the area bounded by the integrand function and the vertical line $x=1$ is again trapezoidal. It is given by: $$\begin{align} \frac{|-a|+|1-a|}{2}
&=\frac{(a)+(-1+a)}{2} \\
&=\frac{2a-1}{2} \\
&=a-\frac{1}{2} \\ \end{align}$$

*For the values of $a \in [0,1]$, the area is given by the sum of areas of triangles: $$\begin{align} \frac{|-a|a}{2}+\frac{(1-a)|1-a|}{2}
&=\frac{a^2}{2}+\frac{1-2a+a^2}{2} \\
&=\frac{1}{2}-a+a^2 \\ \end{align}$$
Finally, the resultant integral is expressed as:

$$I(a)=\int_{0}^{1} |x-a|\space dx= \begin{cases} a-\frac{1}{2},\space \space \forall a\gt 1 \\ 
\frac{1}{2}-a+a^2,\space \space \forall a \in [0,1] \\
\frac{1}{2}-a, \space \space \forall a\lt 0 
\end{cases}$$
