1

I am wanting to try find the upper and lower bounds of a function in R.

I am trying to use the built in R function 'optimise', but the function tries to find where the function's tangent is zero. For instance, if you run the following code:

phi_function <- function(x) {
  return(((x^6)/8) - ((3*(x^2))/4))
}

lbound_ex <- -1.79550879355662
ubound_ex <- 0.168144378782495

LX_ex <- optimise(phi_function, interval = c(lbound_ex, ubound_ex), maximum = FALSE)$objective
UX_ex <- optimise(phi_function, interval = c(lbound_ex, ubound_ex), maximum = TRUE)$objective

curve(phi_function, from = lbound_ex, to = ubound_ex)
abline(v = lbound_ex)
abline(v = ubound_ex)
abline(h = LX_ex, col = 'red')
abline(h = UX_ex, col = 'red')
abline(h = phi_function(-1.79550879355662), col = 'green')

You get the following graph:

phi_function plot

The red lines are the minimum and maximums given by the 'optimise' function and the green line is the upper bound of phi_function in the interval (lbound_ex, ubound_ex). Does anyone know how to find upper and lower bounds in R?

Thanks in advance for any help! Ryan

user-2147482565
  • 453
  • 7
  • 16
  • 3
    From `?optimize`: If ‘f’ is a unimodal function and the computed values of ‘f’ are always unimodal when separated by at least eps * |x| + (tol/3), then x_0 approximates the abscissa of the global minimum of ‘f’ on the interval ‘lower,upper’ with an error less than eps * |x_0|+ tol. If ‘f’ is not unimodal, then ‘optimize()’ may approximate a local, but perhaps non-global, minimum to the same accuracy. – nicola Jan 14 '19 at 17:11
  • 2
    It's looking for points where the first derivative of the function changes sign. It's going to miss maxima and minima which occur at the boundaries of the interval. You need to check those yourself. – Joseph Clark McIntyre Jan 14 '19 at 17:15

1 Answers1

4

I don't think the green line represents a maximum (or minimum). They are defined as point where the slope of the tangent is 0, and the slope.

Your function is not unimodal (only one peak), and the max and min are outside your range.

The function optimize will search the upper and lower interval and look for maximum and minimums in the continuous function in the interval. If you extend your function you will find there is another maximum at (0,0)

Carlos Santillan
  • 1,077
  • 7
  • 8
  • Okay thanks, yes you're right about that. For what I need I need a function to find the upper and lower bounds, not the maximum. I will edit the question. – user-2147482565 Jan 14 '19 at 17:23
  • 2
    So use optimize and also check the endpoints of your interval. – Gregor Thomas Jan 14 '19 at 17:24
  • `optimize()` does find *one* minimum and/or maximum, but this may not be the **global** optimum in the intervall. You will need to check *all* minima and maxima plus the function values at the end points. For identifying the global minima and maxima, the function `pracma::findmins()` may be of use. – Hans W. Feb 02 '19 at 20:27