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

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

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

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

• ${\displaystyle \mathbf {s} ^{(0)}={\frac {d^{0}\mathbf {s} }{dt^{0}}}=\mathbf {s} }$
• ${\displaystyle \mathbf {s} ^{(1)}={\frac {d^{1}\mathbf {s} }{dt^{1}}}=\mathbf {v} }$
• ${\displaystyle \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 ${\displaystyle {\frac {1}{n!}}}$ decreases rapidly. Additionally, the time ${\displaystyle t}$ can be synchronized such that ${\displaystyle t_{0}=0}$ (Maclaurin series).

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

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

or

${\displaystyle \mathbf {s} (t)=\mathbf {s} _{0}+\mathbf {v} _{0}t+{\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
${\displaystyle s(t)=s_{0}}$
or
${\displaystyle s(t)={\frac {t^{0}}{0!}}s^{(0)}(t_{0}=0)}$
• Constant Velocity
${\displaystyle v(t)=v_{0}}$
${\displaystyle s(t)=\int vdt=\int v_{0}dt}$
${\displaystyle s(t)=v_{0}t+s_{0}}$
or
${\displaystyle s(t)={\frac {t^{0}}{0!}}s^{(0)}(t_{0}=0)+{\frac {t^{1}}{1!}}s^{(1)}(t_{0}=0)}$
• Constant Acceleration
${\displaystyle a(t)=a_{0}}$
${\displaystyle v(t)=\int adt=\int a_{0}dt}$
${\displaystyle v(t)=a_{0}t+v_{0}}$
${\displaystyle s(t)=\int vdt=\int (a_{0}t+v_{0})dt}$
${\displaystyle s(t)={\frac {1}{2}}a_{0}t^{2}+v_{0}t+s_{0}}$
or
${\displaystyle 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
${\displaystyle a^{(1)}(t)=a_{0}^{(1)}}$
${\displaystyle a(t)=\int a^{(1)}dt=\int a_{0}^{(1)}dt}$
${\displaystyle a(t)=a_{0}^{(1)}t+a_{0}}$
${\displaystyle v(t)=\int adt=\int (a_{0}^{(1)}t+a_{0})dt}$
${\displaystyle v(t)={\frac {1}{2}}a_{0}^{(1)}t^{2}+a_{0}t+v_{0}}$
${\displaystyle s(t)=\int vdt=\int ({\frac {1}{2}}a_{0}^{(1)}t^{2}+a_{0}t+v_{0})dt}$
${\displaystyle s(t)={\frac {1}{6}}a_{0}^{(1)}t^{3}+{\frac {1}{2}}a_{0}t^{2}+v_{0}t+s_{0}}$
or
${\displaystyle 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.