Skip to content
Snippets Groups Projects

Feature/refonte notebook

Merged Olivier Cots requested to merge feature/refonte-notebook into feature/refonte
1 file
+ 0
3
Compare changes
  • Side-by-side
  • Inline
%% Cell type:markdown id: tags:
<center>
<h1> TP-Projet d'optimisation numérique </h1>
<h1> Algorithme de Newton </h1>
</center>
%% Cell type:markdown id: tags:
## Implémentation
1. Coder l’algorithme de Newton local en respectant la spécification ci-dessous (fichier `Algorithme_De_Newton.jl`)
%% Cell type:code id: tags:
``` julia
using LinearAlgebra
using Documenter
using Markdown
include("Algorithme_De_Newton.jl")
@doc Algorithme_De_Newton
```
%% Output
┌ Info: Precompiling Documenter [e30172f5-a6a5-5a46-863b-614d45cd2de4]
└ @ Base loading.jl:1342
# Objet
Cette fonction implémente l'algorithme de Newton pour résoudre un problème d'optimisation sans contrainte
# Syntaxe
```julia
xk,f_min,flag,nb_iters = Algorithme_de_Newton(f,gradf,hessf,x0,option)
```
# Entrées :
* f : (Function) la fonction à minimiser
* gradf : (Function) le gradient de la fonction f
* hessf : (Function) la Hessienne de la fonction f
* x0 : (Array{Float,1}) première approximation de la solution cherchée
* options : (Array{Float,1})
* max_iter : le nombre maximal d'iterations
* Tol_abs : la tolérence absolue
* Tol_rel : la tolérence relative
# Sorties:
* xmin : (Array{Float,1}) une approximation de la solution du problème : $\min_{x \in \mathbb{R}^{n}} f(x)$
* f*min : (Float) ``f(x*{min})``
* flag : (Integer) indique le critère sur lequel le programme à arrêter
* 0 : Convergence
* 1 : stagnation du xk
* 2 : stagnation du f
* 3 : nombre maximal d'itération dépassé
* nb_iters : (Integer) le nombre d'itérations faites par le programme
# Exemple d'appel
```@example
f(x)=100*(x[2]-x[1]^2)^2+(1-x[1])^2
gradf(x)=[-400*x[1]*(x[2]-x[1]^2)-2*(1-x[1]) ; 200*(x[2]-x[1]^2)]
hessf(x)=[-400*(x[2]-3*x[1]^2)+2 -400*x[1];-400*x[1] 200]
x0 = [1; 0]
options = []
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f,gradf,hessf,x0,options)
```
\section{Objet}
Cette fonction implémente l'algorithme de Newton pour résoudre un problème d'optimisation sans contrainte
\section{Syntaxe}
\begin{verbatim}
xk,f_min,flag,nb_iters = Algorithme_de_Newton(f,gradf,hessf,x0,option)
\end{verbatim}
\section{Entrées :}
\begin{itemize}
\item f : (Function) la fonction à minimiser
\item gradf : (Function) le gradient de la fonction f
\item hessf : (Function) la Hessienne de la fonction f
\item x0 : (Array\{Float,1\}) première approximation de la solution cherchée
\item options : (Array\{Float,1\})
\begin{itemize}
\item max\_iter : le nombre maximal d'iterations
\item Tol\_abs : la tolérence absolue
\item Tol\_rel : la tolérence relative
\end{itemize}
\end{itemize}
\section{Sorties:}
\begin{itemize}
\item xmin : (Array\{Float,1\}) une approximation de la solution du problème : $\min_{x \in \mathbb{R}^{n}} f(x)$
\item f\emph{min : (Float) ``f(x}\{min\})``
\item flag : (Integer) indique le critère sur lequel le programme à arrêter
\begin{itemize}
\item 0 : Convergence
\item 1 : stagnation du xk
\item 2 : stagnation du f
\item 3 : nombre maximal d'itération dépassé
\end{itemize}
\item nb\_iters : (Integer) le nombre d'itérations faites par le programme
\end{itemize}
\section{Exemple d'appel}
\begin{verbatim}
f(x)=100*(x[2]-x[1]^2)^2+(1-x[1])^2
gradf(x)=[-400*x[1]*(x[2]-x[1]^2)-2*(1-x[1]) ; 200*(x[2]-x[1]^2)]
hessf(x)=[-400*(x[2]-3*x[1]^2)+2 -400*x[1];-400*x[1] 200]
x0 = [1; 0]
options = []
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f,gradf,hessf,x0,options)
\end{verbatim}
 Objet
 ≡≡≡≡≡≡≡
Cette fonction implémente l'algorithme de Newton pour résoudre un problème
d'optimisation sans contrainte
 Syntaxe
 ≡≡≡≡≡≡≡≡≡
 xk,f_min,flag,nb_iters = Algorithme_de_Newton(f,gradf,hessf,x0,option)
 Entrées :
 ≡≡≡≡≡≡≡≡≡≡≡
