 # Help making a for loop for a set of linked equations

Hi, I am an absolute newbie to R, but I'm having some issues!
Basically I want to plug in multiple values in a vector "p" and get multiple outputs "SSE", but make it so I can put in any number of p values and get an answer for each one. I've tried making a for loop but I'm really lost. Any ideas? (also sorry for the format, I had some issues with reprex )

A <- 1.131
Vd <- 0.4
Vr <- 1.2
Vs <- 0.6
f <- 1- (Vs / Vr)
Cd0 <- 489.6
Cr0 <- 0
t <- c(0, 600, 1200, 1800, 2400, 3000, 3600)
Cr_exp <- c(0, 22.1, 26.7, 28.6, 27.2, 25.3, 23.5)
p <- c(1, 0.00005)
Cd_t2 <- c(Cr0 * Vr + Cd0 * Vd)
E2 <- c(Cr0)
E3 <- c(Cd_t2 / (Vr + Vd) + (Cr0 - Cd_t2 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * t))
Cd_t3 <- c(Cd_t2 - Vs * E3)
E4 <- c(Cd_t3 / (Vr + Vd) + (E3 * f - Cd_t3 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t4 <- c(Cd_t3 - Vs * E4)
E5 <- c(Cd_t4 / (Vr + Vd) + (E4 * f - Cd_t4 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t5 <- c(Cd_t4 - Vs * E5)
E6 <- c(Cd_t5 / (Vr + Vd) + (E5 * f - Cd_t5 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t6 <- c(Cd_t5 - Vs * E6)
E7 <- c(Cd_t6 / (Vr + Vd) + (E6 * f - Cd_t6 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t7 <- c(Cd_t6 - Vs * E7)
E8 <- c(Cd_t7 / (Vr + Vd) + (E7 * f - Cd_t7 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
d <- 0
Cft <- c(d, E2, E3, E4, E5, E6, E7, E8)
repp_Cr_exp <- rep(c(Cr_exp),each=2)
diff_2_Cr <- c((repp_Cr_exp - Cft)^2)
SSE <- c(sum(diff_2_Cr[i]))

``````get_sse <- function(x) {
A <- 1.131
Vd <- 0.4
Vr <- 1.2
Vs <- 0.6
f <- 1- (Vs / Vr)
Cd0 <- 489.6
Cr0 <- 0
t <- c(0, 600, 1200, 1800, 2400, 3000, 3600)
Cr_exp <- c(0, 22.1, 26.7, 28.6, 27.2, 25.3, 23.5)
p <- c(1, x)
Cd_t2 <- c(Cr0 * Vr + Cd0 * Vd)
E2 <- c(Cr0)
E3 <- c(Cd_t2 / (Vr + Vd) + (Cr0 - Cd_t2 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * t))
Cd_t3 <- c(Cd_t2 - Vs * E3)
E4 <- c(Cd_t3 / (Vr + Vd) + (E3 * f - Cd_t3 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t4 <- c(Cd_t3 - Vs * E4)
E5 <- c(Cd_t4 / (Vr + Vd) + (E4 * f - Cd_t4 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t5 <- c(Cd_t4 - Vs * E5)
E6 <- c(Cd_t5 / (Vr + Vd) + (E5 * f - Cd_t5 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t6 <- c(Cd_t5 - Vs * E6)
E7 <- c(Cd_t6 / (Vr + Vd) + (E6 * f - Cd_t6 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
Cd_t7 <- c(Cd_t6 - Vs * E7)
E8 <- c(Cd_t7 / (Vr + Vd) + (E7 * f - Cd_t7 / (Vr + Vd)) * exp(-A * p * (1 / Vd + 1 / Vr) * (t - t)))
d <- 0
Cft <- c(d, E2, E3, E4, E5, E6, E7, E8)
repp_Cr_exp <- rep(c(Cr_exp),each=2)
diff_2_Cr <- c((repp_Cr_exp - Cft)^2)
sum(diff_2_Cr)
}

get_sse(0.00005)
#>  13443.7

the_ps <- c(0.5,0.05,0.005,0.0005,0.00005)

unlist(lapply(the_ps,get_sse))
#>  26200.39 26200.39 26200.13 19994.68 13443.70
``````

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.