# Convex Unimodal Function

A convex function is a function where a line can be drawn between any two points in the domain and the line remains in the domain.

For a one-dimensional function shown as a two-dimensional plot, this means the function has a bowl shape and the line between two remains above the bowl.

Unimodal means that the function has a single optima. A convex function may or may not be unimodal; similarly, a unimodal function may or may not be convex.

The range for the function below is bounded to -5.0 and 5.0 and the optimal input value is 0.0.

# convex unimodal optimization function

from numpy import arange
from matplotlib import pyplot

# objective function

def objective(x):
return x**2.0

# define range for input

r_min, r_max = -5.0, 5.0

# sample input range uniformly at 0.1 increments

inputs = arange(r_min, r_max, 0.1)

# compute targets

results = objective(inputs)

# create a line plot of input vs result

pyplot.plot(inputs, results)

x_optima = 0.0

# draw a vertical line at the optimal input

pyplot.axvline(x=x_optima, ls=’–’, color=‘red’)

pyplot.show()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# convex unimodal optimization function

from numpy import arange
from matplotlib import pyplot

# objective function

def objective(x):
return x**2.0

# define range for input

r_min, r_max = -5.0, 5.0

# sample input range uniformly at 0.1 increments

inputs = arange(r_min, r_max, 0.1)

# compute targets

results = objective(inputs)

# create a line plot of input vs result

pyplot.plot(inputs, results)

x_optima = 0.0

# draw a vertical line at the optimal input

pyplot.axvline(x=x_optima, ls=’–’, color=‘red’)

# show the plot

pyplot.show()
Running the example creates a line plot of the function and marks the optima with a red line.

Line Plot of Convex Unimodal Optimization Function
Line Plot of Convex Unimodal Optimization Function

This function can be shifted forward or backward on the number line by adding or subtracting a constant value, e.g. 5 + x^2.

This can be useful if there is a desire to move the optimal input away from a value of 0.0.