In [2]:
using Plots
pyplot(tickfont=font("sans-serif"), titlefont=font("sans-serif"), legendfont=font("sans-serif"))


Out[2]:
Plots.PyPlotBackend()

In [3]:
# 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.05:6
y = -6:0.05: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)
#zlims!(0,50.0)


Out[3]:

In [4]:
# Show the plot in log-space
plot3d(X, Y, log(Z-minimum(Z)+sqrt(eps(1.0))), seriestype=:surface)


Out[4]:

In [ ]:


In [5]:
plotly()
t = π
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], ylims=[-3, 8])


Out[5]:

In [6]:
using Interact
gr()
@manipulate for θ = 0:(2*pi)/500:2*pi
    t = θ
    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=[-1.5, 4])
end


Out[6]:
-6 -4 -2 0 2 4 6 -6 -4 -2 0 2 4 6 0 500 1000 1500 2000 2500 3000 3500 4000 4500 y2 -5.0 -2.5 0.0 2.5 5.0 -1 0 1 2 3 4 y3

In [7]:
# What we'll do is observe that it has a local minimizer on any line thorugh
# the origin, but there is no minimizer there!
pyplot()
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[7]:

In [ ]:


In [35]:
## 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)


MethodError: Cannot `convert` an object of type Plots.Surface{Array{Float64,2}} to an object of type Array{Float64,1}
This may have arisen from a call to the constructor Array{Float64,1}(...),
since type constructors fall back to convert methods.

 in trisurface(::Plots.Surface{Array{Float64,2}}, ::Plots.Surface{Array{Float64,2}}, ::Array{Float64,1}) at /opt/julia_packages/.julia/v0.5/GR/src/GR.jl:3104
 in gr_display(::Plots.Subplot{Plots.GRBackend}, ::Measures.Length{:mm,Float64}, ::Measures.Length{:mm,Float64}, ::Array{Float64,1}) at /opt/julia_packages/.julia/v0.5/Plots/src/backends/gr.jl:834
 in gr_display(::Plots.Plot{Plots.GRBackend}) at /opt/julia_packages/.julia/v0.5/Plots/src/backends/gr.jl:494
 in _show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("image/svg+xml")}, ::Plots.Plot{Plots.GRBackend}) at /opt/julia_packages/.julia/v0.5/Plots/src/backends/gr.jl:1089
 in show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("image/svg+xml")}, ::Plots.Plot{Plots.GRBackend}) at /opt/julia_packages/.julia/v0.5/Plots/src/output.jl:197
 in show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::MIME{Symbol("text/html")}, ::Plots.Plot{Plots.GRBackend}) at /opt/julia_packages/.julia/v0.5/Plots/src/output.jl:177
 in show(::Base.AbstractIOBuffer{Array{UInt8,1}}, ::String, ::Plots.Plot{Plots.GRBackend}) at ./multimedia.jl:33
 in #sprint#304(::Void, ::Function, ::Int64, ::Function, ::String, ::Vararg{Any,N}) at ./strings/io.jl:37
 in display_dict(::Plots.Plot{Plots.GRBackend}) at /opt/julia_packages/.julia/v0.5/Plots/src/output.jl:266
 in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /opt/julia_packages/.julia/v0.5/IJulia/src/execute_request.jl:188
 in eventloop(::ZMQ.Socket) at /opt/julia_packages/.julia/v0.5/IJulia/src/eventloop.jl:8
 in (::IJulia.##13#19)() at ./task.jl:360

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])