I previously posted a code-related questions on StackOverflow here but after doing some more investigation work into the code, I had an epiphany when looking at the actual Newton-Raphson section because the formula used to represent $f(x)$ is a trigonometric equation of the following form:
$$f(x) = a\cdot\cos x + b\cdot\sin x - c$$
Now I've been trying to solve this for "$x$" using guidance from mathforum.org here and Quora here and I've got as far as the format at the end of Harshit Vyas' answer, and it looks like this:
$$C1\cdot\cos x + C2\cdot\sin x - C3 = 0$$
This equation/formula calculates the values of $C1$, $C2$ and $C3$ before it starts the iteration loop, so they become constants as far as the equation is concerned:
$$\sin x = \frac{C2 \cdot C3 \pm C1 \cdot \sqrt{C1^2 + C2^2 - C3^2}}{C1^2 + C2^2}$$
I can then get the value for $x$ with $\sin^{-1}$. My questions are as follows:
- Have I missed anything when I was solving for $x$?
- $C1$, $C2$ and $C3$ are calculated using sines and cosines of three provided angles, and they can be negative due to the use of $-\cos$ and since a negative number has a complex square root, is there a way to compensate for this?
04/06/2019 Edit to add:
I've gone back over the equation and rewritten according to the steps in the Quora article I linked to and this is what I get as my result (with C1=a, C2=b and C3=c, which are the actual coefficients was referring to earlier); apologies for the long list but I wanted to show my working step by step:
$$C1 \cdot cos(\beta) + C2 \cdot sin(\beta) - C3$$
$$C1 \cdot cos(\beta) + C2 \cdot sin(\beta) = C3$$
$$C1 \cdot cos(\beta) = C3 - C2 \cdot sin(\beta)$$
$$C1^2 \cdot cos^2(\beta) = C3^2 - 2 \cdot C2 \cdot C3 \cdot sin(\beta) + C2^2 \cdot sin^2(\beta)$$
$$C1^2 \cdot (1 - sin^2(\beta)) = 2 \cdot C2 \cdot C3 \cdot sin(\beta) + C2^2 + C2^2 \cdot sin^2(\beta)$$
$$(C1^2 + C2^2)sin^2(\beta) - 2 \cdot C2 \cdot C3 \cdot sin(\beta) + (C3^2 - C1^2) = 0$$
Let t = $sin(\beta)$ for simplicity:
$$(C1^2 + C2^2)t^2 + (-2 \cdot C2 \cdot C3)t + (C3^2 - C1^2) = 0$$
Let D = the discriminant of the formula:
$$D = (-2 \cdot C2 \cdot C3)^2 - 4(C1^2 + C2^2)(C3^2 - C1^2)$$
$$D = 4C2^2C3^2 - 4(C1^2C3^2 - C1^4 + C2^2C3^2 - C1^2C2^2)$$
$$D = 4C1^2 + 4C1^2C2^2 - 4C1^2C3^2$$
$$D = 4C1^2(C1^2 + C2^2 - C3^2)$$
$$t = \frac{-(-2 \cdot C2 \cdot C3 \pm \sqrt{D})}{2(C1^2 + C2^2)}$$
$$t = \frac{(2 \cdot C2 \cdot C3 \pm \ 2 \cdot C1 \sqrt{C1^2 + C2^2 - C3^2})}{C1^2 + C2^2}$$
$$t = sin(\beta) = \frac{(C2 \cdot C3 \pm C1 \sqrt{C1^2 + C2^2 - C3^2})}{C1^2 + C2^2}$$
$$\beta = sin^1 \left (\frac{C2 \cdot C3 \pm C1 \cdot ^{\sqrt{C1^2 + C2^2 - C3^2}}}{C1^2 + C2^2} \right )$$ Does this look like right or have I missed anything else out?
Write your equation in the form $$\sqrt{a^2+b^2}\left(\frac{a}{\sqrt{a^2+b^2}}\cos(x)+\frac{b}{\sqrt{a^2+b^2}}\sin(x)-\frac{c}{\sqrt{a^2+b^2}}\right)$$ and define $$\sin(\phi)=\frac{a}{\sqrt{a^2+b^2}}$$ $$\cos(\phi)=\frac{b}{\sqrt{a^2+b^2}}$$