function gradient2d() %%%%%%%%%%%%%% close all x(1)=-1.2 x(2)=1.0 t=0.0001 kmax=1000 for i=1:kmax i if (norm(g(x))<1e-5) break end d=-g(x) t=RL(x,d) x=x+t*d abs_grad_f=norm(g(x)) f_val=f(x) y(i,:)=x; end %%%%%%%%%%%%%%%%% %%% Partie Plot %%%%%%%%%%%%%%%%% x1 = -1.5:0.1:1.5; x2= -1.5:0.1:1.5; [x1,x2] = meshgrid(x1,x2); Z = 100*(x2-x1.^2).^2 + (1-x2).^2; contourf(x1,x2,Z,30); hold on plot(y(:,1),y(:,2),'w-o') colormap jet fontsz=16; xlabel('$x$','Interpreter','latex','FontSize',fontsz) ylabel('$y$','Interpreter','latex','FontSize',fontsz) title({'Acheminement des iter\''ees de la m\''ethode du gradient'},'Interpreter','latex') %%%%%%%%%%%%%%%%% function d=f(x); % d = (x(1)-1).^2+(x(2)-1).^2; d=100*(x(2)-x(1).^2).^2 + (1-x(1)).^2; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function d=g(x); %d(1) = 2*(x(1)-1);%d(2) = 2*(x(2)-1); d(1)=-(400*(x(2)-x(1).^2)).*x(1)-2+2*x(1); d(2)=200*x(2)-200*x(1).^2; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y=theta(t,x,d); y=f(x+t*d); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function y=thetap(t,x,d); y=g(x+t*d)*d'; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%Recherche lineaire %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function tk=RL(x,d); kmax=150; tg=0.0; td=1e-3; beta=10; %%%%Chercher le premier pas td for i=1:100; if (thetap(td,x,d)>=0) break else td=beta*td; end end %%%%%%%% for i=1:kmax; tk=(tg+td)/2; if (abs(thetap(tk,x,d))<1e-5) break elseif (thetap(tk,x,d)<0) tg=tk; else td=tk; end end %%%%%%%%%%%%%%%%%%%%%%%%%%%