% 22.3MB1 Fall 01
% Yvan Petillot
% Curl of a 2D vector field 

close all; clear;
% generates mesh grid for calculus
x = -1:0.1:1; y = -1:0.1:1;    
[xx,yy] = meshgrid(x,y);     

% generates the 2D vector field and displays it
px = exp(-(xx.^2+yy.^2));
py = sin(xx.*yy);
figure;quiver(x,y,px,py,2);
xlabel('x');ylabel('y');
title('2-D vector field: {\bf P} = e^{-(x^2+y^2)}{\bf x} + sin(xy) {\bf y} ');
pause;
%caculates the gradient of each component of the field with respect to x and y
[pxx,pxy]=gradient(px,0.1,0.1);
[pyx,pyy]=gradient(py,0.1,0.1);

curl_p_z=(pyx-pxy); % as given by the definition of curl (See notes)
[m,n]=size(curl_p_z);
curl_p_x=zeros(m,n);
curl_p_y=zeros(m,n);
figure;quiver3(xx,yy,zeros(m,n),curl_p_x,curl_p_y,curl_p_z,4,'r');
hold on; surf(xx,yy,zeros(m,n));shading flat;colormap pink;
rotate3d;
xlabel('x');ylabel('y'); zlabel('z');
title('Curl of 2-D vector field: \nabla \times {\bf P}');

pause;

figure;surf(x,y,curl_p_z);
rotate3d;
xlabel('x');ylabel('y'); zlabel('|\nabla \times {\bf P}|');
title('Magnitude of Curl of 2-D vector field: |\nabla \times {\bf P}|');

