# R/kinematics.R In kinematics: Studying Sampled Trajectories

```# Kinematics-related functions

#' Approximate derivative
#'
#' @param t Vector of times
#' @param x Vector of values
#'
#' @return A vector (of the same size of t) representing the numerical derivative
#'
approx_derivative <- function(t, x) {
# Turn data into approximating function...
x_fun <- stats::splinefun(t, x)

# ... so numDeriv can be used to differentiate anywhere.

# Why not use just a differences vector?
# Because this way we ensure that both the input and output vectors have the
# same size.

return(dxdt)
}

#' Return speeds
#'
#' @param t The times vector
#' @param x The x positions
#' @param y The y positions
#'
#' @return The speeds
#' @export
#'
#'
speed <- function(t, x, y) {
# Differentiate the positions...
vx <- approx_derivative(t, x)
vy <- approx_derivative(t, y)

# ... and return result as data frame
speeds <- data.frame(vx, vy)

return(speeds)
}

#' Return accelerations
#'
#' @param t The times vector
#' @param x The x positions
#' @param y The y positions
#'
#' @return The accelerations
#' @export
#'
#'
accel <- function(t, x, y) {
# First get speeds...
speeds <- speed(t, x, y)

# ... and then differentiate again
ax <- approx_derivative(t, speeds\$vx)
ay <- approx_derivative(t, speeds\$vy)

# Return result as data frame
accels <- data.frame(ax, ay)

return(accels)
}

#' Return curvatures
#'
#' @param t The times vector
#' @param x The x positions
#' @param y The y positions
#'
#' @return The local curvature
#' @export
#'
#'
curvature <- function(t, x, y) {
# First get speeds and accelerations
speeds <- speed(t, x, y)
aspeed <- sqrt(speeds\$vx^2 + speeds\$vy^2)
accels <- accel(t, x, y)

# Calculate the cross product
cross_prod <- speeds\$vx*accels\$ay - speeds\$vy*accels\$ax

# Apply the definition of curvature
curv <- abs(cross_prod) / abs(aspeed^3)

return(curv)
}

#'
#' @param t The times vector
#' @param x The x positions
#' @param y The y positions
#'
#' @return The local curvature radius
#' @export
#'
#'
curvature_radius <- function(t, x, y) {
# The curvature radius is just the inverse of the local curvature
curvatures <- curvature(t, x, y)

}

#' Return displacements
#'
#' @param x The x positions
#' @param y The y positions
#'
#' @return The displacements between a position and its previous
#' @export
displacement <- function(x, y) {
# Extract the displacements
disp_x <- c(NA, diff(x)) # The NA ensures that displacements have the ...
disp_y <- c(NA, diff(y)) # ... same length as the data