# Simultaneous equation of trig functions

If I have the two following equations:

\begin{align} A &= F\sin(x)+ G\cos(x)\\ B &= G\sin(x)+ F\cos(x) \end{align}

and $$A$$,$$B$$,$$F$$,$$G$$ and are all constants, what is the easiest method of calculating $$x$$?

I've had a look at Cramers rule and the $$R\cos(x-\alpha)$$ rule. I need to find the most effective method for a program I am writing!

• Use sum of angle trig identities, or related. – Pixel May 31 at 21:51
• isn't that a simple linear system in $\sin x, \; \cos x$ ? – G Cab May 31 at 22:30

Use $$A^2+B^2=F^2+G^2+2FG\sin 2x,\,A^2-B^2=(G^2-F^2)\cos 2x$$ to get $$\sin 2x,\,\cos 2x$$.

$$\left\{ \matrix{ A = F\sin x + G\cos x \hfill \cr B = F\cos x + G\sin x \hfill \cr} \right. \Rightarrow \left\{ \matrix{ F\sin x + G\cos x = A \hfill \cr G\sin x + F\cos x = B \hfill \cr} \right.$$ is a linear system in $$\sin x$$ and $$\cos x$$.

To solve it programmatically you will first solve the linear system in $$\sin x, \, \cos x$$, and after determine if the the couple of values obtained are "trigonometrically" congruent so that they may correspond to a single value for $$x$$. That can happen only if $$\sin ^2 x+ \cos ^2 x = 1$$.
Therefore

a) compute the determinant of the coefficients $$D_c = F^2-G^2$$
b) if $$D_c=0$$ then
- that means $$F = \pm G$$, choose the sign according to actual values
- compute the det of one minor of the complete matrix, e.g. $$GB-FA = F(\pm B-A)$$
- if that is also null, then the two equations are dependent, and you can solve just one of them, e.g. $$\sin x = A/F \mp \cos x$$ , else there's no solution
- thereafter, to have solutions in $$x$$ it must be \eqalign{ & 1 = \cos ^2 x + \sin ^2 x = \cos ^2 x + \left( {A/F \mp \cos x} \right)^2 \; \Rightarrow \cr & \Rightarrow \;2\cos ^2 x \mp 2A/F\cos x + \left( {A/F} \right)^2 - 1 = 0\; \Rightarrow \cr & \Rightarrow \;\cos x = c_1 ,c_2 \cr} - if $$c_1 ,\, c_2 \in \mathbb R$$ and one or both are in the range $$[-1,1]$$ you can solve via $$\arccos$$ to get from one to four solutions (apart periodicity), else no (real) solution .
c) elif $$D_c \ne 0$$ then you have a unique solution, e.g. via Cramer rule, $$\left\{ \matrix{ \sin x = S \hfill \cr \cos x = C \hfill \cr} \right.$$ - again, to have a solution in $$x$$, it must be $$- 1 \le S,C \le 1\; \cap \;S^2 + C^2 = 1$$ and solve one of them via $$\arccos$$ or $$\arcsin$$, taking into account the respective signs, i.e. via $$\arctan$$ on the four quadrants.

How to implement your code depends on what you know a priori about the input data $$A,B,F,G$$.

If you are sure that they lead to a unique solution in $$x$$, because, for example, they come from a physical system in which $$x$$ is well defined, then you can use just one equation or a better suited combination of the two.

My answer concerns the situation in which the input data are general.

In this case, you shall avoid answers such as "division by 0", or that the program enters in loop trying to find solutions that cannot be find, or provide you with a solution which is not unique, or other inconvenients.
If you want to avoid that, I am sorry but cannot devise any simpler way that the one indicated, which essentially comes to distinguish two cases $$F^2 - G^2=0$$ and $$F^2 - G^2 \ne 0$$.
In the second case, the result from Cramer's formula is not much complicated $$\left\{ \matrix{ \sin (x) = S = {{FA - GB} \over {F^{\,2} - G^{\,2} }} \hfill \cr \cos (x) = C = {{FB - GA} \over {F^{\,2} - G^{\,2} }} \hfill \cr} \right.$$ and you have to check the three conditions $$- 1 \le S \le 1$$ and $$- 1 \le C \le 1$$ and $$S^2 + C^2 = 1$$ for a real solution in $$x$$ to exist.

• Im not very mathematically inclined, I tried to follow as best I could! My first thought is that Johns formula requires less code than doing crramers rule? Or am i missing something? – Duncan O'Hara Jun 1 at 12:49
• I put an addendum to answer to your comment – G Cab Jun 1 at 14:03
• I concur that G Cab's is the better solution. – John Wayland Bales Jun 1 at 19:41
• Thank you guys! this was my first post here and I am very impressed with the friendly welcoming environment and top tier help! – Duncan O'Hara Jun 3 at 13:18

If $$F\ne G$$, then subtracting the second equation from the first gives

$$\begin{eqnarray} (F-G)\sin x-(F-G)\cos x&=&A-B\\\ \sin x-\cos x&=&\frac{A-B}{F-G}\\\ \sqrt{2}\left(\frac{1}{\sqrt{2}}\sin x-\frac{1}{\sqrt{2}}\cos x\right)&=&\frac{A-B}{F-G}\\\ \sqrt{2}\left(\sin x\cos\frac{\pi}{4}-\cos x\sin\frac{\pi}{4}\right)&=&\frac{A-B}{F-G}\\\ \sqrt{2}\sin\left(x-\frac{\pi}{4}\right)&=&\frac{A-B}{F-G}\\\ \sin\left(x-\frac{\pi}{4}\right)&=&\frac{\sqrt{2}}{2}\cdot\frac{A-B}{F-G}\tag{1} \end{eqnarray}$$ In the case $$F=G$$ it follows that $$A=B$$ and the equation reduces to the form

$$\sin x+\cos x=C$$

which can be reformed into

$$\cos\left(x-\frac{\pi}{4}\right)=\frac{\sqrt{2}}{2}C$$

ADDENDUM Due to the comment by @GCab

If $$F\ne-G$$ and the first equation is added to the second a similar process gives the result

$$\cos\left(x-\frac{\pi}{4}\right)=\frac{\sqrt{2}}{2}\cdot\frac{A+B}{F+G} \tag{2}$$ These equations can be solved for $$x$$ over whichever range is required.

In the case that $$G=-G$$ it follows that $$A=-B$$ and the equation will be of the form

$$\sin x-\cos x=C$$

which has solution

$$\sin\left(x-\frac{\pi}{4}\right)=\frac{\sqrt{2}}{2}C$$

• that's one equation, you missed the second ! – G Cab May 31 at 21:55
• @GCab I finally realized what you meant by the "second" equation. Thanks. – John Wayland Bales May 31 at 22:51
• I was meaning to say that the two equations form a system : you shall go on with the system, upon combining – G Cab May 31 at 23:09
• Hi thanks for your solution! Ive tried some test examples and you always get two roots, whats the best way to determine which root is the correct one(without guessing and checking)? – Duncan O'Hara Jun 1 at 12:40
• Note that $A$ and $B$ are not independent. There are bounds on the value of $A$, and for any given value of $A$ within those bounds there are only two values of $B$ which will yield simultaneous solutions to the two equations. For any other values of $A$ and $B$ equations 1 and 2 will yield non-solutions for $x$. The derivations assume that there are simultaneous solutions, which may not be the case depending on the values of $A, B, F, G$. – John Wayland Bales Jun 1 at 19:16

Each of the two equations determines the value of $$x$$ for a fitting value of $$A$$, so if the values are distinct the system is uncompatible. You have $$\frac{A}{\sqrt{F^2+G^2}}=\frac{F}{\sqrt{F^2+G^2}}\sin(x)+\frac{G}{\sqrt{F^2+G^2}}\cos(x)$$ $$\frac{A}{\sqrt{F^2+G^2}}=\sin(\alpha+x)$$ where $$\cos(\alpha)=\dfrac{F}{\sqrt{F^2+G^2}}$$ and $$A$$ a convenient value.

Similarly you have $$\frac{B}{\sqrt{F^2+G^2}}=\cos(\alpha-x)$$ and for suitable $$B$$ you do have compatibility or not of your system.

Given $$A$$, $$F$$, and $$G$$, you're limited to only one or two valid angles: using the $$R$$-$$\alpha$$ method lets you convert it to $$x = \alpha\pm\arccos(A/R)$$. You can get a similar answer from $$B$$, $$F$$, and $$G$$. warning: your computer will probably not have the precision necessary to get the exact same answer both times, even if the answers would, absent rounding errors, be compatible.