%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% function gd2(x0) - minimizes f(x)=cos(x)*cos(y) %%% %%% INPUT: x0 = 2 by 1 column vector for starting point s.t. -41e-9) %% count the iterations i=i+1; %% current point x=xnew; %%find the optimal step parameter tempf=@(lambda)cos(xnew(1)-lambda*gradnew(1))*cos(xnew(2)-lambda*gradnew(2)); tstep=fminbnd(tempf, -4, 4); %%compute the new point using the current one and the multiple of the %%gradient vector xnew=x-tstep*gradnew'; %%compute the gradient of f at the new point gradnew=gradient(xnew(1),xnew(2)); %%graph the new point and the line path between the previous and the new %%points hold on plot(xnew(1),xnew(2),'xk'); x1=[x(1),xnew(1)]; y1=[x(2),xnew(2)]; plot(x1,y1) disp(['iteration ',num2str(i),', the current point is:(',num2str([xnew(1)]),',',num2str([xnew(2)]),')'... ' with step sizing: ', num2str(tstep) ]); if i>100 break; end end hold off