Compte Rendu TP3 Optim
Compte Rendu TP3 Optim
Compte Rendu TP3 Optim
1. Fonctions objectifs :
Soit la fonction f(x,y)= x 𝑒 −𝑥 2−𝑦2 et g(x,y)= x²+y²+z²-xy-yz+3x-y-2z
x = linspace(-2,2,20);
y = x' ;
z = x .* exp(-x.^2 - y.^2);
surf(x,y,z)
Q1.2-
function [Y]=gradf(X)
% je décompose X en deux éléments
x=X(1);
y=X(2);
% j’initialise Y
Y=zeros(1,2);
% Je calcule la dérivée partielle de f par rapport à x
Y(1)=exp(-x^2-y^2)-2*x^2*exp(-x^2-y^2);
% Je calcule la dérivée partielle de f par rapport à y
Y(2)=-2*y*x*exp(-x^2-y^2);
% Ces mêmes résultats peuvent être calculés à partir de la fonction
prédéfinie sur Matlab
Syms pour Création de variables symboliques
syms x y
f=x*exp(-x^2-y^2);
YY=subs(gradient(f),[x,y],[X(1),X(2)]);
End
Dans ce cas :
function [Y,YY]=gradf(X)
x=X(1);
y=X(2);
Y=zeros(1,2);
YY=zeros(1,2);
Y(1)=exp(-x^2-y^2)-2*x^2*exp(-x^2-y^2);
Y(2)=-2*y*x*exp(-x^2-y^2);
syms x y
f=x*exp(-x^2-y^2);
YY=subs(gradient(f),[x,y],[X(1),X(2)]);
end
Appel en Main
X=[0.5 1.5];
[Y,YY]=gradf(X);
Q 1.3-
function [H]=hessf(X)
syms x y
f=x*exp(-x^2-y^2);
X=[0.5 1.5];
A=subs(hessian(f,[x,y]),[x,y],[X(1),X(2)]);
H=eval(A);
End
Main Program
X=[0.5 1.5];
[H]=hessf(x);
Solution :
H=
-0.2052 -0.1231
-0.1231 0.2873
Q1.4-
function [Y]=gradg(X)
syms x y z
g=x^2+y^2+z^2-x*y-y*z+3*x-y-2*z;
sol=subs(gradient(g),[x,y,z],[X(1),X(2),X(3)]);
Y=eval(sol)
end
Main program
Xg=[0.5 1.5 1];
[Yg]=gradg(Xg);
Résultat de simulation
Yg =
2.5000
0.5000
-1.5000
Main program
x0=[0.5;1.5];
e=10^-3;
p=0.5;
k=0;
[X,k]=gradfix(x0,e,p)
3. Algorithme de Newton:
function [X,k]=newton(x0,e)
X=x0;
k=0;
while norm(gradf(X))>e
X=X-hessf(X)\(eval(gradf(X)));
k=k+1;
eval(gradf(X));
end
End
Main Program
e=10^-12;
[X1,k1]=newton(X0,e);
Interprétation:
- Newton converge plus rapidement ( ici pour vérifier ceci il faut choisir
e=10^-12)
- Si p=1 Newton et gradient fixe donnent le même résultat