Fitting a sinusoid to $n$ points

66 Views Asked by At

I was inspired by a closed question to consider the following problem.

Given a set of $n$ points $\{ (x_i, y_i) , i = 1,..., n \}$ where $n \ge 4 $, find the sinusoid function of the form

$ y = f(x) = a \cos (k x )+ b \sin (k x) + c $

that fits these points, assuming they do lie on a single sinusoid.

My attempt:

Define the error function

$E = \displaystyle \sum_{i=1}^n \left( a \cos (k x_i) + b \sin (k x_i) + c - y_i \right)^2 $

Then find its partial derivatives $\dfrac{\partial E}{\partial a}, \dfrac{\partial E}{\partial b}, \dfrac{\partial E}{\partial c}, \dfrac{\partial E}{\partial k} $, set them to zero, and numerically solve the resulting nonlinear system of $4$ equations in the parameters $a,b,c,k$.

EDIT: I have implemented this method of solving for the parameters by solving the nonlinear system of the $4$ partial derivatives being equal to zero, but it did not work as I hoped it would (it diverged). So I modified the method and used the steepest descent (the simplest minimization algorithm) to minimize the error function, with $n = 8$, and it did converge, but it needs more testing over several choice of parameters and different number of points.

My Question:

I wonder if a non-numerical method exists for finding $a,b,c,k$ in closed form.

Thank you for any hints or full solutions.

1

There are 1 best solutions below

1
On BEST ANSWER

For the general case of $n$ data points

Suppose that $k$ is known. Define for this value $C_i=\cos(kx_i)$ and $S_i=\sin(kx_i)$. So, the model is $$y=a \,C+b \,S+c$$ Use the normal equations $$\sum_{i=1}^n y_i=a \sum_{i=1}^n C_i+\sum_{i=1}^n S_i+n \,c$$ $$\sum_{i=1}^n C_i\,y_i=a \sum_{i=1}^n C^2_i+b\sum_{i=1}^n C_i\,S_i+c\, \sum_{i=1}^n C_i$$ $$\sum_{i=1}^n S_i\,y_i=a \sum_{i=1}^n C_i\,S_i+b\sum_{i=1}^n S^2_i+c\, \sum_{i=1}^n S_i$$

Solve explicitly these three linear equations to have $a(k)$, $b(k)$ and $c(k)$. So now, you just need to minimize $$E(k) = \sum_{i=1}^n \left( a(k) \cos (k x_i) + b(k) \sin( k x_i )+ c(k) - y_i \right)^2$$ which you can plot to locate more or less its minimum. At this point, start a Newton method using numerical derivatives to make $E'(k)=0$.