clear
format short
xmin=-10; 
xmax=10;
ymin=-10;
ymax=10;
X=xmin:0.1:xmax;
Y=ymin:0.1:ymax;
for i=1:length(X)
   for j=1:length(Y)
      Z(i,j)=fungsixy(X(i),Y(j));
   end
end
mesh(X,Y,Z), grid on, xlabel('x'),ylabel('y'),zlabel('J(x,y)')
pause
plot(Z), grid on, ylabel('J(x,y)')
pause
dx=0.01;
dy=0.01;
Xo=10;
Yo=10;
x0(1)=Xo;
y0(1)=Yo;
step(1)=1;
Jmin(1)=fungsixy(x0(1),y0(1));
for i=2:10000
   clc
   step(i)=i;
   count=i
   Xo=x0(i-1);
   Yo=y0(i-1);
   Jmin(i)=fungsixy(Xo,Yo);
   x0(i)=Xo;
   y0(i)=Yo;
   J1=fungsixy(Xo+dx,Yo);
   J2=fungsixy(Xo+dx,Yo+dy);
   J3=fungsixy(Xo+dx,Yo-dy);
   J4=fungsixy(Xo-dx,Yo);
   J5=fungsixy(Xo-dx,Yo+dy);
   J6=fungsixy(Xo-dx,Yo-dy);
   J7=fungsixy(Xo,Yo+dy);
   J8=fungsixy(Xo,Yo-dy);
   if J1<Jmin(i)
      Jmin(i)=J1;
      x0(i)=Xo+dx;
      y0(i)=Yo;
   elseif J2<Jmin(i)
      Jmin(i)=J2;
      x0(i)=Xo+dx;
      y0(i)=Yo+dy;
   elseif J3<Jmin(i)
      Jmin(i)=J3;
      x0(i)=Xo+dx;
      y0(i)=Yo-dy;
   elseif J4<Jmin(i)
      Jmin(i)=J4;
      x0(i)=Xo-dx;
      y0(i)=Yo;
   elseif J5<Jmin(i)
      Jmin(i)=J5;
      x0(i)=Xo-dx;
      y0(i)=Yo+dy;
   elseif J6<Jmin(i)
      Jmin(i)=J6;
      x0(i)=Xo-dx;
      y0(i)=Yo-dy;
   elseif J7<Jmin(i)
      Jmin(i)=J7;
      x0(i)=Xo;
      y0(i)=Yo+dy;
   elseif J8<Jmin(i)
      Jmin(i)=J8;
      x0(i)=Xo;
      y0(i)=Yo-dy;
   end
   if x0(i)>10;
      break
   elseif x0(i)<-10;
      break
   elseif y0(i)>10;
      break
   elseif y0(i)<-10;
      break
   elseif abs(Jmin(i)-Jmin(i-1))<0.0000000000000000000000001
      break
   end
end
plot3(x0,y0,Jmin), xlabel('x'), ylabel('y'),grid on
pause
subplot(211), plot(step,x0,step,y0), grid on, xlabel('step'), ylabel('x dan y')
subplot(212), plot(step,Jmin), grid on, xlabel('step'), ylabel('J(x,y)')

