% mininewton03.m   
% fd, 30 juin 2026
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
clear , format long
   
printf("mininewton03 ; minimisation d une fonction quadratique \n"); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

ff  = inline(".5*yy**2 - .5*xx**2  - .5*yy*xx**2 + xx**3/3. + .25*(xx**4 + yy**4)") ; 
dxf = inline("-xx -xx*yy + xx**2 + xx**3") ; 
dyf = inline("yy - .5*xx**2 + yy**3") ; 
   
printf("\n"); 

printf("isovaleurs de la fonction ff = .5*yy**2 - .5*xx**2  - .5*yy*xx**2 + xx**3/3. + .25*(xx**4 + yy**4) \n"); 


xmin = -3;  xmax = 3. ; dx= .1 ; 
xxx = xmin:dx:xmax ;
nxxx = length(xxx)
yyy = xmin:dx:xmax ;
zzz = zeros(nxxx,nxxx) ;  
nvaleurs = 40 ; 

for i=1:nxxx
for j=1:nxxx
	zzz(i,j) = ff(xxx(i),yyy(j)) ; end ; end;
contour(xxx,yyy,zzz,nvaleurs) 
title ('isovaleurs de la fonction non quadratique') ;
pause (2) 

hold on ;

print('03a-minimisation-gradient', '-djpg','-S891,630'); 
	
hold on 

niter = 50
xx = -3. ; yy = .5 ;
xiter = zeros(niter+1,1) ; 
yiter = zeros(niter+1,1) ; 

rho = .06

for j=1:niter
xiter(j) = xx ;
yiter(j) = yy ;

valeur = ff(xx,yy) ; 
gx = dxf(xx,yy) ; gy = dyf(xx,yy) ;

xx = xx - rho*gx ;
yy = yy - rho*gy ;

end 	
xiter(niter+1) = xx ;	yiter(niter+1) = yy ;    


title ('algorithme de gradient pour une fonction non quadratique') ;
plot (xiter, yiter, '*r;;' , xiter, yiter, 'r;;' ) 
axis ( [ xmin,xmax, xmin,xmax ])  
print('03b-minimisation-gradient', '-djpg','-S891,630'); 
pause (.5) ; hold on 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
xx = 3. ; yy = .0 ;
xiter = zeros(niter+1,1) ; 
yiter = zeros(niter+1,1) ; 

rho = .06

for j=1:niter
xiter(j) = xx ;
yiter(j) = yy ;

valeur = ff(xx,yy) ; 
gx = dxf(xx,yy) ; gy = dyf(xx,yy) ;

xx = xx - rho*gx ;
yy = yy - rho*gy ;

end 	
xiter(niter+1) = xx ;	yiter(niter+1) = yy ;    


title ('algorithme de gradient pour une fonction non quadratique') ;
plot (xiter, yiter, '*r;;' , xiter, yiter, 'r;;' ) 
print('03c-minimisation-gradient', '-djpg','-S891,630'); 
hold off




