Find the value of $a$ such that $F(a)=\int^{\frac \pi 2}_{0}{|\sin x-a\cos x|} \space dx$ is minimised 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?

 A: The logic of that first part is troubling, we shouldn't be differentiating $F$ before we've properly defined it, or with respect to a variable that isn't one of its arguments. Two ways to fix this:
We could define $G(a,\theta)=\int_0^{\theta}a\cos x-\sin x\,dx+\int_{\theta}^{\pi/2}\sin x-a\cos x\,dx$. Then $F(a)$ is the maximum of $G(a,\theta)$ with respect to $\theta\in [0,\frac{\pi}{2}]$, which we will find by differentiating $G$ with respect to $\theta$. Why the maximum? Because at each point in the interval, we're integrating either $+|\sin x-a\cos x|$ or $-|\sin x-a\cos x|$; the integral of the absolute value comes when we take the value of $\theta$ that gives us the $+$ sign everywhere.
Alternately, we solve for the splitting point $\theta$ directly. We have $\sin x-a\cos x$ when that is positive, and $a\cos x-\sin x$ when $\sin x-a\cos x$ is negative. The splitting point between them comes when $\sin x-a\cos x=0$, or $\tan x=a$.
This $\theta=\arctan a$ is always inside $[0,\frac{\pi}{2})$ for positive $a$, and the formula $F(a)=2\sqrt{a^2+1}-a-1$ we get is valid in this range. For $a<0$, that splitting point is outside the interval, and we instead get that $|\sin x-a\cos x|=\sin x + (-a\cos x)$ for all $x\in [0,\frac{\pi}{2}]$, a sum of two positive terms. Integrating this, $F(a)=1-a$ for $a<0$. So then, globally,
$$F(a)=\begin{cases}2\sqrt{a^2+1}-a-1&a\ge 0\\1-a&a<0\end{cases}$$
Differentiating that, we get $F'(a)=\begin{cases}\frac{2a}{\sqrt{a^2+1}}-1&a>0\\-1&a<0\end{cases}$. That derivative is zero when $a>0$ and $\frac{2a}{\sqrt{a^2+1}}=1$, the single point $a=\frac1{\sqrt{3}}$. We must also treat $0$ where the formula changes and the endpoints $\pm\infty$ as critical points. Testing these points, we get $F(\infty)=F(-\infty)=\infty$; those certainly aren't minima. $F(0)=1$, and if we look closer $F$ is actually differentiable at zero with derivative $-1$. That isn't an extremum or even technically a critical point - but we had to look at it anyway to know. Then
$$F\left(\frac1{\sqrt{3}}\right)=2\sqrt{\frac43}-\frac1{\sqrt{3}}-1=\frac{4}{\sqrt{3}}-\frac1{\sqrt{3}}-1=\sqrt{3}-1\approx 0.732$$
That value $F\left(\frac1{\sqrt{3}}\right)$ is the smallest value at any of the critical points, and is thus the minimum we sought. It looks like you made a mistake in the evaluation there that confused you. [Corrected now]
As for $-\frac1{\sqrt{3}}$? That's not a critical point, because the formula it came from doesn't apply in that region.
A: 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}$.
A: First, let's discuss the sign of $a$. Suppose $a$ is negative, then $\sin x - a\cos x$ is positive everywhere. We would $a$ to be positive so that the two areas can cancel out each other. 
Notice that $\theta$ is a function of $a$. $\theta$ is defined to be the position where $\sin \theta-a\cos \theta=0$. We know that $$a\cos \theta = \sin \theta$$
This property holds everywhere and not just at optimal value. We can substitute this directly into the $F$ expression to express everything in $a$ just like what you did or use implicit differentiation.
$$\theta = \tan^{-1}a$$
$$F(a) = 2a\sin \theta + 2\cos \theta-a-1$$
Using implicit differentiation,
$$F'(a)=2\sin \theta + \left(2a\cos \theta -2\sin \theta\right)\frac{d\theta}{da}-1=0$$
$$2\sin \theta  =1$$
$$\sin \theta = \frac12$$
$$a=\tan \theta=\frac1{\sqrt3}$$
Btw, it seems that two $F$ values that you evaluated seems to have been swapped.
