%   dir_field  direction field for first order ODE
%   dir_field(func,tval,yval) plots the direction field for the first order
%   ODE given by func, using the grid of t and y values given by the vectors
%   tval and yval. func is either a the name of an inline function of two 
%   variables, or a string with the name of an m-file.

% assumes uniform mesh

function dir_field(func,tval,yval)
[tm,ym]=meshgrid(tval,yval);
dt = tval(2) - tval(1); 
dy = yval(2) - yval(1);
yp=feval(func,tm,ym); 
s = 1./max(1/dt,abs(yp)./dy)*0.35;
h = ishold;
quiver(tval,yval,s,s.*yp,0,'.r'); hold on;
quiver(tval,yval,-s,-s.*yp,0,'.r');
if h
  hold on
else
  hold off
end

axis tight;
