Skip to content
Snippets Groups Projects
Commit a988f1d8 authored by Olivier Cots's avatar Olivier Cots
Browse files

foo

parent 59d0506e
Branches
No related tags found
No related merge requests found
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
<center> <center>
<h1> TP-Projet d'optimisation numérique </h1> <h1> TP-Projet d'optimisation numérique </h1>
<h1> Algorithme de Newton </h1> <h1> Algorithme de Newton </h1>
</center> </center>
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Implémentation ## Implémentation
1. Coder l’algorithme de Newton local en respectant la spécification ci-dessous (fichier `Algorithme_De_Newton.jl`) 1. Coder l’algorithme de Newton local en respectant la spécification ci-dessous (fichier `Algorithme_De_Newton.jl`)
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
using LinearAlgebra using LinearAlgebra
using Documenter using Documenter
using Markdown using Markdown
include("Algorithme_De_Newton.jl") include("Algorithme_De_Newton.jl")
# @doc Algorithme_De_Newton @doc Algorithme_De_Newton
``` ```
%% Output
LoadError: syntax: invalid escape sequence
in expression starting at /Users/gergaud/git-ENS/optimisation-numerique/projet-optinum/src/Algorithme_De_Newton.jl:42
Stacktrace:
[1] top-level scope
@ ~/git-ENS/optimisation-numerique/projet-optinum/src/Algorithme_De_Newton.jl:42
[2] include(fname::String)
@ Base.MainInclude ./client.jl:444
[3] top-level scope
@ In[1]:4
[4] eval
@ ./boot.jl:360 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1116
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
2. Vérifier que les tests ci-dessous passent. 2. Vérifier que les tests ci-dessous passent.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
using Test using Test
# Tolérance pour les tests d'égalité # Tolérance pour les tests d'égalité
tol_erreur = sqrt(eps()) tol_erreur = sqrt(eps())
## ajouter les fonctions de test ## ajouter les fonctions de test
include("../test/fonctions_de_tests.jl") include("../test/fonctions_de_tests.jl")
include("../test/tester_algo_newton.jl") include("../test/tester_algo_newton.jl")
include("../src/Algorithme_De_Newton.jl") include("../src/Algorithme_De_Newton.jl")
affiche = false affiche = false # Mettre affiche = true pour afficher les résultats
# des appels à l'algorithme de Newton. Cela peut vous
# servir pour les réponses aux questions en fin de notebook.
@testset "Test algo newton" begin @testset "Test algo newton" begin
# Tester l'algorithme de Newton # Tester l'algorithme de Newton
tester_algo_newton(affiche,Algorithme_De_Newton) tester_algo_newton(affiche,Algorithme_De_Newton)
end; end;
``` ```
%% Output
LoadError: syntax: invalid escape sequence
in expression starting at /Users/gergaud/git-ENS/optimisation-numerique/projet-optinum/src/Algorithme_De_Newton.jl:42
Stacktrace:
[1] top-level scope
@ ~/git-ENS/optimisation-numerique/projet-optinum/src/Algorithme_De_Newton.jl:42
[2] include(fname::String)
@ Base.MainInclude ./client.jl:444
[3] top-level scope
@ In[2]:9
[4] eval
@ ./boot.jl:360 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1116
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` julia ``` julia
#using Pkg; Pkg.add("LinearAlgebra"); Pkg.add("Markdown") #using Pkg; Pkg.add("LinearAlgebra"); Pkg.add("Markdown")
# using Documenter # using Documenter
using LinearAlgebra using LinearAlgebra
using Markdown # Pour que les docstrings en début des fonctions ne posent using Markdown # Pour que les docstrings en début des fonctions ne posent
# pas de soucis. Ces docstrings sont utiles pour générer # pas de soucis. Ces docstrings sont utiles pour générer
# la documentation sous GitHub # la documentation sous GitHub
include("Algorithme_De_Newton.jl") include("Algorithme_De_Newton.jl")
# Affichage les sorties de l'algorithme des Régions de confiance # 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) function my_afficher_resultats(algo,nom_fct,point_init,xmin,fxmin,flag,sol_exacte,nbiters)
println("-------------------------------------------------------------------------") println("-------------------------------------------------------------------------")
printstyled("Résultats de : ",algo, " appliqué à ",nom_fct, " au point initial ", point_init, ":\n",bold=true,color=:blue) printstyled("Résultats de : ",algo, " appliqué à ",nom_fct, " au point initial ", point_init, ":\n",bold=true,color=:blue)
println(" * xsol = ",xmin) println(" * xsol = ",xmin)
println(" * f(xsol) = ",fxmin) println(" * f(xsol) = ",fxmin)
println(" * nb_iters = ",nbiters) println(" * nb_iters = ",nbiters)
println(" * flag = ",flag) println(" * flag = ",flag)
println(" * sol_exacte : ", sol_exacte) println(" * sol_exacte : ", sol_exacte)
end end
# Fonction f0 # Fonction f0
# ----------- # -----------
f0(x) = sin(x) f0(x) = sin(x)
# la gradient de la fonction f0 # la gradient de la fonction f0
grad_f0(x) = cos(x) grad_f0(x) = cos(x)
# la hessienne de la fonction f0 # la hessienne de la fonction f0
hess_f0(x) = -sin(x) hess_f0(x) = -sin(x)
sol_exacte = -pi/2 sol_exacte = -pi/2
options = [] options = []
x0 = sol_exacte x0 = sol_exacte
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options) 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) my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
x0 = -pi/2+0.5 x0 = -pi/2+0.5
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options) 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) my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
x0 = pi/2 x0 = pi/2
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options) 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) my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
``` ```
%% Output
LoadError: syntax: invalid escape sequence
in expression starting at /Users/gergaud/git-ENS/optimisation-numerique/projet-optinum/src/Algorithme_De_Newton.jl:42
Stacktrace:
[1] top-level scope
@ ~/git-ENS/optimisation-numerique/projet-optinum/src/Algorithme_De_Newton.jl:42
[2] include(fname::String)
@ Base.MainInclude ./client.jl:444
[3] top-level scope
@ In[3]:7
[4] eval
@ ./boot.jl:360 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1116
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Interprétation ## Interprétation
1. Justifier les résultats obtenus pour l'exemple $f_0$ ci-dessus; 1. Justifier les résultats obtenus pour l'exemple $f_0$ ci-dessus;
2. Soit 2. Soit
$$ f_{1} : \mathbb{R}^3 \rightarrow \mathbb{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$$ $$ f_{1} : \mathbb{R}^3 \rightarrow \mathbb{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}$; Justifier que l’algorithme implémenté converge en une itération pour $f_{1}$;
3. Soit 3. Soit
$$ f_{2} : \mathbb{R}^2 \rightarrow \mathbb{R}$$ $$ (x_1,x_2) \mapsto 100(x_2-x_1^2)^2 + (1-x_1)^2 $$ $$ f_{2} : \mathbb{R}^2 \rightarrow \mathbb{R}$$ $$ (x_1,x_2) \mapsto 100(x_2-x_1^2)^2 + (1-x_1)^2 $$
Justifier que l’algorithme puisse ne pas converger pour $f_{2}$ avec certains points initiaux. Justifier que l’algorithme puisse ne pas converger pour $f_{2}$ avec certains points initiaux.
**Remarque.** Vous pouvez mettre `affiche=true` dans les tests de l'algorithme de Newton pour
%% Cell type:code id: tags: vous aider.
``` julia
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment