# Deriving relative position from instanteous acceleration and time

I'm working on a mobile app that uses the accelerometer to move a cursor. Although it's technically a computer science problem, once you get past how you get the values, it's more of a math problem, so I figured its more relevant to post here.

I can get the phone's acceleration (in the x, y, and z axis) every time the phone detects a change in acceleration. I can also figure out how long it's been since the last event (i.e. since the last time a phone noticed a change in acceleration). My goal is to create a displacement vector from this information.

I found this paper that suggests I use the following formula:

Xa = (Aa * Ta2)/4

Where Xa is the displacement during interval Ta and Aa is the acceleration during the period.

My prototype used this method and gathered the instantaneous acceleration and time intervals for a set sampling period, and then ran that formula on the data points and took the sum of those values to get total displacement. Unfortunately, the values I got during testing were inconsistent.

I assume that my math is the cause of my problem. What equation/approach should I use so I can turn the information I have (i.e. time between readings, and instantaneous accelerations) into a displacement vector (relative change in distance, direction) for each axis?

Note that if $x(t)$ denotes the displacement in the $x$-direction after $t$ seconds then $x''(t)= f(t)$ for $x \in [0,T]$, where $0$ is when the motion starts from rest and $T$ is when the phone again comes to rest.

Integrate: $x'(t) = \int_0^t f(s) ds + c$.

Put $t=0$ in this equation, to see that $c=x'(0)$, the initial velocity in the $x$-direction, which is $0$.

So $x'(t) = \int_0^t f(s) ds$.

Integrate again:

$x(t) = \int_0^t \int_0^u f(u) dudt +x(0) = t \int_0^u f(u) du$.

In particular,

$x(T) = T \int_0^u f(u) du + x_0$.

Similar formulas hold for $y$ and $z$ where we will replace $f$ by $g$ and $h$ respectively.

The total displacement is therefore:

$$\sqrt{\sum_{i=1}^3(x_i(0) - T \int_0^u f_i(u) du)^2}$$

where I have written $x_2 = y, x_3 = z, f_2 = g, f_3 = h$. If you can indeed measure the acceleration in each direction at each time between $0$ and $T$, then you just need to substitute in $f,g,h$ (approximations, probably piecewise-linear for your software) and calculate the integrals.

EDIT: As you say you have instantaneous accelerations at several times between $0$ and $T$ you $f,g$ and $h$ will be piecewise linear, making the above integrals very simple.