# Computer Programming/Physics/Position of an accelerating body function (constant acceleration)

The position of an accelerating body is a mathematical function expressed as:

$\mathbf{s}(t) = \begin{matrix}\frac{1}{2}\end{matrix} \mathbf{a} t^2 + \mathbf{v}_0 t + \mathbf{s}_0 \quad\$   if a is constant.

a is acceleration v is initial velocity t is time s is starting position

It is expressed below in various programming languages

# AssemblyEdit

## Intel x86 (32-bit)Edit

; eax = a, ebx = v, ecx = t, edx = s
push edx
mul ecx
mul ecx
shr eax,1
add DWORD PTR [esp],eax
mov eax,ebx
mul ecx
add DWORD PTR [esp],eax
pop eax


## Intel x86 (32-bit) using the FPUEdit

somewhere in the data section:
a dd 0.0 ; m*s^-2
s dd 0.0 ; m
v dd 0.0 ; m*s^-1
t dd 0.0 ; s
half dd 0.5 ; scalar

code:
push 40000000h
fld DWORD PTR [esp]
fld t
fyl2x
fld st(0)
frndint
fsub st(1),st(0)
fxch st(1)
f2xm1
fld1
fadd
fscale
fstp st(1)
fld a
fmul
fld half
fmul
fld v
fld t
fmul
fadd
fld s
fadd
add esp,4
ffree st(0)


# CEdit

inline float PositionofAcceleratingBody(float a, float v, float t, float s)
{
return .5 * a * t * t + v * t + s;
}


# C++Edit

template<class Vector,class Number>
inline Vector PositionofAcceleratingBody(Vector a,Vector v0,Vector s0,Number t)
{
return (0.5*a*t+v0)*t+s0;
}



# DelphiEdit

function PositionofAcceleratingBody(real a, v, t, s): real;
begin
result := .5 * a * t * t + v * t + s;
end;


# PHPEdit

function PositionofAcceleratingBody($a,$v, $t,$s)
{
return .5 * $a *$t * $t +$v * $t +$s;
}


# JavaEdit

public double positionOfAcceleratingBody(double a, double v, double t, double s){
return a * t * t / 2 + v * t + s;
}


# PythonEdit

def positionOfAcceleratingBody(a, v, t, s):
return .5 * a * (t ** 2) + v * t + s

Last modified on 18 December 2008, at 01:42