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.
0 个评论
采纳的回答
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')
3 个评论
更多回答(1 个)
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')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!