f=inline('x.^2+y.^2-2*x-4*y','x','y')

fx=inline('2*x-2','x','y')

fy=inline('2*y-4','x','y')

x=-1:.05:5;y=x;

% this is the fine mesh for the level curves.

[X,Y]=meshgrid(x,y);

Z=f(X,Y);

% We choose the level curves.

levels = [-6:.5:6];

contour(X,Y,Z,levels)

hold on

xx=-1:.2:5; yy=xx;

% This is the coarse mesh for the arrows

[XX,YY]=meshgrid(xx,yy);

U=fx(XX,YY); V=fy(XX,YY);

quiver(XX,YY,U,V)

axis equal
