We can prove this by using properties of complex numbers:
$$ \begin{align}a\cos(x) + b\sin(x) &= a \cos(x) + b \cos\left(x-\frac{\pi}{2}\right)\\
&=a \operatorname{Re}\left\{e^{ix}\right\} + b \operatorname{Re}\left\{e^{i(x-\pi/2)}\right\}\\
&= \operatorname{Re}\left\{ae^{ix} + be^{i(x-\pi/2)}\right\}\\
&= \operatorname{Re}\left\{e^{ix}(a-bi) \right\} \\
&= \operatorname{Re}\left\{e^{ix}\sqrt{a^2+b^2}e^{i\cdot\operatorname{atan2}(-b,a)} \right\}\\
&= \operatorname{Re}\left\{\sqrt{a^2+b^2}e^{i(x+\operatorname{atan2}(-b,a))}\right\}\\
&= \sqrt{a^2+b^2} \cos(x+\operatorname{atan2}(-b,a))
\end{align}
$$
where $\operatorname{atan2}(y_0,x_0)$ is the angle of the complex number $x_0+iy_0$.
This formula works for all real numbers $a$ and $b$ except when $a=b=0$.
Generalization
Following the same procedure, we can find a general formula for the sum of two sinusoids with same frequency but (possibly) different phases:
$$ \begin{align}a\cos(x+\alpha) + b \cos(x+\beta) &= \operatorname{Re}\left\{ae^{i(x+\alpha)} + be^{i(x+\beta)}\right\}\\
&= \operatorname{Re}\left\{e^{ix}\left(ae^{i\alpha}+be^{i\beta}\right) \right\}\\
&= \operatorname{Re}\left\{e^{ix}ce^{i\gamma} \right\}\\
&= \operatorname{Re}\left\{ce^{i(x+\gamma)} \right\}\\
&= c \cos(x+\gamma)
\end{align}
$$
where $ce^{i\gamma} = ae^{i\alpha}+be^{i\beta}$.
We have
$$\begin{align}ce^{i\gamma}&=a (\cos(\alpha)+i\sin(\alpha)) +b (\cos(\beta)+i\sin(\beta))\\
&= (a\cos(\alpha)+b\cos(\beta)) + (a\sin(\alpha)+b\sin(\beta))i.\\
\end{align}
$$
So
$$c=\sqrt{(a\cos(\alpha)+b\cos(\beta))^2+(a\sin(\alpha)+b\sin(\beta))^2}$$
and
$$\gamma=\operatorname{atan2}\big(a\sin(\alpha)+b\sin(\beta),a\cos(\alpha)+b\cos(\beta)\big).$$
Sidenotes:
- This result is equivalent to
phasor addition.
- We can get similar formula involving $\sin$ by taking the imaginary
part.