In [1]:
## Lecture 7, look at quadratic.
A = [3 -1.0; -1.0 4]
b = [-1, 2.0]
xsoln = A\b
Out[1]:
2-element Array{Float64,1}:
 -0.1818181818181818
  0.4545454545454546
In [2]:
##
# A quadratic function is
# f(x) = 1/2 x'*A*x - x'*b
# We will show, miniminzing f(x) <=> solving Ax = b when A is symm. pos. def.
# sym. pos. def <=> x'*A*x > 0
quadratic(A,b,x) = 0.5*x'*A*x - x'*b
@show quadratic(A,b,xsoln)
@show quadratic(A,b,randn(2))
quadratic(A, b, xsoln) = -0.5454545454545455
quadratic(A, b, randn(2)) = -0.09484014849677008
Out[2]:
-0.09484014849677008
In [3]:
##
using Plots
pyplot()
x = y = range(-1, stop = 1, length = 25)
plot(x,y,(x,y) -> quadratic(A,b,[x,y]), st=:surface,camera=(80,30))
scatter!([xsoln[1]],[xsoln[2]], [quadratic(A, b, xsoln)], markersize = 12)
gui()
In [4]:
##
using Plots
pyplot()

x = y = range(-1, stop = 1, length = 25)
x0 = randn(2)
p = plot(x,y,(x,y) -> quadratic(A,b,[x,y]), camera=(150,30),st=:surface, xlim=(-1,1), ylim=(-1,1), zlim=(0,5))
plot!([x0[1]], [x0[2]], [quadratic(A,b,x0)])

gamma = 0.25
x = copy(x0)
anim = @animate for i=1:100
  x .+= -gamma*(A*x - b)
  push!(p[1][2], x[1],x[2], quadratic(A,b,x))
end
gif(anim, "quadratic-1.gif")
┌ Info: Saved animation to 
│   fn = /Users/dgleich/Dropbox/courses/cs515-2019/web/input/julia/quadratic-1.gif
â”” @ Plots /Users/dgleich/.julia/packages/Plots/h3o4c/src/animation.jl:95
Out[4]:
In [5]:
##
##
using Plots
pyplot()

x = y = range(-1, stop = 1, length = 25)
x0 = randn(2)
p = plot(x,y,(x,y) -> quadratic(A,b,[x,y]), st=:surface,camera=(150,30), xlim=(-1,1), ylim=(-1,1), zlim=(0,5))
plot!([x0[1]], [x0[2]], [quadratic(A,b,x0)])

α = 0.1
x = copy(x0)
for i=1:15
  x .+= -α*(A*x - b)
  push!(p[1][2], x[1],x[2], quadratic(A,b,x))
  gui()
  sleep(0.25)
end