The best way to plot direction fileds is to use existing m-files, credited to John Polking from Rice University (http://math.rice.edu/~polking/). He is the author of two special matlab routines: dfield8 plots direction fields for single, first order ordinary differential equations, and allows the user to plot solution curves; pplane8 plots vector fields for planar autonomous systems. It allows the user to plot solution curves in the phase plane, and it also enables a variety of time plots of the solution. It will also find equilibrium points and plot separatrices. The latest versions of dfield8 and pplane8 m-functions are not compatible with the latest matlab version. You can grab its modification that works from dfield.m

Since matlab has no friendly subroutine to plot direction fileds for ODEs, we present several codes that allow to plot these fields directly. Many others can found on the Internet. The following Octave code shows how to plot the direction field for the linear differential equation \( y' = 5\,y - 3\,x . \)

% plot direction field for first order ODE y' = f(t,y)
function dirfield
              
f = @(t,y) -y^2+t % f = @(independent_var, dependent_var)
    % mathematical expression y in terms of t

figure;             
dirplotter(f, -1:.2:3, -2:.2:2) % dirfield(f, t_min:t_spacing:t_max, y_min:y_spacing:y_min)
    % using t-values from t_min to t_max with spacing of t_spacing
    % using y-values from y_min to t_max with spacing of y_spacing
end

function dirplotter(f,tval,yval) %  plots direction field using aboved defined (f,tval,yval)
[tm,ym]=meshgrid(tval,yval); % creates a grid of points stored in matrices in the (t,y)-plane
  dt = tval(2) - tval(1); % determines t_spacing and y_spacing based on tval
  dy = yval(2) - yval(1); % determines t_spacing and y_spacing based on yval
  fv = vectorize(f); % prevents matrix algebra; if f is a function handle fv is a string
  if isa(f,'function_handle') % if f is function handle
  fv = eval(fv); % turn fv into a function handle from a string 
  end
  yp=feval(fv,tm,ym); % evaluates differential equation fv from tm and ym from meshgrid
  s = 1./max(1/dt,abs(yp)./dy)*0.35;
  h = ishold; % define h as ishold which returns 1 if hold is on, and 0 if it is off
  quiver(tval,yval,s,s.*yp,0,'.r'); hold on; % plots vectors at each point w direction based on righthand side of differential equation
  quiver(tval,yval,-s,-s.*yp,0,'.r');
  if h % h above defined as ishold
  hold on % retain current plot 
  else % refers to all not h 
  hold off % resets axes properties to default
  end
  axis([tval(1)-dt/2,tval(end)+dt/2,yval(1)-dy/2,yval(end)+dy/2]) % sets the scaling for axes on current graph to ends of tval and yval   
end    

% Simple direction field plotter
clear;
[T Y]=meshgrid(-2:0.2:2,-2:0.2:2);
dY=cos(3*T)+Y./T;
dT=ones(size(dY));
scale=sqrt(1+dY.^2);
figure;
quiver(T, Y, dT./scale, dY./scale,.5,'Color',[1 0 0]);
xlabel('t');
ylabel('y');
title('Direction Field for dy/dt');
axis tight;
hold on;

% change color
% do multiple examples, x.^2-y.^2; use T, etc

% Simple direction field plotter
clear;
[T Y]=meshgrid(-2:0.2:2,-2:0.2:2);
dY=T.^2-Y.^2;
dT=ones(size(dY));
scale=sqrt(1+dY.^2);
figure;
quiver(T, Y, dT./scale, dY./scale,.5,'Color',[0 1 0]);
xlabel('t');
ylabel('y');
title('Direction Field for dy/dt');
axis tight;
hold on;


			Complete
  1. Babolian, E. and Javadi, Sh., “New method for calculating Adomian polynomials”, Applied Mathematics and Computation, 2004, Volume 153, Issue 1, 25 May 2004, pages 253--259. https://doi.org/10.1016/S0096-3003(03)00629-5