Solve: $\sin x - y\cos x = z$ for $x$. I am working on programming a series of algorithms into a project, however I have run into trouble trying to solve this equation for $x$:
$$
\sin x - y\cos x = z
$$
It should be noted that $y$ and $z$ will be known at runtime and that $0 < x < \pi/2$.
I remember doing something like this in calculus, but it has been a while and searching the web has failed to enlighten me.
 A: This is possible, but it is certainly not trivial. What we need to do to solve it is turn the left hand $\sin(x)-y\cos(x)$ into something of the form $c\sin(x+b)$ for some constants $c$ and $b$ which can be computed from $y$. This is because there is no way we can invert two "special functions" like $\sin$ and $\cos$ at the same time and thus need to find a different identity.
However, it turns out that this is fairly easy to do; using the identity
$$\sin(x+b)=\sin(x)\cos(b)+\cos(x)\sin(b)$$
which, gives, when multiplied by $c$
$$c\sin(x+b)=c\sin(x)\cos(b)+c\cos(x)\sin(b)$$
and we want the above to equal $\sin(x)-y\cos(x)$ - thus, if we get the coefficients of $\sin(x)$ and $\cos(x)$ right, we are really requiring a simultaneous solution to:
$$c\cos(b) = 1$$
$$c\sin(b) = -y$$
What can be seen by squaring and adding the above two equations is
$$c^2(\cos(b)^2+\sin(b)^2)=c^2=1^2+(-y)^2$$
which yields $c=\sqrt{1+y^2}$. Taking the ratio of the two equations gives
$$\tan(b)=-y$$
so $b=\tan^{-1}(-y)$.
This tells us that we can write
$$\sin(x)-y\cos(x)=\sqrt{1+y^2}\sin(x-\tan^{-1}(y))$$
and to solve for the above equalling $z$ we simply undo the operations in order to receive:
$$x=\sin^{-1}\left(\frac{z}{\sqrt{1+y^2}}\right)+\tan^{-1}(y).$$
A: If
$$
\sin x-y\cos x=z
$$
then
$$
x = \arcsin {z\over\sqrt{1+y^2}}+\arccos {1\over\sqrt{1+y^2}}
$$
To see why, rewrite the top equation as
$$
r(\sin x \cos q - \cos x\sin q) = z.
$$
where $y=r\sin q$ and $r\cos q = 1$. Then $r^2=1+y^2$ and $\cos q=1/r$.
This is equivalent to
$$
\sin(x-q)=z/r
$$
Also notice that $\sqrt{1+y^2}\leq z$ or there is no solution, in other words $y^2\leq z^2-1$.
Numerical check:
package main

import (
    "fmt"
    "math"
)

func main() {
    incx := 0.01
    incy := 0.1
    for x := 0.0; x < math.Pi/2.0; x+=incx {
        for y := 0.0; y < 10.0; y+=incy {
            z := math.Sin(x) - y*math.Cos(x)
            r := math.Sqrt(1 + math.Pow(y, 2))
            q := math.Acos(1/r)
            x2 := math.Asin(z/r) + q
            if x - x2 > 0.0001 {
                fmt.Println(x, x2)
            }
        }
    }
}

A: In general, when you add two sinusoids of the same frequency, you get back another sinusoid of that frequency.  Their phases and amplitudes don't have to be the same.
So first write:
$$\sin(x) - y\cos(x) = z$$
as a more standard amplitude/phase form:
$$\sin(x) - y\sin(\pi/2 - x) = \sin(x) + y\sin(x - \pi/2)$$
$$1~\sin(x + 0) + y~\sin(x - \pi/2) = z$$
And now we have the sum of two sinusoids with amplitude/phase being $1, 0$ and $y, -\pi/2$ (and both have frequency $\frac{1}{2\pi}$).
To add them using polar form , $\angle$, use the formula

$$f(x) = A_1\sin(fx + p_1) + A_2\sin(fx + p_2)$$
  can be simplified as
  $$f(x) = A_3\sin(fx + p_3)$$
  given that 
  $$A_1 ~\angle~p_1 + A_2 ~\angle~p_2 = A_3 ~\angle~p_3$$

So we have 
$$A_3 \sin(x + p_3) = z$$
for 
$$A_3~\angle~ p_3 = 1~\angle~0 + y~\angle~-\pi/2$$
This is a basic trig problem:

And you get $A_3 = \sqrt{1 + y^2}$ and $p_3 = \arctan(-y)$ so
$$\sqrt{1 + y^2}\sin(x + \arctan(-y)) = z$$
$$x = \arcsin\left(\frac{z}{\sqrt{1 + y^2}}\right) + \arctan(y)$$
