# How can I calculate $a$, $f$, $x_0$ and $y_0$ in $y = a \cos(f(x - x_0)) + y_0$ given four arbitrary points?

How can I calculate $a$, $f$, $x_0$ and $y_0$ in $y = a \cos(f(x - x_0)) + y_0$, given four arbitrary points $(x_1, y_1)$, $(x_2, y_2)$, … that the graph must go though?

The complexity of substituting each $x_n$ for $x$ and $y_n$ for $y$, solving for a variable and substituting back in to the original equation becomes impossible. Every variable I substitute carries the rest of the variables in its solution.

I'm getting the idea that I'm going to have to use an iterative method, but even there I don't know how I would do it since each variable depends on the definition of all the rest, and this is a transcendental equation.

-

You've got four equations

$$y_i=a\cos(\omega(x_i-x_0))+y_0\;,$$

for $i$ from $1$ to $4$, in which $y_0$ and $a$ appear linearly and $\omega$ and $x_0$ appear transcendentally. (I'm writing $\omega$ where you wrote $f$, both because $f(x-x_0)$ is easily mistaken for a function application and because in physics $f$ usually denotes a frequency and angular frequencies are usually denoted by $\omega$.)

You can solve one of these, say for $i=4$, for $y_0$ and substitute into the others, which yields

$$y_0=y_4-a\cos(\omega(x_4-x_0))\;,$$ $$y_i=y_4+a\cos(\omega(x_i-x_0))-\cos(\omega(x_4-x_0))$$

for $i$ from $1$ to $3$. Then you can solve one of the remaining equations, say for $i=3$, for $a$ and substitute into the other two, which yields

$$a=\frac{y_3-y_4}{\cos(\omega(x_3-x_0))-\cos(\omega(x_4-x_0))}\;,$$

$$\frac{y_i-y_4}{\cos(\omega(x_i-x_0))-\cos(\omega(x_4-x_0))}=\frac{y_3-y_4}{\cos(\omega(x_3-x_0))-\cos(\omega(x_4-x_0))}\;,$$

$$(y_i-y_4)(\cos(\omega(x_3-x_0))-\cos(\omega(x_4-x_0)))=(y_3-y_4)(\cos(\omega(x_i-x_0))-\cos(\omega(x_4-x_0)))$$

for $i=1,2$. Now you can apply the angle difference formula for the cosine to separate out $\omega x_0$,

$$\begin{eqnarray} && (y_i-y_4)(\cos\omega x_3\cos\omega x_0+\sin\omega x_3\sin\omega x_0-\cos\omega x_4\cos\omega x_0-\sin\omega x_4\sin\omega x_0) \\ &=& (y_3-y_4)(\cos\omega x_i\cos\omega x_0+\sin\omega x_i\sin\omega x_0-\cos\omega x_4\cos\omega x_0-\sin\omega x_4\sin\omega x_0)\;, \end{eqnarray}$$

divide through by $\cos\omega x_0$ to express everything in terms of $\tan\omega x_0$,

$$\begin{eqnarray} && (y_i-y_4)(\cos\omega x_3+\sin\omega x_3\tan\omega x_0-\cos\omega x_4-\sin\omega x_4\tan\omega x_0) \\ &=& (y_3-y_4)(\cos\omega x_i+\sin\omega x_i\tan\omega x_0-\cos\omega x_4-\sin\omega x_4\tan\omega x_0)\;, \end{eqnarray}$$

solve for $\tan\omega x_0$,

$$\tan\omega x_0=\frac{ (y_i-y_4)(\cos\omega x_3-\cos\omega x_4) - (y_3-y_4)(\cos\omega x_i-\cos\omega x_4) }{ (y_i-y_4)(\sin\omega x_3-\sin\omega x_4) - (y_3-y_4)(\sin\omega x_i-\sin\omega x_4) } \;,$$

eliminate $\tan\omega x_0$,

$$\begin{eqnarray} && \frac{ (y_1-y_4)(\cos\omega x_3-\cos\omega x_4) - (y_3-y_4)(\cos\omega x_1-\cos\omega x_4) }{ (y_1-y_4)(\sin\omega x_3-\sin\omega x_4) - (y_3-y_4)(\sin\omega x_1-\sin\omega x_4) }\\ &=& \frac{ (y_2-y_4)(\cos\omega x_3-\cos\omega x_4) - (y_3-y_4)(\cos\omega x_2-\cos\omega x_4) }{ (y_2-y_4)(\sin\omega x_3-\sin\omega x_4) - (y_3-y_4)(\sin\omega x_2-\sin\omega x_4) }\;,\end{eqnarray}$$

multiply by the denominators and use

$$\begin{eqnarray} &(\cos\beta-\cos\alpha)(\sin\gamma-\sin\alpha)-(\cos\gamma-\cos\alpha)(\sin\beta-\sin\alpha) \\ &=\sin(\alpha-\gamma)+\sin(\beta-\alpha)+\sin(\gamma-\beta) \end{eqnarray}$$

to simplify,

$$\begin{eqnarray} && (y_3-y_4)(y_3-y_4)(\sin(\omega(x_2-x_4))+\sin(\omega(x_4-x_1))+\sin(\omega(x_1-x_2))) \\ &-& (y_1-y_4)(y_3-y_4)(\sin(\omega(x_2-x_4))+\sin(\omega(x_4-x_3))+\sin(\omega(x_3-x_2))) \\ &-& (y_2-y_4)(y_3-y_4)(\sin(\omega(x_3-x_4))+\sin(\omega(x_4-x_1))+\sin(\omega(x_1-x_3))) \\ &=&0\;, \end{eqnarray}$$

and divide through by $(y_3-y_4)$ and collect corresponding sines to recover the expected symmetry:

$$\begin{eqnarray} && (y_1-y_2)\sin(\omega(x_3-x_4)) + (y_3-y_1)\sin(\omega(x_2-x_4)) + (y_3-y_2)\sin(\omega(x_4-x_1)) \\ &+& (y_3-y_4)\sin(\omega(x_1-x_2)) + (y_1-y_4)\sin(\omega(x_2-x_3)) + (y_2-y_4)\sin(\omega(x_3-x_1)) \\ &=&0\;, \end{eqnarray}$$

or, to put it more succinctly,

$$\sum_{\sigma\in A_4}(y_{\sigma(1)}-y_{\sigma(2)})\sin(\omega(x_{\sigma(3)}-x_{\sigma(4)}))=0\;,$$

where the permutations $\sigma$ run over all even permutations in the alternating group $A_4$.

This is a single transcendental equation for $\omega$ that you can solve using your favourite root finding algorithm; then you can substitute the value back up the chain to obtain

$$x_0=\frac1\omega\arctan\frac{ (y_2-y_4)(\cos\omega x_3-\cos\omega x_4) - (y_3-y_4)(\cos\omega x_2-\cos\omega x_4) }{ (y_2-y_4)(\sin\omega x_3-\sin\omega x_4) - (y_3-y_4)(\sin\omega x_2-\sin\omega x_4) } \;,$$

$$a=\frac{y_3-y_4}{\cos(\omega(x_3-x_0))-\cos(\omega(x_4-x_0))}\;,$$

$$y_0=y_4-a\cos(\omega(x_4-x_0))\;.$$

P.S.: I just realized I introduced a spurious root $\omega=0$ in dividing through by $\cos(\omega(x_3-x_0))-\cos(\omega(x_4-x_0))$; you can find a non-trivial root by dividing through by $\omega$:

$$\frac1\omega\sum_{\sigma\in A_4}(y_{\sigma(1)}-y_{\sigma(2)})\sin(\omega(x_{\sigma(3)}-x_{\sigma(4)}))=0\;.$$

Note that the equation will typically have infinitely many roots, which are not artifacts but correspond to infinitely many sinusoidals that go through your four points; you'll need to have some idea which one of these you want, e. g. perhaps the lowest one.

-
@J. M.: Thanks for the edit, but the spelling I had is in use, too; see dictionary.reference.com/browse/artefact. –  joriki Aug 23 '11 at 14:18
joriki: Came to me hours after I left the computer... :D –  Guess who it is. Aug 24 '11 at 2:27
@J. M.: I never thought about it before, but I guess I prefer "artefact" because it's "Artefakt" in German and because that reflects the correct Latin case: "arte factum", made with/through/by art/craft, not "arti factum", made for art/craft. By the way, does it work to ping you like this? I'm never sure with the early space in your name... –  joriki Aug 24 '11 at 2:41
Yes, the ping works nicely with that; Didier and Wille once tried pinging me without the space and it didn't work now (but it used to). –  Guess who it is. Aug 24 '11 at 2:53