% Modified from (Lynch, 2004):
% Chapter 8 - Planar Systems.
% Program_8a - Program to plot a vector field.
% Save M-file as vectorfield.m.
% Copyright Birkhauser 2004. Stephen Lynch.

% See the phase protraits in the book.
function vectorfield(deqns,xval,yval,t)
% deqns: differential equations and an inline function
% xval: x-range
% yval: y-range
% t: initial time
if nargin==3;   %
    t=0;        % Renders time initialization unnecessary   
end
m=length(xval);
n=length(yval);
x1=zeros(n,m);
y1=zeros(n,m);
for a=1:m
  for b=1:n
    pts = feval(deqns,t,[xval(a);yval(b)]); % Evaluate function
    x1(b,a) = pts(1);
    y1(b,a) = pts(2);
  end
end
arrow=sqrt(x1.^2+y1.^2);
quiver(xval,yval,x1./arrow,y1./arrow,.5,'r');
axis tight;

% End of Program_8a.

