It's hard to explain it but i'll take it step by step
Let's say I've 2 cars, one following another, and I've the speed of the lead car, and I want to calculate the distance between two of them, and we can calculate the distance using multiple equations, also I know the initial speed of the following car and the distance between two of them.
Following_Car_Speed = 13.68490 m/s
Distance = 17.024 m
Lead_Car_Speed = c(13.784896, 13.745834, 13.880556, 13.893577, 13.893577, 13.923959,
13.945661, 13.919619, 13.897917, 14.002257, 14.002257, 13.980556,
13.980556, 14.067536, 14.063195, 14.080556, 14.123959, 14.163021,
14.236806, 14.167188)
Delta_Speed = Lead_Car_Speed[1]-Following_Car_Speed = 13.784896-13.68490 = 0.1
Gap <- 1.554 + Following_Car_Speed*0.878- (Following_Car_Speed*Delta_Speed)/(2*sqrt(0.8418*0.8150))=
1.554+ 13.68490*0.878- (13.68490*0.1)/(2*sqrt(0.8418*0.8150) = 12.74325
Acceleration <- 0.8418*(1-(Following_Car_Speed/29.2)^3.52-(Gap/Distance)^2)=0.3116923
Now I've calculated the acceleration, so I've to calculate the new speed of the following car .
Following_Car_Speed <- Following_Car_Speed + Acceleration*0.1
So now I've to calculate the new delta in speed between the lead and following car
Delta_Speed <- Lead_Car_Speed[2]-Following_Car_Speed
Distance<- Distance+(Delta_Speed[2]+Delta_Speed[1])/2*0.1
Then continue using the same equations till we end all the values of the following car.
It's easy to do this using For loops, but i want to get a more efficient way, I tried to use dplyr
, but it's hard and I failed to get an answer.
So please help me.
myfun <- function(list, lcs,lcs2){
ds <- lcs - list[[1]]
Distance <- list[[1]]*D_Time - (list[[1]] * ds) / (2*sqrt(M_Acc*Com_Acc))
if (Distance < 0|is.na(Distance)) {Distance <- 0}
gap <- Gap_J + Distance
acc <- M_Acc * (1 - (list[[1]] / D_Speed)^Beta - (gap / list[[2]])^2)
fcs_new <- list[[1]] + acc * 0.1
ds_new <- lcs2- fcs_new
di_new <- list[[2]]+(ds_new+ds)/2*0.1
return(list(Speed = fcs_new,Distance = di_new))
}
Generated_Data <- data %>%group_by(Driver,FileName)%>%
mutate(Speed_Distance_Calibrated = accumulate2( .init = list(Filling_Speed[1],
Filling_Range[1]),.x = Lead_Veh_Speed_F,.y = Lead_Veh_Speed_F2, myfun)[-1])%>%ungroup()
I've add the lead of the lead_car_speed also i wanted to use the new distance and new speed, so i made it into a list and put it into .initla
Your operation depends on the iteration or some sort of recursion to be complete (due to possible
NA
,Inf
etc) - seems likefor
orwhile
is needed.My alternative version is to break down the functions into easier unit for scaling and unit test for larger example or wrap around with another function calls .
I assume your var
Distance
is fixed for some reason and expected output is not provided, so I can't verify the functions completely.Here is a simple way using
accumulate
from thepurrr
package which is part of the tidyverse.First I define a function
myfun
which updates the following_car_speed (fcs).If distance changes as well you can use
accumulate2
rather thanaccumulate
.