Home > Software design >  Output of a Function is zero not a vector
Output of a Function is zero not a vector

Time:11-13

When I recall this function, its output is zero. The x in output should be a vector containing 365 values based on timepoints from 1 to 365. Maybe there is something wrong with the function definition or returning values.

vax_fun = function(timepoint, parms1){
  with (
    as.list (parms1),
    {
      if (timepoint < T0){
          v_t = 0
          return((v_t))
      } else if (timepoint <= T1){
        v_t = timepoint * (exp(tetha)-C_0)/(T1-T0)   C_0 - T0 * (exp(tetha)-C_0)/(T1-T0)
          return((v_t))
      }else if (timepoint <= T2){
          v_t = (exp(tetha)* timepoint)
          return((v_t))
      } else if (timepoint <= T3){
          v_t = -timepoint * (exp(tetha)-C_1)/(T3-T2)   C_1 - T3 * (exp(tetha)-C_1)/(T3-T2)
          return((v_t))
      } else {
          v_t=0
          return((v_t))
      }
    }
  )
}
parms1=c(
  
  T0=55,
  T1=115,
  T2=175,
  T3=235,
  threshold = 275,
  tetha = 5,
  C_0 = 100,
  C_1 = 100

)

for (timepoint in 1:365){
  x = vax_fun(timepoint, parms1)
  x
}

CodePudding user response:

In the first if case, and in your else, you have v_t == 0, which is a boolean, right? Should just be one =

CodePudding user response:

You are overwriting the results on every pass through the loop:

y <- NULL
for (timepoint in 1:365){
  x = vax_fun(timepoint, parms1)
  y <- c(y, x)
}
str(y)
#  num [1:365] 0 0 0 0 0 0 0 0 0 0 ...
summary(y)
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  -279.2     0.0     0.0  3528.5   123.4 25972.3 
  • Related