# A problem on sinusoids

Given $N$ points $\{x_i\} \in (0,1)$ and $N$ real numbers $\{d_i\}$ such that $\sum\limits_{i=1}^{N}d_i = 0$. Can we find a function of the form $$f(x) = A_k\sin(\pi x + \theta_k), x_{k-1}\le x \le x_{k}, k = 1,2,..N+1$$ assume $x_0 = 0, x_{N+1} = 1$.

Determine $A_k$ and $\theta_k$ such that $f$ is continuous and $$\sqrt{\sum\limits_{k = 1}^N(f(x_i)-d_i)^2}$$ is minimum. Is the solution unique?

• Do I understand correctly that you need a piecewise defined function that passes as close as possible to the assigned points? Am I missing something that prevents us from making every piece of this function start exactly at the left point $(x_{k-1},d_{k-1}$ and end at $x_k,d_k$, where it meets the next piece, satisfying the continuity request and making the sum null (thus obviously minimum)? May 16, 2017 at 20:48
• @lesath82 : You are wrong. What makes you think I want any piecewise function, while I clearly mentioned the exact type of piecewise function I need. Just determine $A_k$'s and $\theta_k$'s. You cannot have null in this problem. May 17, 2017 at 1:30
• I've posted my attempt. I'm sorry I wasn't clear, I didn't want to use any piecewise function, I believed that there is enough room to tune the function you give to satisfy my idea. I think I succeeded, tell me if I missed something essential May 18, 2017 at 23:26

My strategy is to define every piece $f_k(x)$ of $f(x)$ so that it starts exactly at $(x_{k-1}, d_{k-1})$ and ends at $(x_k,d_k)$, where it meets the next piece ensuring continuity. If this can be done, it means that the complete function not only goes close to the target points, but it touches them all, making the required sum null (thus minimum). Moreover it wouldn't be necessary to force the condition $\sum d_i = 0$.

So, for the $k^{th}$ piece I am imposing the conditions:

$$\begin{cases} A_k \sin(\pi x_{k-1} + \theta_k) = d_{k-1}\\ A_k \sin(\pi x_k + \theta_k) = d_k \end{cases}$$

This is not a linear problem, but thinking naively we have two conditions to be satisfied tuning two parameters ($A_k$ and $\theta_k$) so that it is worth trying. For $k=1$ and $k=N+1$ only the innermost side is constrained so it is trivial to find compliant pieces $f_1(x)$ and $f_{N+1}(x)$.

I'll extract $\theta_k$ from the first condition. Due to the nature of the $\sin$ function and its inverse, I have two possibilities:

$$\theta_k = \arcsin \left(\frac{d_{k-1}}{A_k}\right)-\pi x_{k-1}\\ \theta'_k = \pi - \arcsin \left(\frac{d_{k-1}}{A_k}\right)-\pi x_{k-1}$$ I've found that the second one ends up giving the same function as the first, so I'll look only at $\theta_k$. Putting it in the second condition I have $$\require{cancel} A_k \sin \left( \underbrace{\pi x_k - \pi x_{k-1}}_{:= \ \beta} + \arcsin \left(\frac{d_{k-1}}{A_k}\right) \right)=d_k\\ A_k \cos(\beta) \sin \left(\arcsin\left(\frac{d_{k-1}}{A_k}\right)\right)+A_k\sin(\beta)\cos\left(\arcsin\left(\frac{d_{k-1}}{A_k}\right)\right)=d_k\\ \cancel{A_k}\cos(\beta)\frac{d_{k-1}}{\cancel{A_k}}\pm A_k\sin(\beta)\sqrt{1-\frac{{d_{k-1}}^2}{A_k^2}}=d_k\\ \cos(\beta)d_{k-1}\pm \sin(\beta)\sqrt{A_k^2-d^2_{k-1}}=d_k\\ \pm\sqrt{A_k^2-d^2_{k-1}}=\frac{d_k-\cos(\beta)d_{k-1}}{\sin(\beta)}\\ A_k^2=\left(\frac{d_k-\cos(\beta)d_{k-1}}{\sin(\beta)}\right)^2+d^2_{k-1}\\ A_k^2=\frac{1}{\sin^2(\beta)}\left(d_k^2-2\cos(\beta)d_k d_{k-1}+\cos^2(\beta)d^2_{k-1}+\sin^2(\beta)d^2_{k-1}\right)\\ A_k^\pm=\pm\frac{1}{\sin(\beta)}\sqrt{d_k^2+d^2_{k-1}-2\cos(\beta)d_k d_{k-1}}$$ Now I can go back to plug these parameters in the first condition to get $$\theta_k^\pm=\pm\arcsin \left( \frac{d_{k-1}\sin(\beta)}{\sqrt{d_k^2+d^2_{k-1}-2\cos(\beta)d_k d_{k-1}}} \right)-\pi x_{k-1}$$ Finally for each $k=2 \ldots N$ we have: $$f^\pm_k(x) = A^\pm_k \sin(\pi x + \theta^\pm_k)$$ where it turns out that only one of the two satisfies the rightmost condition too. Gluing together the correct ones I build the complete $f(x)$.

I've tested my result generating some random sets $\{ x_i \}$ (uniform distribution in $(0,1)$ ) and $\{ d_i \}$ (normal distribution around $0$) for different $N$'s. Here below the plots (dashed lines when $f^-_k$ was chosen). Matlab's code is available if needed.

• Excellent! It was not abvious to me that you can pass a sinusoid through two given points just by manipulating the amplitude and phase. Perhaps it not trivial because it is very nonlinear. I have not gone through fully and will get back soon. May 19, 2017 at 6:52
• It'd be great if you could share the matlab code. The figures look great on random data. I'd like to take a well known curve, say a sinuoid and then pick random points from it, or alternative points as data and then try this techniqe and see how good it looks. May 19, 2017 at 6:54
• @Rajesh Sent! ;-) May 19, 2017 at 7:44