% Direction field m-file (script) 
% Source: Bäckström: Praktisk matematik med Matlab
clear variables; hold off;
c = 0.25; k = 1;
xb=-5; xe=5; delx=(xe - xb)/30;
ymax=3.1; dely=ymax/30;   % *** problem dependent
[X,Y]=meshgrid(xb:delx:xe,-ymax:dely:ymax);
  DX=ones(size(X));  % Unit x-xomponents of arrows
  DY= -c - k * sin(X) ./ Y;      % Slopes
  L=sqrt(DX.^2+DY.^2); % Initial lengths
  DX1=DX./L; DY1=DY./L; % Unit length for all arrows
%figure(1); quiver(X,Y,DX1,DY1,'.'); grid on;  % The period ('.') suppresses
                                               % arrowheads
figure(1); quiver(X,Y,DX1,DY1,'.'); grid on;
  axis([xb xe -ymax ymax]); title('Direction field');

