This is not really a job for Matlab.
Let $$s = \sin(\phi_A) \sin(\phi_B) + \cos(\phi_A)\cos(\phi_B) = \cos(\phi_A - \phi_B)$$ This can be anywhere from $-1$ to $+1$ (assuming you want your variables to be real). Then $$f = -\cos(\theta_A)\cos(\theta_B) - s \sin(\theta_A)\sin(\theta_B) = \dfrac{1-s}{2} \cos(\theta_A +\theta_B) -
\dfrac{1+s}{2} \cos(\theta_A - \theta_B) = \lambda \cos(\theta_A + \theta_B) - (1-\lambda) \cos(\theta_A - \theta_B)$$
Since $-1 \le s \le 1$, $0 \le \lambda = (1-s)/2 \le 1$. Thus $f$ can be
anywhere from $\cos(\theta_A + \theta_B)$ to $-\cos(\theta_A-\theta_B)$, and so again from $-1$ to $1$.
To get the maximum value of $1$, you have three alternatives:
- $\cos(\theta_A + \theta_B) = 1$ and $\cos(\theta_A - \theta_B) = -1$
(e.g. with $\theta_A = \pi/2$ and $\theta_B = -\pi/2$); $\phi_A$ and
$\phi_B$ can be anything.
- $\cos(\theta_A + \theta_B) = 1$ and $\lambda = 1$, so $s = \cos(\phi_A - \phi_B) = -1$.
Thus you could have $\theta_B = -\theta_A$ and $\phi_B = \pi + \phi_A$
for any $\theta_A$ and $\phi_A$.
- $\cos(\theta_A - \theta_B) = -1$ and $\lambda = 0$, so $s = 1$.
Thus you could have $\theta_B = \pi + \theta_A$ and $\phi_B = \phi_A$ for any $\theta_A$ and $\phi_A$.