So I have a function where I want to model the population of predators and prey based off of the classic model Lotka-Volterra. In my code I designate dy as an array with the two equations.
function[predator,prey]=lotka_volterra(fprime, timespan, y0, h)h=.1; %step size
%initial conditions
X(1)=timespan(1);Y(1)=4;Y(2)=4;prey=Y(1);predator=Y(2);dy=[2*prey(X)-predator(X)*prey(X); predator(X)*prey(X)-2*predator(X)];i=2;while X(end)<timespan(end) X(i)=X(i-1) + h; Y(i)=Y(i-1)+h*fprime(X(i-1)); % Y(i)=Y(i+1)+h*y'(X(i-1))
i=i+1;end end
In the command window I typed,
[predator,prey]=lotka_volterra(@(X)(dy), [0 10], [4 4], .1)
but it is not recognizing that the equations are supposed to change by X (time). What am I messing up?
Best Answer