library(Deriv)
## Warning: package 'Deriv' was built under R version 3.4.3

question:

4.3.13: A powerline is to be run to an offshore facility in the manner described in Example 102. The offshore facility is 2 miles at sea and 5 miles along the shoreline from the power plant. It costs $ $50,000 per mile to lay a power line underground and $80,000 to run the line underwater.

How much of the power line should be run underground to minimize the overall costs?

figure out the equation.

c(x) = 50000(5 - x) + 80000(sqrt(x^2 + 2^2))

fun_c <-function (x) {
  50000*(5 - x) + 80000*(sqrt(x^2 + 2^2)) 
}

find the min and max

min_c<- fun_c(0)
min_c
## [1] 410000
max_c <- fun_c(5)
max_c
## [1] 430813.2

the derivative of this function is:

derv <- function(x){
  -50000 + (80000 * x / sqrt(x^2 +4))
}

set derviative to 0 and solve for x

derv(0)
## [1] -50000
(80000^2)-(50000^2)
## [1] 3.9e+09
x <- sqrt(10000000000) / sqrt(3900000000)
x
## [1] 1.601282
fun_c(x)
## [1] 374900

The minimum cost is 374900