% opti3.m
% fd, 14  janvier 2006, 02, 03 mars 2025
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% xlabel (' opti3. Optimisation a deux variables sans contrainte ; \
%%% algorithme du gradient a pas fixe  ') ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear ; format long

'Rosenbrock = (x-1)**2 + 10*(x**2-y)**2'
  
  rho=.02
axis ( [ -1.15 , 1.1 , -.5 , 1.5 ]) ;
x0 = 0. ; y0 = 0. ;
x0 = -1. ; y0 = 1. ;
x0 = 0. ; y0 = 0. ;
x0 = -1. ; y0 = 1. ;

x0 = -.9 ; y0 = 1.2 ;

xx(1:1,1)=x0 ; yy(1:1,1)=y0 ;

for lll = 1:5
if lll==1 ; niter = 10 ; end 
if lll==2 ; niter = 50 ; end 
if lll==3 ; niter = 100 ; end 
if lll==4 ; niter = 500 ; end 
if lll==5 ; niter = 1000 ; end 
if lll==6 ; niter = 5000 ; end 
   xplot = zeros(niter+1,1) ;  yplot = zeros(niter+1,1) ;
xplot(1,1) = x0 ; yplot(1,1) = y0 ;

x=x0 ; y=y0 ; 
for j=1:niter ;

frosen = rosen(x,y)(1:1,1) ; 
gx = rosen(x,y)(2:2,1) ;  
gy = rosen(x,y)(3:3,1) ; 
  
x1=x-rho*gx ;   y1=y-rho*gy ;
xplot(j+1,1) = x1 ; yplot(j+1,1) = y1 ;
x = x1 ; y = y1 ; 
end ; 

hold off 
plot (xplot(1:1,1) , yplot(1:1,1) ,'ob;initialisation;') ; hold on 
if lll==1 ; 
plot ( xplot(:,1) , yplot(:,1) ,'*-k;10 iterations;',  xplot(:,1) , yplot(:,1) ,'k;;') ; end
if lll==2 ; 
plot ( xplot(:,1) , yplot(:,1) ,'*-k;50 iterations;',  xplot(:,1) , yplot(:,1) ,'k;;') ; end
if lll==3 ; 
plot ( xplot(:,1) , yplot(:,1) ,'*-k;100 iterations;',  xplot(:,1) , yplot(:,1) ,'k;;') ; end
if lll==4 ; 
plot ( xplot(:,1) , yplot(:,1) ,'*-k;500 iterations;',  xplot(:,1) , yplot(:,1) ,'k;;') ; end
if lll==5 ; 
plot ( xplot(:,1) , yplot(:,1) ,'*-k;1000 iterations;',  xplot(:,1) , yplot(:,1) ,'k;;') ; end
if lll==6 ; 
plot ( xplot(:,1) , yplot(:,1) ,'*-k;5000 iterations;',  xplot(:,1) , yplot(:,1) ,'k;;') ; end
plot  (1 , 1 ,'*r;minimum de la fonction de Rosenbrock;') ;
axis ( [ -1.2 ,     1.2  , -.05   , 1.31 ])
title ('minimisation de la fonction de Rosenbrock ; gradient a pas fixe') ;
if lll==1 ; print('03a-rosenbrock-gradient-a-pas-fixe', '-djpg','-S891,630'); end 
if lll==2 ; print('03b-rosenbrock-gradient-a-pas-fixe', '-djpg','-S891,630'); end
if lll==3 ; print('03c-rosenbrock-gradient-a-pas-fixe', '-djpg','-S891,630'); end
if lll==4 ; print('03d-rosenbrock-gradient-a-pas-fixe', '-djpg','-S891,630'); end
if lll==5 ; print('03e-rosenbrock-gradient-a-pas-fixe', '-djpg','-S891,630'); end
if lll==6 ; print('03f-rosenbrock-gradient-a-pas-fixe', '-djpg','-S891,630'); end
hold  off ; pause(.3) 
end

