Tuesday 16 April 2013

trigonometry - How to Determine a Sinus Function from Samples




I keep failing ungracefully at a seemingly simple task of determing a function to describe the coherence between two measured variables (a, b) and and a resulting value. Here's a complete round of example measurements and their respective result:



x: -0.08    y: -0.28    result: 1.850
x: -0.16 y: -0.25 result: 2.129
x: -0.23 y: -0.19 result: 2.446
x: -0.26 y: -0.11 result: 2.744
x: -0.27 y: -0.03 result: 3.047
x: -0.25 y: +0.06 result: 3.363
x: -0.20 y: +0.13 result: 3.730
x: -0.14 y: +0.20 result: 4.121

x: -0.05 y: +0.22 result: 4.498
x: +0.03 y: +0.22 result: 4.844
x: +0.11 y: +0.19 result: 5.240
x: +0.19 y: +0.14 result: 5.652
x: +0.22 y: +0.08 result: 5.956
x: +0.26 y: -0.01 result: 6.330
x: +0.25 y: -0.10 result: 6.645
x: +0.21 y: -0.18 result: 6.996
x: +0.18 y: -0.24 result: 7.229
x: +0.09 y: -0.29 result: 7.553

x: +0.00 y: -0.29 result: 7.844
x: -0.07 y: -0.28 result: 1.829


I've figured so far that $result = (something + 0.5) \times \pi $, but I can't figure out the $something$ part. x seems to move between -0.27 and +0.27, while y between -0.30 and 0.22. I've tried to visualize their behaviour in circles, for hopefully it brings help rather than further confusion:



Visualization



I seem to have forgotten how to go about solving such a task and been fiddling with Wolfram Alpha for some time now, but can't seem to get behind the concept. Please help!




To sum it all up, the question is: find a function to calculate result from x and y.


Answer





You're right that $x$ and $y$ are related by circles. In fact, by plotting $y$ against $x$, we have the following graph.



enter image description here



This looks pretty close to a circle. The radius of a circle is given by
$$

r = \sqrt{x^{2} + y^{2}},
$$
so by taking the average of each $\sqrt{x^{2} + y^{2}}$ for your data we find that the radius is approximately $r \approx 0.2653$ (to $4$ significant figures). We also find that the average of the $x$ values is $\bar{x} = -0.0085$, and the average of the $y$ values is $\bar{y} = -0.0505$. These give an estimate for the center of the circle.



The next graph shows the circle of radius $r = 0.2653$ and midpoint $(-0.0085, -0.0505)$ (orange) compared to the plot of your data (blue).



enter image description here



It seems to fit fairly well.






Assuming that this is 'good enough', we can parametrise your $(x, y)$ coordinates by replacing the $2$-tuple $(x, y)$ with $\theta$, namely the angle that the point $(x, y)$ makes with the positive $x$-axis. The formula for $\theta$ has a few different formulations depending on what range you want $\theta$ in, but by playing around with the trigonometric identities you can determine which one you want. The formula that I used is
$$
\theta = \begin{cases}
\arctan(y/x),& \text{if $x > 0$},\\
\pi + \arctan(y/x),& \text{if $x < 0$},\\
\mathrm{sgn}(y)\pi/2,& \text{if $x = 0$}.\\
\end{cases}
$$

Note that $\mathrm{sgn}(y)$ is the function that returns the sign of $y$, so
$$
\mathrm{sgn}(y) = \begin{cases}
+1,& \text{if $y > 0$},\\
-1,& \text{if $y < 0$},\\
0,& \text{if $y = 0$}.\\
\end{cases}
$$
Plotting $\theta$ against result, we have the following graph.




enter image description here



Performing simple linear regression on this gives the formula
$$
\text{result} = 6.2865 - \theta.\tag{$\ast$}
$$
The values have been rounded to $4$ decimal places. Note that
$$
6.2865 \approx 6.283185307 \approx 2\pi.
$$

The following graph shows how close the fit is.



enter image description here



The blue dots are the original data, and the orange dots are defined by the line $(\ast)$





Now that we know the relationship, we can write up an estimate for your function. We will suppose that the constant in $(\ast)$ is indeed $2\pi$. If
$$

\text{result} = 2\pi - \theta
$$
and
$$
\theta = \begin{cases}
\arctan(y/x),& \text{if $x > 0$},\\
\pi + \arctan(y/x),& \text{if $x < 0$},\\
\mathrm{sgn}(y)\pi/2,& \text{if $x = 0$},\\
\end{cases}
$$

then it is easy to see that
$$
\text{result} = \begin{cases}
2\pi - \arctan(y/x),& \text{if $x > 0$},\\
\pi - \arctan(y/x),& \text{if $x < 0$},\\
2\pi - \mathrm{sgn}(y)\pi/2,& \text{if $x = 0$}.\\
\end{cases}
$$
The following tables shows how close the values are using this function. The real results are in the 'result' column, and the values using the function are in the 'forecast' column.
$$

\begin{array}{cc|c|c|c}
x & y & \text{result} & \theta & \text{forecast}\\\hline
-0.08 & -0.28 & \color{red}{1.849} & 4.434 & \color{green}{1.85} \\
-0.16 & -0.25 & \color{red}{2.14} & 4.143 & \color{green}{2.129} \\
-0.23 & -0.19 & \color{red}{2.451} & 3.832 & \color{green}{2.446} \\
-0.26 & -0.11 & \color{red}{2.741} & 3.542 & \color{green}{2.744} \\
-0.27 & -0.03 & \color{red}{3.031} & 3.252 & \color{green}{3.047} \\
-0.25 & 0.06 & \color{red}{3.377} & 2.906 & \color{green}{3.363} \\
-0.2 & 0.13 & \color{red}{3.718} & 2.565 & \color{green}{3.73} \\
-0.14 & 0.2 & \color{red}{4.102} & 2.182 & \color{green}{4.121} \\

-0.05 & 0.22 & \color{red}{4.489} & 1.794 & \color{green}{4.498} \\
0.03 & 0.22 & \color{red}{4.848} & 1.435 & \color{green}{4.844} \\
0.11 & 0.19 & \color{red}{5.237} & 1.046 & \color{green}{5.24} \\
0.19 & 0.14 & \color{red}{5.648} & 0.635 & \color{green}{5.652} \\
0.22 & 0.08 & \color{red}{5.934} & 0.349 & \color{green}{5.956} \\
0.26 & -0.01 & \color{red}{6.322} & -0.038 & \color{green}{6.33} \\
0.25 & -0.1 & \color{red}{6.664} & -0.381 & \color{green}{6.645} \\
0.21 & -0.18 & \color{red}{6.992} & -0.709 & \color{green}{6.996} \\
0.18 & -0.24 & \color{red}{7.21} & -0.927 & \color{green}{7.229} \\
0.09 & -0.29 & \color{red}{7.553} & -1.27 & \color{green}{7.553} \\

0 & -0.29 & \color{red}{7.854} & -1.571 & \color{green}{7.844} \\
-0.07 & -0.28 & \color{red}{1.816} & 4.467 & \color{green}{1.829} \\
\end{array}
$$
All values are rounded to $3$ decimal places.


No comments:

Post a Comment

real analysis - How to find $lim_{hrightarrow 0}frac{sin(ha)}{h}$

How to find $\lim_{h\rightarrow 0}\frac{\sin(ha)}{h}$ without lhopital rule? I know when I use lhopital I easy get $$ \lim_{h\rightarrow 0}...