We exploreOptim package.

We will first load Optim and Gadfly package. Optim is to use optimize and Gadfly is to plot a graph.


Here, I create a quartric function, a fourth-degree polynomial to test optimize function. From the graph that plots the quartric function, the minimum of the function should lie within the range of 0 and 5.

using Optim, Gadfly 
# univariate 
f1(x) = (x-1)*(x-2)*(x-3)*(x-4)
plot(1:10, x-> f(x))

Let’s test optimize.

optimize(f1, -10.0, 10.0)

Output looks as follows:

Results of Optimization Algorithm
 * Algorithm: Brent's Method
 * Search Interval: [-10.000000, 10.000000]
 * Minimizer: 3.618034e+00
 * Minimum: -1.000000e+00
 * Iterations: 14
 * Convergence: max(|x - x_upper|, |x - x_lower|) <= 2*(1.5e-08*|x|+2.2e-16): true
 * Objective Function Calls: 15

It used Brent’s Method as optimizing algorithm. It searched the minimizer from the search interval that I pre-specified, -10.0 and 10.0. The minimizer is approximately 3.6 and the minimum of the function is -1. The convergence reached with 14 iterations.


We can use optimize for optimizing multivariate function. For example, below I create \( f(x, y)= 1-x^2 - y^2+2x + 4y \). Note that x and y are stored for each element of vectior x below. Also note that optimize finds minimizer. Therefore, depending on how the function looks like, we may need to multiply the function by -1 to make it into a minimization problem. Original f(x,y) looks as follows:

Therefore as can be seen from below code, I’ve multiplied the function by -1 to make it into a minimization problem.

f2(x) =-1*( 1-x[1]^2-x[2]^2+2*x[1]+4*x[2])
x0=[0.0, 0.0]
res=optimize(f2, x0)

Then output looks as follows:


2-element Vector{Float64}:

This implies that when x=1 and y=2, -f(x,y) reaches its minimum or in other words, f(x,y) reaches its maximum.