## Lecture 7, look at quadratic.
A = [3 -1.0; -1.0 4]
b = [-1, 2.0]
xsoln = A\b
##
# 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))
##
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()
##
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")
##
##
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