Guest User

Untitled

a guest
Apr 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. CL <- 5
  2. V1 <- 19
  3. Rate <- 1000
  4. Q <- 0.05
  5. V2 <- 5.263157895
  6. time <- seq(0, 48, 1)
  7.  
  8. library(deSolve)
  9. library(dplyr)
  10.  
  11. #deSolve function
  12. comp2Inf <- function(t, state, parameters) {
  13. with(as.list(c(state, parameters)), {
  14. #Differential equations
  15. dC1 <- Rate/V1 - C1*(CL/V1) - C1*(Q/V1) + C2*(Q/V1)
  16. dC2 <- C1*(Q/V2) - C2*(Q/V2)
  17. #return rate of change
  18. list(c(dC1, dC2))
  19. })
  20. }
  21.  
  22. ref_fcn <- function(times, Rate, CL, V1, Q, V2) {
  23. b = (Q/V1+Q/V2+CL/V1-sqrt((Q/V1+Q/V2+CL/V1)**2-4*Q*CL/V1/V2))/2
  24. a = Q*CL/V1/V2/b
  25. A = (a-Q/V2)/V1/(a-b)
  26. B = (b-Q/V2)/V1/(b-a)
  27. return(sapply(times, function(t) {
  28. Rate*(A/a*(1-exp(-a*t))+B/b*(1-exp(-b*t)))
  29. }))
  30. }
  31.  
  32. my_fcn <- function(times, Rate, CL, V1, Q, V2) {
  33. a = (Q/V2+Q/V1+CL/V1-sqrt((Q/V2+Q/V1+CL/V1)**2-4*CL*Q/V1/V2))/2
  34. b = (Q/V2+Q/V1+CL/V1+sqrt((Q/V2+Q/V1+CL/V1)**2-4*CL*Q/V1/V2))/2
  35. return(sapply(times, function(t) {
  36. Rate*(Q/a/b+(Q-(V2*a))*exp(-a*t)/a/(a-b)+(Q-(V2*b))*exp(-b*t)/b/(b-a))
  37. }))
  38. }
  39.  
  40. tab_deS <- ode(y = c(C1=0,C2=0), times = time, func = comp2Inf, parms = list(Rate=Rate, CL=CL, V1=V1, Q=Q, V2=V2)) %>% as.matrix
  41. arr_ref <- ref_fcn(time, Rate, CL, V1, Q, V2)
  42. arr_my <- my_fcn(time, Rate, CL, V1, Q, V2)
  43.  
  44. data.frame(time=time, deS=tab_deS[,2], ref=arr_ref, my=arr_my)
Add Comment
Please, Sign In to add comment