• f : (Function) la fonction à minimiser
• gradf : (Function) le gradient de la fonction f
• hessf : (Function) la Hessienne de la fonction f
• x0 : (Array{Float,1}) première approximation de la solution
cherchée
• options : (Array{Float,1})
• max_iter : le nombre maximal d'iterations
• Tol_abs : la tolérence absolue
• Tol_rel : la tolérence relative
 Sorties:
 ≡≡≡≡≡≡≡≡≡≡
• xmin : (Array{Float,1}) une approximation de la solution du
problème : \min_{x \in \mathbb{R}^{n}} f(x)
• fmin : (Float) ``f(x{min})``
• flag : (Integer) indique le critère sur lequel le programme à
arrêter
• 0 : Convergence
• 1 : stagnation du xk
• 2 : stagnation du f
• 3 : nombre maximal d'itération dépassé
• nb_iters : (Integer) le nombre d'itérations faites par le
programme
 Exemple d'appel
 ≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡≡
 f(x)=100*(x[2]-x[1]^2)^2+(1-x[1])^2
 gradf(x)=[-400*x[1]*(x[2]-x[1]^2)-2*(1-x[1]) ; 200*(x[2]-x[1]^2)]
 hessf(x)=[-400*(x[2]-3*x[1]^2)+2 -400*x[1];-400*x[1] 200]
 x0 = [1; 0]
 options = []
 xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f,gradf,hessf,x0,options)
%% Cell type:markdown id: tags:
2. Vérifier que les tests ci-dessous passent.
%% Cell type:code id: tags:
``` julia
using Test
# Tolérance pour les tests d'égalité
tol_erreur = sqrt(eps())
## ajouter les fonctions de test
include("../test/fonctions_de_tests.jl")
include("../test/tester_algo_newton.jl")
include("../src/Algorithme_De_Newton.jl")
affiche = false
@testset "Test algo newton" begin
# Tester l'algorithme de Newton
tester_algo_newton(affiche,Algorithme_De_Newton)
end;
```
%% Output
Test Summary: | Pass Total
Test algo newton |  14  14
%% Cell type:code id: tags:
``` julia
#using Pkg; Pkg.add("LinearAlgebra"); Pkg.add("Markdown")
# using Documenter
using LinearAlgebra
using Markdown # Pour que les docstrings en début des fonctions ne posent
# pas de soucis. Ces docstrings sont utiles pour générer
# la documentation sous GitHub
include("Algorithme_De_Newton.jl")
# Affichage les sorties de l'algorithme des Régions de confiance
function my_afficher_resultats(algo,nom_fct,point_init,xmin,fxmin,flag,sol_exacte,nbiters)
println("-------------------------------------------------------------------------")
printstyled("Résultats de : ",algo, " appliqué à ",nom_fct, " au point initial ", point_init, ":\n",bold=true,color=:blue)
println(" * xsol = ",xmin)
println(" * f(xsol) = ",fxmin)
println(" * nb_iters = ",nbiters)
println(" * flag = ",flag)
println(" * sol_exacte : ", sol_exacte)
end
# Fonction f0
# -----------
f0(x) = sin(x)
# la gradient de la fonction f0
grad_f0(x) = cos(x)
# la hessienne de la fonction f0
hess_f0(x) = -sin(x)
sol_exacte = -pi/2
options = []
x0 = sol_exacte
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options)
my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
x0 = -pi/2+0.5
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options)
my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
x0 = pi/2
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options)
my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
```
%% Output
-------------------------------------------------------------------------
Résultats de : Newton appliqué à f0 au point initial -1.5707963267948966:
* xsol = -1.5707963267948966
* f(xsol) = -1.0
* nb_iters = 0
* flag = 0
* sol_exacte : -1.5707963267948966
-------------------------------------------------------------------------
Résultats de : Newton appliqué à f0 au point initial -1.0707963267948966:
* xsol = -1.0707963267948966
* f(xsol) = -0.8775825618903726
* nb_iters = 0
* flag = 0
* sol_exacte : -1.5707963267948966
-------------------------------------------------------------------------
Résultats de : Newton appliqué à f0 au point initial 1.5707963267948966:
* xsol = 1.5707963267948966
* f(xsol) = 1.0
* nb_iters = 0
* flag = 0
* sol_exacte : -1.5707963267948966
%% Cell type:markdown id: tags:
## Interprétation
1. Justifier les résultats obtenus pour l'exemple $f_0$ ci-dessus;
2. Soit $$ f_{1} : \mathbf{R}^3 \rightarrow \mathbf{R}$$ $$ (x_1,x_2, x_3) \mapsto 2 (x_1 +x_2 + x_3 -3)^2 + (x_1-x_2)^2 + (x_2 - x_3)^2$$ Justifier que l’algorithme implémenté converge en une itération pour $f_{1}$;
3. Soit $$ f_{2} : \mathbf{R}^2 \rightarrow \mathbf{R}$$ $$ (x_1,x_2) \mapsto 100(x_2-x_1^2)^2 + (1-x_1)^2 $$ que l’algorithme puisse ne pas converger pour $f_{2}$ avec certains points initiaux.
Loading