Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % Plot the trajectory of Anti-Gradient Descent
- [x,y]=meshgrid(-20:.3:21);
- z=-20*exp(-0.2*(0.5*(x.^2+y.^2)).^(1/2))-exp(0.5*(cos(2*pi*x)+cos(2*pi*y)));
- figure(1)
- hold off
- contour(x,y,z)
- [dx,dy]=gradient(z,.2,.2);
- hold on
- quiver(x,y,dx,dy)
- % set the initial point
- x=[1,0];
- xs=x; % dummy variable required for the iterative process
- itsk=50; % the number of iterations
- step=0.1; % the step size
- for i=1:itsk
- % compute the next point
- x = xs - step*gradient1(xs);
- % plot the step
- plot([xs(1),x(1)],[xs(2),x(2)],'r',[xs(1),x(1)],[xs(2),x(2)],'ro')
- % refresh the variables
- xs=x;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement