% The gradient descent method
% clear; clc;
% x=-1:0.1:2;
% f=x.^3-2*x.^2+2;
% plot(x,f); grid on;
% Gradient descent method
% clear; clc;
% syms x
% f=x.^3-2*x.^2+2;
% gf=diff(f,x);
% e=0.1;
% x=2;
% for k=1:1:100
%     x0=x;
%     x=x-e*eval(gf);
%     if abs(x-x0) < 0.0001
%         break;
%     end
% end
% display(x);
% display(k);
% 2-D Case
% clear; clc;
% [X1,X2] = meshgrid(-4:.1:4,-4:.1:4);
% f=(X1-2).^2+(X2-1).^2;
% surf(X1,X2,f);
% Gradient descent method
clear; clc;
syms X1 X2
f=(X1-2).^2+(X2-1).^2;
gf=gradient(f);
e=0.1;
X1=0; X2=0;
x=[X1; X2];
for k=1:1:100
    x0=x;
    x=x-e*eval(gf);
    if abs(x-x0) < 0.001
        break;
    end
    X1=x(1,1); X2=x(2,1);
end
display(x);
display(k);

