optimize() function in R: How to use the optimize function in R

optimize () function in R

In this tutorial, you will learn how to use the optimize() function in R. The optimize() function in R can help you search for the minimum or maximum value of the given function. Let’s follow this article to learn more about it with the explanation and examples below.

Optimize function in R

What does optimize() do in R?

The optimize() function in R (programming language) can help you search the minimum or maximum value of your function in the interval from the lower value to the upper value. It is used in the problem of finding the extreme point of a function.

Syntax:

optimize(function, interval, lower = min(interval), upper = max(interval), maximum = FALSE)

Parameters:

  • function: The function you want to search for the minimum or maximum value.
  • interval: The vector contains the start-point and the end-point.
  • lower: The start-point of the interval to be searched.
  • upper: The end-point of the interval to be searched.
  • maximum: The default is FALSE. Searching for the maximum value or not.

After learning the usage and syntax of the optimize() function in R, you will learn how to use it in the next title below.

How to use the optimize function in R?

The optimize() function can help you find the minimum value or maximum value of the given function. In mathematics, you can use it to find the extreme point of a function.

Find the minimum value with the optimize() function

You can find the minimum value of the given function with the optimize() function and the location of the minimum value.

Look at the example below to learn more about this solution.

# Create the function.
my_func <- function(x) {
    x^4 +3*x^2 -5*x +6
}

# Use the optimize() function to find the minimum value of this function.
optimize(my_func, interval = c (-2,2))

Output

$minimum
[1] 0.6501538

$objective
[1] 4.196006

The first value in this output is the position of the minimum value (0.6501538), and the second is the minimum value (4.19606).

Find the maximum value with the optimize() function

You can find the maximum value of the given function with the optimize() function and the location of the maximum value.

Look at the example below to learn more about this solution.

# Create the function.
my_func <- function(x) {
    x^4 +3*x^2 -5*x +6
}

# Use the optimize() function to find the minimum value of this function.
optimize(my_func, interval = c (-2,2), maximum = TRUE)

Output

$maximum
[1] -1.99994

$objective
[1] 43.99705

The first value in this output is the position of the maximum value (-1.99994), and the second is the maximum value (43.99705).

Summary

You have learned about the usage, syntax, and how to use the optimize() function in R. By this function, you can find the extreme point of a function. If you have any questions about this tutorial, leave a comment below. I will answer your questions. Thanks!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *