To make the arrows of the quiver plot follow the contour lines, you need to ensure that the vectors are tangent to the contour lines. This can be achieved by calculating the gradient of the contour plot and then using it to adjust the direction of the arrows.
To calculate the gradient, you can replace the below lines creating “U” and “V”:
U = (sig*H/pi).*cos(pi*Y/H).*(((exp(r2*L)-1).*exp(r1*X)+(1-exp(r1*L)).*exp(r2*X))/(exp(r2*L)-exp(r1*L))-1);
V = -1.*(sig*H^2/pi^2).*sin(pi*Y/H).*(((r1.*exp(r2*L)-1).*exp(r1*X)+r2.*(1-exp(r1*L)).*exp(r2*X))/(exp(r2*L)-exp(r1*L))-1);
with
[px, py] = gradient(p, x, y);
% Normalizing the vectors
magnitude = sqrt(px.^2 + py.^2);
px = px ./ magnitude;
py = py ./ magnitude;
% Assigning to U & V
U = -py;
V = px;
Replacing this to the existing code, we get results as below:
For more information, refer to the following MATLAB documentation links:

