In [1]:
using Optim
In [3]:
using Plots
plotlyjs()
ezcontour(x, y, f) = begin
X = repmat(x', length(y), 1)
Y = repmat(y, 1, length(x))
# Evaluate each f(x, y)
Z = map((x,y) -> log(f([x,y])), X, Y)
plot(x, y, Z, st=:contour)
end
Out[3]:
In [4]:
f = x -> (x[1].^2 + x[2] - 11).^2 + (x[1] + x[2].^2 - 7).^2
sol = optimize(f, [0.0;0.0], NelderMead())
xs = sol.minimizer
Out[4]:
In [5]:
ezcontour(-5:0.02:5, -5:0.02:5, z -> f(z)[1])
scatter!([xs[1]],[xs[2]],label=false)
Out[5]: