function plotdefield(defn,axisvec,ngrid) % PLOTDEFIELD plots the direction field for a single first order % differential equation. % % plotdefield(defn,axisvec) uses the d.e. defined % in normal form in function whose name is in the string defn. % The function must have calling structure defn(t,y) % and work with vector input arguments. % % axisvec is the vector [tmin,tmax,ymin,ymax] giving % the minimum and maximum desired values of t and y. % % ngrid is an optional argument specifying the number of line % segments in t and y directions; the default value is 9. % % Specify "axis('square')" before calling this routine to get % a square diagram if desired. % Author: Colin Fox, January 1995. % Modified for MatLab 4 Aug 1995 % Modified for MATLAB 5 June 23 1997 Michelle Wood if nargin < 3, ngrid = 9; end gap = (axisvec([2,4]) - axisvec([1,3]))/ngrid; t = linspace(axisvec(1),axisvec(2),ngrid); y = linspace(axisvec(3),axisvec(4),ngrid); [tt,yy] = meshgrid(t,y); dy = zeros(size(tt)); dt = ones(size(tt)); for tcount = 1:ngrid for ycount = 1:ngrid dy(ycount,tcount)... = feval(defn,tt(ycount,tcount),yy(ycount,tcount)); end end dirfield(dt,dy,t,y) axis((axisvec + (gap([1,1,2,2]) .* [-1,1,-1,1])))