Can someone explain to me whats off with this function handle?
Show older comments
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?
4 Comments
Andrew Newell
on 24 Apr 2017
It probably has something to do with the fact that you don't use dy after you define it.
Jan
on 24 Apr 2017
"Is not recognized" is a vague description of the problem only. Do you get an error message? Then please read it carefully and if this does not reveal the details for you, please post a complete copy of the message in the forum.
Adam
on 24 Apr 2017
What are you expecting
@(X)(dy)
to do? dy would have to be defined in the workspace above that call for this to make even some sense, but the placeholder 'X' is not used at all so has no purpose.
Mohannad Abboushi
on 24 Apr 2017
Accepted Answer
More Answers (1)
Jan
on 24 Apr 2017
Try this:
[predator, prey] = lotka_volterra(@dy, [0 10], [4 4], 0.1)
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!