MATLAB - ode45 “ODEFUN” format

Is someone able to explain to me exactly what the "odefun" called by the "ode45" ODE solver in MATLAB is supposed to do?

My understanding is that you represent an n-order ODE as a system of n first-order ODEs and that, somehow, from this system, you create the "odefun" which "ode45" uses. My understanding is also that "odefun" should output a column array of derivative values at an the input independent variable value and that the function takes in a column array of values (but I'm not sure what these are).

How do you actually represent the system of first-order ODEs in "odefun"?

-

Yes you have to represent n-order ODE as a system of n 1st-order ODEs.

The function definition for ode45 is:

ode45('odefun', tspan, y_0, option1, option2, ...)


where:

• tspan : problem domain e.g [-10 10]. If you don't want a step-size picked by matlab you can do -10:0.1:10 for a step-size of 0.1
• y_0 : Initial conditions for the system. With n 1st-order ODEs, y_0 should be numeric vector of size n. with specification for all of y'(0), y''(0), y'''(0) ... . Of course you will have a translation to 1st-order like y1 = y, y2 = y', y3 = y'', ... .
• option1, option2, ... : These are meant for odefun.

with that the definition for odefun is:

odefun(t, y_n, option1, option2, ...)


Note: y_0 in ode45 vs y_n in odefun. y_n are the values at the nth discretization as they are computed by ode45. And t is the current time (assuming a dy/dt ODE)

What is expected of you in odefun?

Compute and return the right-hand side of the 1st-order ODEs. This is numeric vector of the same length as y_n. In most cases, these are some function of y_n

Example: If you have y' = 3 - option1 * y as your 1st-order ODE, then in odefun you return 3 - option1 * y_n. In this case y_n is of size 1 as I am assuming a single 1st-order ODE.

-