% folium03.m
% fd, 23, 24, 25  novembre 2008 
% fd, 29 juin 2026
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
   clear , format long
   
printf("folium03 ; valeur optimale sur le folium de Descartes \n"); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 


hold off 
format long 

tt = [-.6 : .01 : 120] ; 
n = length(tt)  
x = zeros(n,1) ; y=x ; 

th = 0 ; 
for j = 1 : n ; 
x(j) = 3*tt(j) / (1+tt(j)**3) ; 
y(j) = tt(j) * x(j) ;  
% th = max ( th , abs ( x(j)**3 + y(j)**3 - 3*x(j)*y(j) ) ) ; 
end 


tt1 = [-80 : .01 : -1.5 ] ; 
n1 = length(tt1)  
x1 = zeros(n1,1) ; y1=x1 ; 

for j = 1 : n1 ; 
x1(j) = 3*tt1(j) / (1+tt1(j)**3) ; 
y1(j) = tt1(j) * x1(j) ;  
% th = max ( th , abs ( x1(j)**3 + y1(j)**3 - 3*x1(j)*y1(j) ) ) ;
end 

xpoint = 1. ; ypoint = 0. ; 
tt2 = [-pi : .01 : pi  ] ; 
n2 = length(tt2)  
x2 = zeros(n2,1) ; y2=x2 ; 
for j = 1 : n2 ; 
x2(j) = xpoint +  cos(tt2(j)) ;   y2(j) =  ypoint + sin(tt2(j)) ; end  


hold off
plot ( x , y , 'b;;' , x1,y1,  'b;folium;' , x2,y2,  'r;cercle;', xpoint,ypoint, 'go;centre;'  ) 
axis equal
title ('folium de descartes et distance a un point') ;
print('05-a-variation-folium-descartes', '-djpg','-S891,630')
  
hold off




pause (1) 


nnewton = 4
xalgo = zeros(nnewton,1) ; yalgo = zeros(nnewton,1) ; 

xx=zeros(2,1) ; 
 xx(1) = 1  ; xx(2)  = 0 ;  lambda = 0 ;
%   xx(1) = sqrt(2) ; xx(2)  =  sqrt(2)  ;  lambda = 0 ;
plot (xx(1) , xx(2)  , 'm*;;' ) 


omega = 1   

 for ll = 1 : nnewton
xalgo(ll) = xx(1) ; yalgo(ll) = xx(2) ; 
 gradla = fofo (xx(1) , xx(2) ,  lambda) ; 
 hh = hhfofo  (xx(1) , xx(2) ,  lambda) ;  
ddnn = - hh \ gradla ; 
for j = 1:2 
xx(j) = xx(j) + omega * ddnn(j) ; end 
lambda = lambda  + omega * ddnn(3) ; 
%% plot (xx(1) , xx(2)  , 'm*;;' ) 
end 


gradla ,  xx , lambda , ddnn , zzz = (xx(1)-1)**2 + xx(2)**2 


hold off
plot (x,y,'b;;' , x1,y1,'b;folium;',x2,y2,'r;cercle;',  xpoint,ypoint,'go;centre;',
      xalgo,yalgo,'*k;algo;', xx(1),xx(2),'om;solution;' ) 
axis equal
title ('folium de descartes et distance a un point') ;
print('05-b-variation-folium-descartes', '-djpg','-S891,630')


