Determine the constant so that the integral is minizimed Presume that $a<b$ and that $f$ is continuous in the interval $[a,b]$,
Determine K so that the integral:
$$\int_a^b (f(x) - K)^2 dx$$
is minimized, also what is this minimum?
I found similar questions but none were working with $f(x).$
 A: Let the function $g(x,y)=(f(x)-y)^2$. Set 
$
G(y)=\int_{a}^{b}g(x,y)\;{\rm d}x.
$
The min point of $G$ is a critical point of $G$. That is, a point $y^\ast$ such that 
$
G^\prime(y)=0
$.
By Libinitz integral rule, 
$$
G^\prime (y)\Big|_{y=y^\ast}
=
\frac{{\rm d}}{{\rm d } y}\int_{a}^{b}g(x,y)\;{\rm d } x\Big|_{y=y^\ast}
=
\int_{a}^{b}\frac{{\rm \partial}}{{\rm \partial } y}g(x,y)\;{\rm d } x\Big|_{y=y^\ast}
=
\int_{a}^{b}-(f(x)-y)\;{\rm d } x\Big|_{y=y^\ast}
\\
=
(b-a)y\Big|_{y=y^\ast}-\int_{a}^{b}f(x)\;{\rm d} x=0
$$
Then the minimum point of $G(y)$ is 
$$
y^\ast=\frac{1}{b-a}\int_{a}^{b}f(x){\rm d} x
$$
And 
$$
G\left(y^\ast\right)=G\left(\frac{1}{b-a)}\int_{a}^{b}f(x){\rm d}x \right)
\\
=\int_{a}^{b}
\left[
f(x)- \frac{1}{(b-a)}\int_{a}^{b}f(x){\rm d}x 
\right]^2
{\rm d}x
\\
= \int_a^b f(x)^2{\rm d} x-\frac{1}{(b-a)}\left[ \int_a^b f(x){\rm d} x\right]^2
$$
A: It may be overkill for this question (the other answers relied very efficiently on calculus), but I think the $L^2$ approach must be at least spoken about here.
Let us consider the Hilbert space $L^2(a,b)$ of (classes of) functions whose square is integrable on $[a,b]$, with its usual inner product $(f|g) = \int_a^b f(x)g(x) \mathrm{d}x$ and the norm $||f|| = \sqrt{(f|f)}$.
Then  $\int_a^b (f(x) - K)^2 \mathrm{d}x = (f-K|f-K)$ is the distance of the function $f$ to the constant function $K$. By the projection on convex theorem, more precisely on a closed subspace here (the constant functions are a closed subspace non empty set of $L^2(a,b)$), the minimum is attained at the only constant function $C$ such that $(f - C|K) = 0$ for all constants $K$. 
You can then easily check (by evaluating at $K = 1$ for example) that $C = \frac{1}{b-a}\int_a^b f(x) \mathrm{d}x.$
As I said, it may be overkill here, but the result presented here will easily generalize to similar problems where calculus will not be able to provide an answer (and is often deemed as the classical way to handle this kind of problem).
PS : Plus, I feel it gives a nice geometric interpretation of the result : the closest constant to $f$ in terms of $L^2$ norm is its own integral ; one could interpret it as a sort of "mean" of the function $f$ on the interval.
A: Hint. One may write
$$
\int_a^b (f(x) - K)^2 dx=K^2\int_a^bdx-2K\int_a^b f(x)\: dx+\int_a^b [f(x)]^2 dx
$$ and one may see the above expression as a quadratic function depending on $K$.
