Error "Not enough Input Arguments" in predator-prey model

1 view (last 30 days)
I'm trying to understand the working of ODE45 to solve multiple variable equations. I copied a code from internet but when I run this code, matlab gives error
Not enough input arguments.
Error in predPrey (line 2)
f=[-x(1)+3*x(3);-x(2)+2*x(3);x(1)^2-2*x(3)];
function f=predPrey(t,x);
the code is given here:
f=[-x(1)+3*x(3);-x(2)+2*x(3);x(1)^2-2*x(3)];
[t,xa] = ode45(@predPrey,[0 1.5],[0 1/2 3]);
plot(t,xa(:,2))
title('y(t)')
xlabel('t'), ylabel('y')
can someone please help me in this error so I can continue modifying model for my system.

Accepted Answer

Star Strider
Star Strider on 3 Jan 2018
You did not copy it correctly or implement it correctly.
Try this:
predPrey = @(t,x) [-x(1)+3*x(3);-x(2)+2*x(3);x(1)^2-2*x(3)];
[t,xa] = ode45(predPrey,[0 1.5],[0 1/2 3]);
plot(t,xa(:,2))
title('y(t)')
xlabel('t'), ylabel('y')

More Answers (1)

Walter Roberson
Walter Roberson on 3 Jan 2018
The way you give your code makes it appear as if all of your code is within predPrey.m and that your call to ode45 is on line 3 of predPrey.m and that you try to run the ode by executing the predPrey file. If so then you need to break out the line starting from [t,xa] into a different file. Or, if you are running R2016b or later, you could use one file that is not named prepPrey.m that contains
[t,xa] = ode45(@predPrey,[0 1.5],[0 1/2 3]);
plot(t,xa(:,2))
title('y(t)')
xlabel('t'), ylabel('y')
function f=predPrey(t,x)
f=[-x(1)+3*x(3);-x(2)+2*x(3);x(1)^2-2*x(3)];
The alternative to all of this is to use just one file containing:
prePrey = @(t, x) [-x(1)+3*x(3);-x(2)+2*x(3);x(1)^2-2*x(3)];
[t,xa] = ode45(predPrey,[0 1.5],[0 1/2 3]);
plot(t,xa(:,2))
title('y(t)')
xlabel('t'), ylabel('y')

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!