How do I find the max value and the unknown variables of $$f=-\cos(\theta_A)\cos(\theta_B)-\sin(\theta_A)\sin(\theta_B)\sin(\phi_A)\sin(\phi_B)-\cos(\phi_A)\cos(\phi_B)\sin(\theta_A)\sin(\theta_B)$$ in matlab.
Where $\theta_A, \theta_B, \phi_A, \phi_B$ are unknown variables. Please tell me how to put the code.
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: