using Plots, LinearAlgebra, Random, StatsBase
using Interact
gr()
The WebIO Jupyter extension was not detected. See the WebIO Jupyter integration documentation for more information.
Plots.GRBackend()
# 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 = repeat(x', length(y), 1)
Y = repeat(y, 1, length(x))
# Evaluate each f(x, y)
Z = map(f, X, Y)
surface(X, Y, Z)
#zlims!(0,50.0)
# Show the plot in log-space
gr()
plot(X, Y, log.(Z.-minimum(Z).+10000*sqrt(eps(1.0))),
seriestype=:surface, clims=(-5,5))
#t = 6*π
t = 5.0
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, log.(Z.-minimum(Z).+10000*sqrt(eps(1.0))),
st=:contour, layout=l)
plot!(p[1], zt*cos(t), zt*sin(t), line=(:red, 2), label="")
z = -5:0.01:5
zf = map(ftheta, z)
plot!(p[2], z, zf, xaxis=[-5, 5], ylims=[-3, 8], label="")
gr()
@manipulate for θ = 0.0:(2*pi)/500:2.0*pi
t = θ
zt = -5:5
ftheta = z -> f(z*cos(t), z*sin(t))
z = -5.0:0.01:5.0
zf = map(ftheta, z)
plot(z, zf)
end
pyplot()
@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, log.(Z.-minimum(Z).+10000*sqrt(eps(1.0))), 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=[-0.5, 1.0])
p
end
sys:1: UserWarning: The following kwargs were not used by contour: 'label'
# What we'll do is observe that it has a local minimizer on any line thorugh
# the origin, but there is no minimizer there!
gr()
thetas = 0:0.01:2*pi
anim = @animate for ti = 1:length(thetas)
# ti = 27 # enable this to get stepping behavior
t = thetas[ti]
zt = -5.0:5.0
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)
## Let's demonstrate the optimality conditions.
# Create a meshgrid
x = -6:0.1:6
y = -6:0.1:6
X = repeat(x', length(y), 1)
Y = repeat(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,
legend=false)
## Let's zoom in
# Create a meshgrid
x = 0.975:0.0005:1.025
y = 0.975:0.0005:1.025
X = repeat(x', length(y), 1)
Y = repeat(y, 1, length(x))
H = map(h, X, Y)
contour(x, y, H)
# More zoom
x = 0.99975:0.000005:1.00025
y = 0.99975:0.000005:1.00025
X = repeat(x', length(y), 1)
Y = repeat(y, 1, length(x))
H = map(h, X, Y)
contour(x, y, H)
# Create a meshgrid
x = 0.975:0.0005:1.025
y = 0.975:0.0005:1.025
X = repeat(x', length(y), 1)
Y = repeat(y, 1, length(x))
H = map(h, X, Y)
contour(x, y, H)
## Let's zoom in further
x = 0.99975:0.000005:1.00025
y = 0.99975:0.000005:1.00025
X = repeat(x', length(y), 1)
Y = repeat(y, 1, length(x))
H = map(h, X, Y)
contour(x, y, H)
## Let's zoom in further
x = 0.9999975:0.00000005:1.0000025
y = 0.9999975:0.00000005:1.0000025
X = repeat(x', length(y), 1)
Y = repeat(y, 1, length(x))
H = map(h, X, Y)
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],legend=false)