% contour plot of V(x,y)= 10y^2 - kappa*cos(pi/2*x) + 0.2*(x-2)^2
%
% jbr, 9/3/2011
%
n = 201;
x = linspace(-5, 5, n);
y = linspace(-5, 5, n);
kappa = 2;
xx = kron(x, ones(n,1));
yy = kron(y', ones(1,n));
xx2 = (xx-2).*(xx-2);
yy2 = yy.*yy;
z = 10*yy2 - kappa*cos(xx*pi/2) + 0.2*xx2;
val = [-1, 0, 1, 2, 3, 5, 8, 10];
[c, lev] = contour(x,y,z,val);
axis([-1.75, 3, -1, 1])
xlabel ("$q$");
% avoid rotating y-axis label
text (-5.75, 0, "$p$")
% write contour data for gnuplot
col = 1;
myfile = fopen('energy.dat', 'w');
while col <= size(c, 2)
coln = col + c(2,col);
data = c(:,col+1:coln);
% print this contour data and move to next contour line
fprintf(myfile, '%e %e \n', data)
fprintf(myfile, '\n\n')
col = coln + 1;
end
fclose(myfile);
% now solve the gradient system to locate the arrows on the contour
% plot; start on the V=3 contour
w0 = [1.60000; 0.36666; 3];
npts = 101;
time = linspace(0, 0.5, npts);
[time, w] = ode15s (@rhs, time, w0);
hold on
plot(w(:,1),w(:,2),'-o')
% choose rows of w matrix corresponding to energy contours in val(1:5)
for i = 1:5
[Vval, Vrows(i)] = min (abs(w(:,3)-val(i)));
end
Vrows = fliplr(Vrows);
diff = w(Vrows+1,1:2) - w(Vrows,1:2);
scale = 0.15;
arrowloc = [w(Vrows,1:2), w(Vrows,1:2)+scale*diff./kron(vecnorm(diff,2,2),ones(1,2))]';
disp ("write out some good gnuplot arrow commands -- gradient system")
for i = 1: length(Vrows)
txt = sprintf ("set arrow %i from %g, %g to %g, %g filled size graph 0.025, 15 lw 1\n", ...
i+5, arrowloc(1:4,i))
end