//
//Descent method 
//
function y=f(x)
  y=0.1*x(1)^2+x(2)^2;
endfunction 
function y=nablaf(x)
  y=[0.2*x(1);2*x(2)];
endfunction
x=rand(2,1);
betaa=0.01;
alphainit=10;
tau=0.5;
epsilon=1E-6;   // stopping criterion
/////////////////
function d=descent(x,f,nablaf)
d=-nablaf(x);
endfunction
////////////////
function alpha=backtrack(x,f,g,d,betaa,alphainit,tau)
  alpha=alphainit;countmax=10000;count=0;
  while (f(x+alpha*d)>f(x)+betaa*alpha*(d'*g(x)))&(count<countmax)
   alpha=tau*alpha;count=count+1;   
  end
endfunction
////////////////
niter=0;Xtot=[];
while(norm(nablaf(x))>epsilon)
  d=descent(x,f,nablaf);
  niter=niter+1;
  alpha=backtrack(x,f,nablaf,d,betaa,alphainit,tau)    
  x=x+alpha*d; 
  Xtot=[Xtot,x];
end  
disp('approximation of the minima:');disp(x);
disp('iteration number');disp(niter);
//
t=linspace(-1,1,30);
function z=my_surface(x,y),z=0.1*x^2+y^2,endfunction
clf
contour(t,t,my_surface,10) 
plot2d(Xtot(1,:),Xtot(2,:))



