``````

In [1]:

using Plots
pyplot()

``````
``````

WARNING: No working GUI backend found for matplotlib.

Out[1]:

Plots.PyPlotBackend()

``````
``````

In [2]:

# Let's demonstrate a failure of optimality condition.
# Consider this function
f = (x, y) -> (y - x.^2).*(y - 3*x.^2)
# Create a meshgrid
x = -6:0.1:6
y = -6:0.1:6
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
# Evaluate each f(x, y)
Z = map(f, X, Y)

plot3d(X, Y, Z, seriestype=:surface)

``````
``````

Out[2]:

/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1236: UserWarning: findfont: Font family ['Helvetica'] not found. Falling back to Bitstream Vera Sans
(prop.get_family(), self.defaultFamily[fontext]))
/usr/lib/pymodules/python2.7/matplotlib/font_manager.py:1246: UserWarning: findfont: Could not match :family=Bitstream Vera Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=8.0. Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmb10.ttf
UserWarning)

``````
``````

In [6]:

# What we'll do is observe that it has a local minimizer on any line thorugh
# the origin, but there is no minimizer there!

thetas = 0:0.1:2*pi
anim = @animate for ti = 1:length(thetas)
# ti = 27 # enable this to get stepping behavior
t = thetas[ti]
zt = -5:5

ftheta = z -> f(z*cos(t), z*sin(t))

# create a layout
l = @layout [a{0.5w} b]
p = plot(x, y, Z, st=:contour, layout=l)

plot!(p[1], zt*cos(t), zt*sin(t), line=(:red, 2))

z = -5:0.01:5
zf = map(ftheta, z)
plot!(p[2], z, zf, xaxis=[-5, 5], yaxis=[-3, 8])
end
gif(anim, "optimality-1.gif", fps = 10)

``````
``````

INFO: Saved animation to /home/juser/CS520-2017/optimality-1.gif

Out[6]:

``````
``````

In [7]:

## Let's demonstrate the optimality conditions.
# Create a meshgrid
x = -6:0.1:6
y = -6:0.1:6
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
## We'll consider the rosenbrock function.
h = (x, y) -> (1-x).^2 + 5*(y - x.^2).^2
hlog = (x, y) -> log10(h(x, y))

H = map(hlog, X, Y)

plot3d(X, Y, H, seriestype=:surface, zlim=[-2, 4], c=:darktest)

``````
``````

Out[7]:

``````
``````

In [8]:

## Let's zoom in

# Create a meshgrid
x = 0.975:0.0005:1.025
y = 0.975:0.0005:1.025
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
H = map(h, X, Y)
Plots.contour(x, y, H)

``````
``````

Out[8]:

``````
``````

In [9]:

# More zoom
x = 0.99975:0.000005:1.00025
y = 0.99975:0.000005:1.00025
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
H = map(h, X, Y)
Plots.contour(x, y, H)

``````
``````

Out[9]:

``````
``````

In [ ]:

# Create a meshgrid
x = 0.975:0.0005:1.025
y = 0.975:0.0005:1.025
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
H = map(h, X, Y)
Plots.contour(x, y, H)

## Let's zoom in further
x = 0.99975:0.000005:1.00025
y = 0.99975:0.000005:1.00025
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
H = map(h, X, Y)
Plots.contour(x, y, H)

## Let's zoom in further
x = 0.9999975:0.00000005:1.0000025
y = 0.9999975:0.00000005:1.0000025
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
H = map(h, X, Y)
Plots.contour(x, y, H)

## Let's look at a 3d plot
H = map(hlog, X, Y)

plot3d(X, Y, H, seriestype=:surface, zlim=[-15, -9],
c=:darktest, clim=[-12, -9])

``````