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

1 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2018-1-3
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')

更多回答(1 个)

Walter Roberson
Walter Roberson 2018-1-3
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')

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by