# Computer Programming/Physics/Position of an accelerating body function

Wikisource:Source code

The position of an accelerating body can be described by a mathematical function $\mathbf{s}(t)$. The generalized function can be attained by using the Taylor series

$\mathbf{s}(t+t_0)=\sum\frac{t^n}{n!}\mathbf{s}^{(n)}(t_0)$,

where $\mathbf{s}^{(n)}$ is the $nth$ derivative of $\mathbf{s}$:

• $\mathbf{s}^{(0)}=\frac{d^0\mathbf{s}}{dt^0}=\mathbf{s}$
• $\mathbf{s}^{(1)}=\frac{d^1\mathbf{s}}{dt^1}=\mathbf{v}$
• $\mathbf{s}^{(2)}=\frac{d^2\mathbf{s}}{dt^2}=\mathbf{a}$
• etc.

The accuracy of this function depends on the number of terms used as $\frac{1}{n!}$ decreases rapidly. Additionally, the time $t$ can be synchronized such that $t_0=0$ (Maclaurin series).

Note that for a constant acceleration most of the terms become zero and we're left with

$s(t)=\frac{1}{0!}\mathbf{s}_0+\frac{t}{1!}\mathbf{s}^{(1)}_0+\frac{t^2}{2!}\mathbf{s}^{(2)}_0$

or

$\mathbf{s}(t)=\mathbf{s}_0+\mathbf{v}_0t+\frac{1}{2}\mathbf{a}t^2$

## C++

template<class Vector,class Number>
Vector PositionAcceleratingBody(Vector *s0,Number t,size_t Accuracy)
{
Vector s(0);     //set to zero if int, float, etc. or invoke the
//     "set to zero" constructor for a class
Number factor(1);//0!==1 and t^0==1
for(size_t n(0);n<Accuracy;n++)
{
if(n)factor*=(t/n);//0!==1 and t^0==1
s+=(factor*s0[n]); //s0 is the array of nth derivatives of s
//     at t=t0=0
}
return s;
}



## Justification for Using the Taylor Series

The Taylor series can be derived by systematically selecting which of our variables is a constant and then extrapolating that to the infinite limit.

• Constant Position
$s(t)=s_0$
or
$s(t)=\frac{t^0}{0!}s^{(0)}(t_0=0)$
• Constant Velocity
$v(t)=v_0$
$s(t)=\int vdt=\int v_0dt$
$s(t)=v_0t+s_0$
or
$s(t)=\frac{t^0}{0!}s^{(0)}(t_0=0)+\frac{t^1}{1!}s^{(1)}(t_0=0)$
• Constant Acceleration
$a(t)=a_0$
$v(t)=\int adt=\int a_0dt$
$v(t)=a_0t+v_0$
$s(t)=\int vdt=\int (a_0t+v_0)dt$
$s(t)=\frac{1}{2}a_0t^2+v_0t+s_0$
or
$s(t)=\frac{t^0}{0!}s^{(0)}(t_0=0)+\frac{t^1}{1!}s^{(1)}(t_0=0)+\frac{t^2}{2!}s^{(2)}(t_0=0)$
• Constant Rate of Change of Acceleration
$a^{(1)}(t)=a^{(1)}_0$
$a(t)=\int a^{(1)}dt=\int a^{(1)}_0dt$
$a(t)=a^{(1)}_0t+a_0$
$v(t)=\int adt=\int (a^{(1)}_0t+a_0)dt$
$v(t)=\frac{1}{2}a^{(1)}_0t^2+a_0t+v_0$
$s(t)=\int vdt=\int (\frac{1}{2}a^{(1)}_0t^2+a_0t+v_0)dt$
$s(t)=\frac{1}{6}a^{(1)}_0t^3+\frac{1}{2}a_0t^2+v_0t+s_0$
or
$s(t)=\frac{t^0}{0!}s^{(0)}(t_0=0)+\frac{t^1}{1!}s^{(1)}(t_0=0)+\frac{t^2}{2!}s^{(2)}(t_0=0)+\frac{t^3}{3!}s^{(3)}(t_0=0)$
• etc.