TITLE: Find the value of $a$ such that $F(a)=\int^{\frac \pi 2}_{0}{|\sin x-a\cos x|} \space dx$ is minimised
QUESTION [11 upvotes]: Recently, I am watching Doraemon episodes on Youtube to relax myself from workload, there's an episode about escaping from Suneo's house(Episode 330: 脱出!恐怖の骨川ハウス (Chinese Subtitle)). To escape the room, one must answer the question correctly in each door, and the difficulty level of each question is depending. Nōbita first chosen the door, where is the nearest to the exit, hence the following question appeared in the screen:


Question:(Translated) Find the value of $a$ such that the function
  $$F(a)=\int^{\frac \pi 2}_{0}{|\sin x-a\cos x|} \space dx$$
  is minimised.

Nōbita then answered $a=1$, which intuitively seemed wrong to me, of course, he got wrong and the room was getting smaller as punishment.
After watching this episode, I have computed the value of $a$ as $\pm 1/\sqrt{3}$, and leave some doubts for my attempt.
My attempt: Since $\sin x$ is a increasing function and $a\cos x$ is a decreasing function over the closed interval $[0, \frac \pi 2]$, to minimise $F(a)$, both function must intersect each other at a point in the open interval $(0, \frac \pi 2)$. Let $(\theta, y(\theta))$ be the intersection point of $y=\sin x$ and $y=a\cos x$.
$$F(a)=\int^{\theta}_{0}{(a\cos x-\sin x)dx}-\int^{\frac \pi 2}_{\theta}{(\sin x - a\cos x)dx}$$
$$=(a\sin x+\cos x)|^{\theta}_{0}-(\cos x+a\sin x)|^{\frac \pi 2}_{\theta}$$
$$=2a\sin \theta+2 \cos \theta -a-1$$
To minimise $F(a)$, its derivative function must be $0$, i.e. $F'(a)=0$
$$F'(a)=2a\cos \theta-2\sin \theta=0 \space \space \space \space \Longrightarrow \tan \theta=a$$
Therefore,
$$F(a)=2a\cdot \frac{a}{\sqrt{a^2+1}}+2\cdot \frac{1}{\sqrt{a^2+1}}-a-1=2\sqrt{a^2+1}-a-1$$
Again, take its derivative yields
$$F'(a)=\frac{2a}{\sqrt{a^2+1}}-1=0 \space \space \space \Longrightarrow a=\pm \frac{1}{\sqrt{3}}$$
My doubts:

First, I don't understand why intersection point occurs to minimise the function $F(a)$. When $a$ is sufficiently large enough, e.g. $a=1000$, both functions $y= \sin x$ and $y= 1000 \cos x$ does not have intersection point over the open interval $(0, \frac \pi 2)$.
Second, when I bring back $a=\pm \frac{1}{\sqrt{3}}$ into the function $F(a)$, I got the answer $$F(-\frac{1}{\sqrt{3}})=1+\frac {1}{ \sqrt{3}} \approx 1.5774$$ and $$F(\frac{1}{\sqrt{3}})=\sqrt{3}-1 \approx 0.73205$$ Which of the following imply the correct answer?

REPLY [4 votes]: Suppose $a<0$. Then, over $[0,\pi/2]$, $\sin x-a\cos x\ge\sin x\ge0$, so $F(a)\ge F(0)$. Therefore $F(a)$ is not minimal.
Hence, in order to have a minimum we need $a\ge0$. In particular there exists a unique $\theta\in[0,\pi/2)$ such that $a=\tan\theta$, namely $\theta=\arctan a$. Thus we have
$$
\sin x-a\cos x
\begin{cases}
\le 0 & 0\le x\le \arctan a \\[4px]
\ge 0 & \arctan a\le x\le \pi/2
\end{cases}
$$
and we can write
$$
F(a)=\int_0^{\arctan a}(a\cos x-\sin x)\,dx+\int_{\arctan a}^{\pi/2}(\sin x-a\cos x)\,dx
$$
Thus
$$
F(a)=2a\sin\arctan a+2\cos\arctan a-a-1
$$
which is the same as you found out, but better shows that $\theta$ depends on $a$.
Computing the derivative is easy and it turns out that
$$
F'(a)=\frac{2a^2}{1+a^2}\sin\arctan a+\frac{2a}{1+a^2}\cos\arctan a-1
$$
It takes just a bit of trigonometry to get
$$
\sin\arctan a=\frac{a}{\sqrt{1+a^2}},\qquad \cos\arctan a=\frac{1}{\sqrt{1+a^2}}
$$
and so
$$
F'(a)=\frac{2a}{\sqrt{1+a^2}}-1
$$
which vanishes for $a=1/\sqrt{3}$.
By the way, this also shows that, for $a\ge0$,
$$
F(a)=2\sqrt{1+a^2}-a+c
$$
and, since $F(0)=1$, $c=1$.
For $a<0$,
$$
F(a)=\int_{0}^{\pi/2}(\sin x-a\cos x)\,dx=1-a
$$
As an alternative, consider $\theta=\arctan a$, so $a=\tan\theta$ and
$$
\cos x-a\sin x=\frac{1}{\cos\theta}(\cos\theta\cos x-\sin\theta\sin x)=
\frac{\cos(x+\theta)}{\cos\theta}
$$
With the substitution $t=x+\theta$, your integral becomes
$$
F(\tan\theta)=
\frac{1}{\cos\theta}\int_{\theta}^{\theta+\pi/2}\lvert\cos t\rvert\,dt
$$
The integral is simple:
$$
\int_\theta^{\pi/2}\cos t\,dt-\int_{\pi/2}^{\theta+\pi/2}\cos t\,dt
=2-\sin\theta-\cos\theta
$$
Thus
$$
G(\theta)=F(\tan\theta)=\frac{2}{\cos\theta}-\tan\theta-1
$$
and $G'(\theta)=\dfrac{2\sin^2\theta-1}{\cos^2\theta}$ vanishes for $\sin\theta=1/2$, that is, $\theta=\pi/6$. This corresponds to $a=1/\sqrt{3}$.