Skip to content
Snippets Groups Projects
Commit 2cfefa0e authored by J. Gergaud's avatar J. Gergaud
Browse files

foo

parent 0a9aa66a
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
## Automatic differentiation
%% Cell type:code id: tags:
```
using LinearAlgebra
using ForwardDiff
A = [-1 2;1 4]; b=[1,1]; c=1
f(x) = 0.5*x'*A*x + b'*x +c
analytic_∇f(x) = 0.5*(A' + A)*x+b
analytic_∇²f(x) = 0.5*(A' + A)
∇f(x) = ForwardDiff.gradient(f, x)
∇²f(x) = ForwardDiff.hessian(f, x)
x0 = [1,-1]
println("∇f(x0) = ", ∇f(x0))
println("analytic_∇f(x0) = ", analytic_∇f(x0))
println("analytic_∇f(x0) - ∇f(x0) = ", analytic_∇f(x0)- ∇f(x0))
println("∇²f(x0) = ", ∇²f(x0))
println("analytic_∇²f(x0) = ", analytic_∇²f(x0))
println("analytic_∇²f(x0) - ∇²f(x0) = ", analytic_∇²f(x0)- ∇²f(x0))
```
%% Cell type:markdown id: tags:
## Newton algorithm
%% Cell type:code id: tags:
```
using LinearAlgebra
using ForwardDiff
"""
Solve by Newton's algorithm the optimization problem Min f(x)
Case where f is a function from R^n to R
"""
function algo_Newton(f,x0::Vector{<:Real};AbsTol= abs(eps()), RelTol = abs(eps()), ε=0.01, nbit_max = 0)
# to complete
# flag = 0 if the program stop on the first criteria
# flag = 2 if the program stop on the second criteria
# ...
return xₖ, flag, fₖ, ∇fₖ , k
end
```
%% Cell type:code id: tags:
```
A = [1 0 ; 0 9/2]
b = [0,0]; c=0.
f1(x) = 0.5*x'*A*x + b'*x +c
x0 = [1000,-20]
println("Results for Newton on f1 : ", algo_Newton(f1,x0))
println("eigen value of 0.5(A^T+A) = ", 0.5*eigen(A'+A).values)
using Plots;
x=range(-10,stop=10,length=100)
y=range(-10,stop=10,length=100)
f11(x,y) = f1([x,y])
p1 = plot(x,y,f11,st=:contourf)
A = [-1 0 ; 0 3]
f2(x) = 0.5*x'*A*x + b'*x +c
println("Results for Newton on f2 : ", algo_Newton(f2,x0))
println("eigen value of 0.5(A^T+A) = ", 0.5*eigen(A'+A).values)
f21(x,y) = f1([x,y])
p2 = plot(x,y,f21,st=:contourf)
# Rosenbrock function
x=range(-1.5,stop=2,length=100)
y=range(-0.5,stop=3.,length=100)
f3(x) = (1-x[1])^2 + 100*(x[2]-x[1]^2)^2
f31(x,y) = f3([x,y])
p3 = plot(x,y,f31,st=:contourf)
x0 = [1.1,1.2]
println("Results for Newton on f3 : ", algo_Newton(f3,[1.1,1.2]))
println("Results for Newton on f3 : ", algo_Newton(f3,[3.,0.]))
plot(p1,p2,p3)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment