Function two variables, integral Could you explain to me how to solve this exercise?
$$f\colon \mathbb{R}^2 \ \rightarrow \mathbb{R}$$
$$f(x,y) =  \begin{cases} 1, \ \ \ \ \ \text{for}  \ x \ge 0, \ x \le y < x+1 \\ -1, \  \ \text{for} \ x \ge 0, \ x+1 \le y<x+2 \\ 0 \ \ \ \ \ \ \  \text{elsewhere}\end{cases}$$
How to compute $$\int_{\mathbb{R}} \left(\int_{\mathbb{R}} f(x,y)dx \right)dy?$$
 A: Start with drawing the sets 
$A= \{ (x,y) \colon x \geq 0 \ \& \ x \leq y < x+1\}$,
$B= \{ (x,y) \colon x \geq 0 \ \& \ x+1 \leq y < x+2\}$
to get the integration limits.
Now, observe that
$\int_{\mathbb{R}} (\int_{\mathbb{R}} f(x,y)dx)dy$
$= \underbrace{\int_{1}^{\infty} \left(\int_{y-1}^{y} dx\right) dy + \int_{0}^{1} \left( \int_{0}^{y} dx \right) dy}_{\mathrm{integration \ over \ a\  set } A} + \underbrace{\int_{2}^{\infty} \left(\int_{y-2}^{y-1} (-1)dx\right) dy + \int_{1}^{2} \left( \int_{0}^{y-1} (-1)dx \right) dy}_{\mathrm{dintegration \ over \ a\  set } B} $
Now, calculate.
A: Let $\phi_y(x) = f(x,y) $. Then $\phi_y(x) =1$ when $x \ge 0$ and $y-1 < x \le y$,
$\phi_y(x) =-1$ when $x \ge 0$ and $y-2 < x \le y-1$ and zero otherwise.

Hence if $y<0$, we have $\phi_y(x) =0$.
If $y \in [0,1)$, we have $\phi_y(x) =1$ when $x \in (0,y]$, and $0$ otherwise.
If $y \in [1,2)$, we have $\phi_y(x) =-1$ when $x \in (0,y-1]$, $\phi_y(x) =1$ when $x \in (y-1,y]$, and $0$ otherwise.
If $y \ge 2$, we have $\phi_y(x) =-1$ when $x \in (y-2,y-1]$, $\phi_y(x) =1$ when $x \in (y-1,y]$, and $0$ otherwise.
Now compute $\int_\mathbb{R} \phi_y(x) dx$ for each of these four cases. Then integrate the result.
A: I learned a really elegant way of doing this stuff from an old German analyst whose advisor's advisor was Hilbert. (He had interesting stories.) He used characteristic functions to avoid drawing any pictures. For example, your function is
$$
   f(x,y)=\chi_{[0,1)}(y-x)\chi_{[0,\infty]}(x)-\chi_{[1,2)}(y-x)\chi_{[0,\infty]}(x).
$$
You can see that $f=1$ if $0 \le y-x < 1$ and $x \ge 0$, etc.
Now the regions tend to take care of themselves. This is particularly useful when you try to swap orders of integration, and you want to avoid pictures and the errors that go with my bad pictures. In this case,
$$
\begin{align}
    & \int\left(\int f(x,y)dx\right)dy = \\
    & = \int\left(\int\left[\chi_{[0,1]}(y-x)-\chi_{[1,2]}(y-x)\right]\chi_{[0,\infty)}(x)\,dx\right)dy \\
    & = \int\left(\int_{y}^{y+1}\chi_{[0,\infty)}(x)\,dx-\int_{y}^{y+2}\chi_{[0,\infty)}(x)\,dx\right)dy
\end{align}
$$
Now I think you can see what is going on, if I haven't made an error.
