I want to create a simple function that will carry out a numerical integration. It loops over values carrying out a simple if else check. For the life of me I cannot see why Im getting the error shown below. Any help would be greatly appreciated.
Code is
Vel <- function(Acc, dt) {
L <- length(Acc) # obtain the length of the Accellerogram
V <- rep(0, L) # create a vector of 0s of length L
for (i in 1:L-1) {
Test <- Acc[i] * Acc[i+1]
if (Test >= 0) { # i.e. are the two values either both +ve or both -ve
V[i+1] <- V[i] + dt * (Acc[i] + Acc[i+1]) / 2
}
else { # i.e. if the one value is +ve and one -ve
dtt <- Acc[i] / ((Acc[i] - Acc[i+1]) / dt)
V[i+1] <- V[i] + 0.5 * Acc[i] * dtt + 0.5 * Acc[i+1] * (dt-dtt)
}
}
}
Acc <- c(0,1,0.5,1,-3,-2,0)
dt <- 0.005
V <- Vel(Acc,dt)
Error in if (Test >= 0) { : missing value where TRUE/FALSE needed