.Solving ODE Error in MATLAB Using ODE45 Solver

Click For Summary
SUMMARY

The discussion addresses an error encountered while using the ODE45 solver in MATLAB to solve a system of ordinary differential equations (ODEs) represented by the Lotka-Volterra equations. The error message "Input argument 'y' is undefined" arises from improper function placement within MATLAB. The solution involves creating separate m-files for the function and the script, ensuring that the function is defined correctly in its own file, which resolves the issue. Additionally, a workaround is suggested by using a dummy function to encapsulate the ODE45 call.

PREREQUISITES
  • Familiarity with MATLAB programming environment
  • Understanding of ordinary differential equations (ODEs)
  • Knowledge of the Lotka-Volterra model
  • Experience with MATLAB function and script file structure
NEXT STEPS
  • Review MATLAB documentation on ODE45 solver usage
  • Learn how to structure functions and scripts in MATLAB
  • Explore the Lotka-Volterra equations and their applications
  • Investigate MATLAB's nested functions and their implementation
USEFUL FOR

Mathematics students, researchers in computational biology, and MATLAB users seeking to solve ODEs effectively.

matlabphd
Messages
2
Reaction score
0
Dear all. I am trying to solve an ODE in MATLAB using ODE45 solver. But the programe keep saying

? Input argument "y" is undefined.

Error in ==> lvv at 4
yprime=[a*y(1)-b*y(1)*y(2);-r*y(2)+c*y(1)*y(2)];

Can anybody be of help? Please.

The complete code is this:
function yprime=lvv(t,y)
%LV: Contains Lotka-Volterra equations
a=.5471;b=.0281;c=.0266;r=.8439;
yprime=[a*y(1)-b*y(1)*y(2);-r*y(2)+c*y(1)*y(2)];
[t,y]=ode45(@lvv,[0 20],[30;4])
plot(t,y(:,1))
Thanks
 
Physics news on Phys.org
make on m-file called whatever you want an place:

[t,y]=ode45(@lvv,[0 20],[30;4])
plot(t,y(:,1))

this in that. Then make another m-file called lvv and place

function yprime=lvv(t,y)
%LV: Contains Lotka-Volterra equations
a=.5471;b=.0281;c=.0266;r=.8439;
yprime=[a*y(1)-b*y(1)*y(2);-r*y(2)+c*y(1)*y(2)];

this in that, then it works, have tested it. You should always place functions in their own m-file, there are ways to put them in the same, but if you place it in another, then you never get problems.
 
a way I sometimes use, is to trick MATLAB to think of the lvv function as a nested function by making your m-file into a function that doesn't do anything then it works, that is writing

function idontdoanything=LaLaLa

[t,y]=ode45(@lvv,[0 20],[30;4])
plot(t,y(:,1))


function yprime=lvv(t,y)
%LV: Contains Lotka-Volterra equations
a=.5471;b=.0281;c=.0266;r=.8439;
yprime=[a*y(1)-b*y(1)*y(2);-r*y(2)+c*y(1)*y(2)];


that is start you m-file with a dummy function.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
3K