.Solving ODE Error in MATLAB Using ODE45 Solver

In summary, the conversation is about solving an ODE in MATLAB using the ODE45 solver. The user is facing an error stating that the input argument "y" is undefined. They are seeking help and have shared the complete code for reference. The solution involves creating two separate m-files, one for the function and one for the code, and placing the function in its own m-file. The conversation also mentions a workaround by tricking MATLAB into thinking the function is nested.
  • #1
matlabphd
2
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
  • #2
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.
 
  • #3
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.
 

1. What is an ODE and why does it need to be solved?

An ODE, or Ordinary Differential Equation, is a mathematical equation that describes how a quantity changes over time. It is used to model a wide range of phenomena in science and engineering. ODEs need to be solved in order to obtain a mathematical solution that accurately predicts the behavior of the system being studied.

2. What is the ODE45 solver in MATLAB?

The ODE45 solver is an algorithm used in MATLAB to solve ODEs numerically. It is a variable-step solver that uses a combination of fourth and fifth order Runge-Kutta methods. This allows it to provide accurate solutions for a wide range of ODEs, including stiff and non-stiff problems.

3. How do I use the ODE45 solver in MATLAB?

To use the ODE45 solver in MATLAB, you must first define your ODE as a function in a separate file. Then, you can use the command "ode45" followed by the name of your ODE function, the time interval to solve for, and the initial conditions. The solver will then output the solution as a vector of time and corresponding values for the dependent variables.

4. What are some common errors encountered when using the ODE45 solver?

Some common errors when using the ODE45 solver include insufficient accuracy, excessive computation time, and the inability to handle stiff problems. These errors can be addressed by adjusting the solver tolerances, using a different solver, or modifying the ODE function to improve its stability.

5. Can the ODE45 solver be used for systems of ODEs?

Yes, the ODE45 solver can be used for systems of ODEs as well as single ODEs. The ODE function used with the solver must be modified to handle multiple dependent variables, and the initial conditions must be provided for each variable. Additionally, the output solution will be a matrix with each column representing the values for a different dependent variable.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
880
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
123
Back
Top