# How to fit a sinusoid to 2 points and their gradients

Given the sinusoidal function

$$f(x) = a \cos(n x + b) + c,$$

if I know $f(x_1)$, $f(x_2)$, $f'(x_1)$ and $f'(x_2)$ is it possible to determine $a, b, c$ and $n$, with $x \in [0,\tfrac{2\pi}{n})$

Edit: put bounds on $x$ so that only one complete cycle is considered.

Edit 2: Current progress:

Let $p = p'/n$ and $x = x'-p'$ then

$$f(x) = a \cos(n x') + c$$

and

$$f'(x) = -an\sin(n x') + c$$

Let $x_2 = x_1 + w$, then we have

$$\frac{f'(x_2)}{f'(x_1)} = \frac{-na \sin(nx_1' + nw)}{-na \sin(nx_1')}$$ $$= \cos(nw) + \frac{\sin(nw)}{\tan(nx_1')}$$

Rearranging, $$x_1' = \frac{\tan^{-1} \left( \frac{\sin(nw)}{\frac{f'(x_2)}{f'(x_1)} - \cos(nw)} \right)}{n}$$ which gives $p$ from before $(p = (x_1'-x_1)/n)$.

I'm most interested in finding $n$ so even if $a$ and $c$ can't be found it woudn't matter. Perhaps the above can be rearranged to give $n$?

Edit 2: Answered own question below.

-
I believe it is possible, as we have four equations and four variables. Whether or not such a solution is trivial is not an easy question, and the answer is probably no, as my Mathematica is still frantically searching. Also note that there are infinitely many solutions if there is one, $b, b+2\pi{}, b+4\pi{}, \cdots$. –  George V. Williams Jan 12 '13 at 1:51
Yikes, I'm having no luck with mathematica either. Given n, it seems to solve it easily, but, something must be wrong, because the resulting solution doesn't depend on $x_1$... f[x_] := a Cos[n x + b] + c; soln = Simplify[Solve[{f[x1] == av && f'[x1] == bv && f[x2] == cv && f'[x2] == dv}, {a, b, c}]] –  NeuroFuzzy Jan 12 '13 at 2:20
Thanks, especially for the Mathematica code - I have a copy but didn't know how to enter it in. I have updated to put the usual bound on $x$. –  geometrikal Jan 12 '13 at 2:41

Your question leads to the solvability of $\mathbf{F}(\mathbf{x},\mathbf{y})=0$ where $$\mathbf{x}=(a,n,b,c),$$ $$\mathbf{y}=(f_{1},f_{2},f'_{1},f'_{2}),$$ and $$\mathbf{F}(\mathbf{x},\mathbf{y})=\left[\begin{array}{l}a\cos(nx_{1}+b)+c-f_{1}\\a\cos(nx_{2}+b)+c-f_{2}\\-an\sin(nx_{1}+b)-f'_{1}\\-an\sin(nx_{2}+b)-f'_{2}\end{array}\right].$$

So whenever $$\det J\mathbb{F}(a,n,b,c)=\left|\frac{\partial(F_{1},F_{2},F_{3},F_{4})}{\partial(a,n,b,c)}\right|\neq0,$$ it will follow from the implicit function theorem that the 4-tuple $(a,n,b,c)$ is solvable in terms of the given parameters $(f_{1},f_{2},f'_{1},f'_{2})$, and this will give you the desired sinusoid (note that $\mathbb{F}$ is continuously differentiable with respect to $\mathbb{x}$, so the Jacobian exists, only that its non-vanishing needs to be verified for some $\mathbb{y}$).

On the other hand, from a practical point of view, solving the non-linear system does not appear to be easy in general. A numerical method like Newton iteration could be used though if you actually needed to calculate the parameters, but it doesn't sound like this is what you're after.

-
Thanks @JTian, I'm most interested in finding the value of $n$. I'm hoping for an analytical solution. –  geometrikal Jan 12 '13 at 3:33
It's not typical that you can just express one of the variables you're trying to solve for in a non-linear system as a single analytic expression in terms of everything else. It's certainly not the case here as your attempts show (unfortunately, there is no way that $n$ can be isolated in that expression you currently have). Your best bet is to solve the system numerically. It is possible to interpolate a set of points using a trigonometric polynomial (whose formula is quite explicit), but the superposition of sinusoids is in general not a sinusoid (unless the frequencies are equal). –  Taylor Martin Jan 12 '13 at 4:07
Thanks, JTian. Actually you've touched on what I am trying to do. I'm trying to find a numerical method of finding the maximum of a real trigonometric polynomial of degree n, sampling at equidistant points no more than $\pi / n$ apart to ensure only one local maxima would occur between the points. I have the coefficients of each sinusoid used in the polynomial, so can easily work out derivatives. I've tried fitting a bezier curve given the 2 points and their derivatives, and now this sinusoid. There are a few iterative methods I've seen, but I'd like something that gives a good first guess. –  geometrikal Jan 12 '13 at 10:45

There are trivially many solutions if $n$ is not known.

e.g. consider $f'(x_1) = v$, $f'(x_2) = -v$, $f(x_1) = f(x_2) = 0$ where $x_1 = -x_2$.

Then we have

$$a = \frac{f'(x_1)}{-n\sin(n x_1)}$$ $$b = 0$$ $$c = -a \cos(n x_1)$$ $$n < \frac{\pi}{2 |x_1|}$$

which will satisfies the constraints.

-