Skip to content

Instantly share code, notes, and snippets.

@sritee
Created July 5, 2018 06:28
Show Gist options
  • Select an option

  • Save sritee/59504b8b6096d056fb5f3f4c826d9ff3 to your computer and use it in GitHub Desktop.

Select an option

Save sritee/59504b8b6096d056fb5f3f4c826d9ff3 to your computer and use it in GitHub Desktop.
function[]=visualize_2D_optimizer(f,traj)
%input - trajectory matrix of size (2,n) containing n iterates of 2D optimizer's trajectory, f is a function which evaluates pointwise.
x = linspace(-3,3,30);
y = linspace(-3,3,30);
[X,Y] = meshgrid(x,y); %creating grid of points to evaluate f for the contour.
val=zeros(size(X));
for m=1:size(X,1)
for n=1:size(X,2)
val(m,n)=f([X(m,n);Y(m,n)]);
end
end
contour(X,Y,val)
hold on;
%we now have the function contours. Let us plot the trajectory!
for k=1:size(traj,2)
plot(traj(1,k),traj(2,k),'*');
if k>1
line([traj(1,k-1),traj(1,k)],[traj(2,k-1),traj(2,k)]) %optimizer descent line.
end
end
hold off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment