# Search the Interval for Minimum and Maximum of the Function in R Programming – optimize() Function

`optimize() or optimise()` function in R Language is used to search the interval from lower to upper for a minimum or maximum of the function f with respect to its first argument.

Syntax: optimize(f, interval, maximum)

Parameters:
f: the function to be optimized. The function is either minimized or maximized over its first argument depending on the value of maximum.

interval: a vector containing the end-points of the interval to be searched for the minimum.

maximum: the logical value says to maximize or minimize. Its default value is minimize.

Example 1:

 `# R program to illustrate ` `# optimize function ` ` `  `# Specifing a funtion ` `f <``-` `function(x) {``5` `*` `x ^ ``2` `-` `12` `*` `x ``+` `17``} ` ` `  `# Calling the optimize() function ` `# over the interval of -5 to 5, to ` `# minimize the value ` `optimize(f, interval ``=` `c(``-``5``, ``5``)) `

Output:

```\$minimum
 1.2

\$objective
 9.8
```

Example 2:

 `# R program to illustrate ` `# optimize function ` ` `  `# Specifing a funtion ` `f <``-` `function(x) {``5` `*` `x ^ ``2` `-` `12` `*` `x ``+` `17``} ` ` `  `# Calling the optimize() function ` `# over the interval of -5 to 5, to ` `# maximize the value ` `optimize(f, interval ``=` `c(``-``5``, ``5``), maximum ``=` `T) `

Output:

```\$maximum
 -4.999944

\$objective
 201.9965
```
My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.