Advertisement
riabcis

Untitled

Mar 27th, 2018
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. % Plot the trajectory of Anti-Gradient Descent
  2. [x,y]=meshgrid(-20:.3:21);
  3. z=-20*exp(-0.2*(0.5*(x.^2+y.^2)).^(1/2))-exp(0.5*(cos(2*pi*x)+cos(2*pi*y)));
  4. figure(1)
  5. hold off
  6. contour(x,y,z)
  7. [dx,dy]=gradient(z,.2,.2);
  8. hold on
  9. quiver(x,y,dx,dy)
  10. % set the initial point
  11. x=[1,0];
  12. xs=x; % dummy variable required for the iterative process
  13. itsk=50; % the number of iterations
  14. step=0.1; % the step size
  15. for i=1:itsk
  16. % compute the next point
  17. x = xs - step*gradient1(xs);
  18. % plot the step
  19. plot([xs(1),x(1)],[xs(2),x(2)],'r',[xs(1),x(1)],[xs(2),x(2)],'ro')
  20. % refresh the variables
  21. xs=x;
  22. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement