TITLE: Determine the constant so that the integral is minizimed
QUESTION [5 upvotes]: 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).$

REPLY [2 votes]: 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.