function gradient() %%%%%%%%%%%%%% x=-1.5 t=0.01 for i=1:1000; d=-g(x); x=x+t*d; y(i)=x; end %%%%%%%%%%%%%%%%% figure(1) fontsz=16; x = -2:0.1:2; plot(x,f(x),'-g'); hold on plot(y,f(y),'.r'); xlabel('$x$','Interpreter','latex','FontSize',fontsz) ylabel('$f(x)$','Interpreter','latex','FontSize',fontsz) legend({'$f(x)$','gradient'},'Interpreter','latex') %%%%%%%%%%%%%%%%%%%%T1%%%%%%%%%%%%%%%%%%%%%%%%% function d=f(x); d = (x-1).^2; %%%%%%%%%%%%%%%%%%%%T1%%%%%%%%%%%%%%%%%%%%%%%%% function d=g(x); d = 2*(x-1);