% compute u,v using finite differencing with hypothetically 
% observed pressure data and compare with actual values

dx=0.2;dy=0.2; %spacing between gridpoints
[x y]=meshgrid(-1:dx:1,-1:dy:1); %grid of measuring instruments
p=x.^2+y.^2; %"oberved" pressure
[nx,ny]=size(p); %number of x,y gridpoints (this stores to both nx and ny)

%u=-dp/dy, v=dp/dx

%create finite difference computed velocity variables
u_fd=zeros(nx,ny); v_fd=u_fd; 

%loop through all y to compute u=-dp/dy
for n=2:nx-2
    u_fd(n,:)=-(p(n+1,:)-p(n-1,:))/(2*dy); %compute -dp/dy for yn and all x
end

%loop through all x to compute v=dp/dx
for n=2:nx-2
    v_fd(:,n)=(p(:,n+1)-p(:,n-1))/(2*dx); %compute dp/dx for xn and all y
end

subplot(2,1,1),quiver(x,y,u_fd,v_fd),title('velocity by finite difference')

u=-2*y;v=2*x;
subplot(2,1,2),quiver(x,y,u,v),title('actual velocity')
