Suppose we have recorded GPS coordinates x 0 ,...,x n at times t 0 ,...,t n whil
ID: 3795952 • Letter: S
Question
Suppose we have recorded GPS coordinates x0,...,xn at times t0,...,tn while running or driving through a straight road. We want to compute the velocity vi and acceleration ai from these position coordinates. Using finite difference approximations, one can establish the formulas
vi = (xi+1-xi-1)/(ti+1-ti-1)
ai = 2(ti+1-ti-1)-1((xi+1-xi/ti+1-xi)-(xi-xi-1/ti-ti-1))
for i,...n-1(vi and ai correspond to the velocity and acceleration at point xi at time ti respectively).
Problem) Write a python function kinematics(i, x, t) for computing vi and ai given the arrays x and t of positon and time coordinates(x0,...,xn and t0,...,tn) and write a Pyhton function test_kinematics function() for testing the implemantation in the case of constant velocity. Set t0=0,t1=0.5,t2=1.5, and t3=2.2, and xi=Vti. Call the kinematics function for the legal i values
Explanation / Answer
#!bin/usr/env python import nose as n """ This module includes a function kinematics which computes the approximate velocity and acceleration of a point given it's position at a set of times. """ def kinematics(x, t): """Kinematics takes two lists as arguments, the first of positions, the second of times""" v = [] a = [] if len(x) != len(t): print "Invalid input. The lengths of the provided arguments must be equal" for i in range(len(x)): if i == 0: #These first two statements I added to take care of index out of bounds errors, but I'm still getting them. v.append("*") a.append("*") elif i == len(x) - 1: v.append("*") a.append("*") else: v.append((x[i + 1] - x[i - 1]) / (float(t[i + 1]) - t[i - 1])) a.append(2.0 / (t[i + 1] - t[i - 1]) * ((x[i + 1] - x[i - 1]) / (t[i + 1] - t[i]) - (x[i] - x[i - 1]) / (t[i] - t[i - 1]))) return v, a def main(): """Runs the function kinematics for x = t = [0, 0.5, 1.5, 2.2]. This implies a constant velocity and zero acceleration. The finite approximation used should match exactly the constant velocity of 1""" print kinematics([0, 0.5, 1.5, 2.2], [0, 0.5, 1.5, 2.2]) def test_kinematics(): test = kinematics([0, 0.5, 1.5, 2.2], [0, 0.5, 1.5, 2.2]) case = (["*",1.0,1.0,"*"], ["*",0.0,0.0,"*"]) #This is going to cause issues, but to find velocity and acceleration values at the endpoints would require additional datapoints in x s = 0 def approx_eq(x,y,tol=0.01): return abs(x - y)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.