% predprey.m - predator prey model for CDS 101/110
% RMM, Oct 02
%
% This file contains a simple model for a predator prey system.  The 
% model is developed following the treatment in the book by Murray
% (the other one).
%
% The states for these equations are
%
%     x(1)	# of rabbits (prey)
%     x(2)	# of foxes (predator)
%
% The equations allow for a quadratic term in both the predator and 
% prey species that cause the population to fall off if you get too 
% many of a given species.  These terms are turned off by default.
% 
% Ref: 

function dxdt=predprey(t, x, flags, rH, K, a, Th, rP, k)

% Extract the states
x1 = x(1);
x2 = x(2);

% Set up the parameter values [these should be passed as arguments...]
if (nargin < 4) rH = 0.2; end;	% growth rate of rabbits, absent foxes
if (nargin < 5) K = 500; end; 	% predator free carrying capacity
if (nargin < 6) a = 0.3; end; 	% interaction term 
if (nargin < 7) Th = 0.5; end;  % time constant for prey consumption
if (nargin < 8) rP = 0.1; end;	% growth rate of the predator
if (nargin < 9) k = 0.2; end;   % rabbit to fox gain

% Check to see if a feedback law has been defined
if (rH < 0)
  % Assume that the user has defined a function that we can call
  rH = predprey_rh(t, x, rH);
end

dx1 = rH * x1 * (1 - x1/K) - a*x1*x2/(1 + a*x1*Th);
dx2 = rP * x2 * (1 - x2/(k*x1));

dxdt = [dx1; dx2];
