使用方程式计算变量,然后使用生成的值生成新的 [英] Calculate variables using equations then use the generated values to generate new one

查看:90
本文介绍了使用方程式计算变量,然后使用生成的值生成新的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很难解释,但我会逐步介绍

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.

使用For循环很容易做到这一点,但是我想获得一种更有效的方法,我尝试使用dplyr,但是很难,但是我没有得到答案.

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.

所以请帮助我.

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()

我添加了lead_car_speed的领先者,我也想使用新的距离和新的速度,因此我将其列入列表并放入.initla

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

推荐答案

以下是使用tidyverse一部分的purrr包中的accumulate的简单方法.

Here is a simple way using accumulate from the purrr package which is part of the tidyverse.

首先,我定义一个函数myfun,该函数将更新following_car_speed(fcs).

First I define a function myfun which updates the following_car_speed (fcs).

myfun <- function(fcs, lcs, di){
  ds <- lcs - fcs
  gap <-  1.554 + fcs*0.878 - fcs * ds / (2*sqrt(0.8418*0.8150))
  acc <- 0.8418 * (1 - (fcs / 29.2)^3.52 - (gap / di)^2)
  fcs_new <- fcs + acc * 0.1

  return(fcs_new)
} 

library(tidyverse)

tibble(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)) %>%
  mutate(following_car_speed = accumulate(lead_car_speed, myfun, .init = 13.68490, di = 17.024)[-1])^


# A tibble: 20 x 2
   lead_car_speed   following_car_speed
            <dbl> <dbl>
 1           13.8  13.7
 2           13.7  13.7
 3           13.9  13.8
 4           13.9  13.8
 5           13.9  13.8
 6           13.9  13.9
 7           13.9  13.9
 8           13.9  13.9
 9           13.9  13.9
10           14.0  14.0
11           14.0  14.0
12           14.0  14.0
13           14.0  14.0
14           14.1  14.1
15           14.1  14.1
16           14.1  14.1
17           14.1  14.1
18           14.2  14.1
19           14.2  14.2
20           14.2  14.2

如果距离也发生变化,则可以使用accumulate2而不是accumulate.

If distance changes as well you can use accumulate2 rather than accumulate.

这篇关于使用方程式计算变量,然后使用生成的值生成新的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