Suppose I have a continuous differentiable function $f:\mathbb{R}\rightarrow\mathbb{R}^+$ such that $\int_{\mathbb{R}}f(x)dx<\infty$.
Let's assume the function to be convex and increasing.
Now, for an $x_1$ and $x_2$, I would like to substitute $f(x)$ with a linear function $l(x)=\alpha+\beta x$ such that the area under the curve for the interval $(x_1,x_2]$ remains the same. Mathematically, $$\int_{x_1}^{x_2}f(x)dx=\int_{x_1}^{x_2}l(x)dx$$
Now, how can I find the values of $\alpha$ and $\beta$ that would minimise the total deviation of $l(x)$ form $f(x)$?
In other words, I would like to find $\alpha$ and $\beta$ that minimises $$\int_{x_1}^{x_2}\{f(x)-l(x)\}^2dx,$$ the area shaded purple in image 3.

Hint: Before expanding the integral, find the derivatives of the square error ($\epsilon$) w.r.t $\alpha$ and $\beta$. Then,expand the integral and set to zero. Finally, resolve the obtained system.
Solution: First, lets define $\epsilon$ as follow:
$$ \epsilon = \int_{x_1}^{x_2}\left(f(x)-l(x)\right)^2dx = \int_{x_1}^{x_2}\left(f(x)-(\alpha+\beta x)\right)^2dx $$ Derivate it w.r.t $\alpha$ and $\beta$: $$ \frac{\partial\epsilon}{\partial \alpha} = \int_{x_1}^{x_2}2 \left(f(x)-(\alpha+\beta x)\right)(-1)dx = 0 \Rightarrow \int_{x_1}^{x_2} f(x) dx = \alpha \left(x_2 - x_1 \right) + \beta \left(x_2^2 - x_1^2\right) $$ $$ \frac{\partial\epsilon}{\partial \beta} = \int_{x_1}^{x_2}2 \left(f(x)-(\alpha+\beta x)\right)(-x)dx = 0 \Rightarrow \int_{x_1}^{x_2} xf(x) dx = \frac{\alpha}{2} \left(x_2^2 - x_1^2 \right) + \frac{\beta}{3} \left(x_2^3 - x_1^3\right) $$
Finllay you can resolve this system and get the desired $\alpha$ and $\beta$.