I have this function
hazwil2 <- function(diam, flow, leng){
psi2=((1/(2.31*100))*1050*((flow/140)^1.852)*leng*diam^-4.87)
return(psi2)
}
I'm trying to use optimize()
to minimize psi2
by varying the value of diam. I'm not looking for an absolute minimum but for the smallest value of diam that will keep psi2
below 2. Also, I should mention that diam
is the diameter of a pipe, so it can only be a discrete values, 2, 3, 4, 5, or 6.
When I use:
optimize(
f = hazwil2,
interval = c(0.1,12),
flow = 100,
leng = 400
)
I get the largest possible value of diam
(11.9), and a very small value for psi2
. My function is monotonic so the minimization of psi2
will always result in the largest possible diam
. I guess I'm struggling to set the constraints for my optimize()
function.
uniroot()
finds the location where a curve/function crosses zero. Subtract 2 from the PSI to find the minimum allowable diameter. It crosses at 3.564, so 4 is the smallest integer that keeps psi under 2.
intercept <- 2L
uniroot(
function(x) hazwil2(x, 100, 400) - intercept ,
interval = c(3, 4)
)
curve(expr=hazwil2(x, 100, 400), from=2.5, to=5)
abline(a=intercept, b=0, col="blue")
The curve()
graph influenced the arbitrary decision to search diameters between 3 and 4.
Result of `uniroot()`:
$`root`
[1] 3.564091
$f.root
[1] -5.618425e-05
$iter
[1] 6
$init.it
[1] NA
$estim.prec
[1] 6.103516e-05
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.