1.function[x,val,k]=grad(fun,gfun,x0)maxk=5000;rho=0.5;sigma=0.4;k=0;eps=0.01;while(kmaxk)g=feval(gfun,x0);d=-g;if(norm(d)eps),break;endm=0;mk=0;while(m20)if(feval(fun,x0+rho^m*d)feval(fun,x0)+sigma*rho^m*g'*d)mk=m;break;endm=m+1;endx0=x0+rho^mk*d;k=k+1;endx=x0;val=feval(fun,x);functionf=fun(x)f=x(1)^2+25*x(2)^2;endfunctiong=gfun(x)g=[2*x(1),50*x(2)]';endx0=[2,2]';[x,val,k]=grad(@fun,@gfun,x0)x=0.00420.0001val=1.7672e-05k=692function[x,val,k]=dampnm(fun,gfun,Hess,x0)maxk=100;rho=0.5;sigma=0.4;k=0;eps=0.01;while(kmaxk)gk=feval(gfun,x0);Gk=feval(Hess,x0);dk=-Gk\gk;if(norm(gk)eps),break;endm=0;mk=0;while(m20)if(feval(fun,x0+rho^m*dk)feval(fun,x0)+sigma*rho^m*gk'*dk)mk=m;break;endm=m+1;endx0=x0+rho^mk*dk;k=k+1;endx=x0;val=feval(fun,x);functionf=fun(x)f=60-10*x(1)-4*x(2)+x(1)^2+x(2)^2-x(1)*x(2);endfunctiong=gfun(x)g=[-10+2*x(1)-x(2),-4+2*x(2)-x(1)]';endfunctionHe=Hess(x)n=length(x);He=zeros(n,n);He=[2,-1;-1,2];end[x,val,k]=dampnm(@fun,@gfun,@Hess,x0)x=86val=8k=13.function[x,val,k]=revisenm(fun,gfun,Hess,x0)n=length(x0);maxk=150;rho=0.55;sigma=0.4;tau=0.0;k=0;epsilon=0.01;while(kmaxk)gk=feval(gfun,x0);muk=norm(gk)^(1+tau);Gk=feval(Hess,x0);Ak=Gk+muk*eye(n);dk=-Ak\gk;if(norm(gk)epsilon),break;endm=0;mk=0;while(m20)if(feval(fun,x0+rho^m*dk)feval(fun,x0)+sigma*rho^m*gk'*dk)mk=m;break;endm=m+1;endx0=x0+rho^mk*dk;k=k+1;endx=x0;val=feval(fun,x);functionf=fun(x)f=4*(x(1)+1)^2+2*(x(2)-1)^2+x(1)+x(2)+10;endfunctionHe=Hess(x)n=length(x);He=zeros(n,n);He=[8,0;0,4];endfunctiong=gfun(x)g=[8*(x(1)+1)+1,4*(x(2)-1)+1]';endx0=[0;0];[x,val,k]=revisenm(@fun,@gfun,@Hess,x0)x=-1.12500.7498val=9.8125k=55.function[x,val,k]=frcg(fun,gfun,x0)maxk=100;rho=0.6;sigma=0.4;k=0;eps=0.01;n=length(x0);while(kmaxk)g=feval(gfun,x0);itern=k-(n+1)*floor(k/(n+1));itern=itern+1;if(itern==1)d=-g;elsebeta=(g'*g)/(g0'*g0);d=-g+beta*d0;gd=g'*d;if(gd=0.0)d=-g;endendif(norm(g)eps),break;endm=0;mk=0;while(m20)if(feval(fun,x0+rho^m*d)feval(fun,x0)+sigma*rho^m*gk'*d)mk=m;break;endm=m+1;endx0=x0+rho^mk*d;val=feval(fun,x0);g0=g;d0=d;k=k+1;endx=x0;val=feval(fun,x);functionf=fun(x)f=2*x(1)^2+x(2)^2-x(1)*x(2);endfunctiong=gfun(x)g=[2*x(1)-x(2),2*x(2)-x(1)]';endx0=[0,0]';[x,val,k]=frcg(@fun,@gfun,x0)x=00val=0k=06.function[x,val,k]=dfp(fun,gfun,x0)n=length(x0);maxk=1e5;rho=0.55;sigma=0.4;k=0;epsilon=0.01;Hk=inv(feval(@Hess,x0));while(kmaxk)gk=feval(gfun,x0);if(norm(gk)epsilon),break;enddk=-Hk*gk;m=0;mk=0;while(m20)if(feval(fun,x0+rho^m*dk)feval(fun,x0)+sigma*rho^m*gk'*dk)mk=m;break;endm=m+1;endx=x0+rho^mk*dk;sk=x-x0;yk=feval(gfun,x)-gk;if(sk'*yk0)Hk=Hk-(Hk*yk*yk'*Hk)/(yk'*Hk*yk)+(sk'*yk);endk=k+1;x0=x;endval=feval(fun,x0);functionf=fun(x)f=4*(x(1)-5)^2+(x(2)-6)^2;endfunctiong=gfun(x)g=[8*(x(1)-5),2*(x(2)-6)]';end[x,val,k]=dfp(@fun,@gfun,x0)x=4.99746.0432val=0.0019k=100000