简体   繁体   中英

Is it possible to export additional variables from within an ODE45 function?

I have an equation of motion function file which I feed into ode45. Necessarily, the output variables of the function file is ydot.

Within my equation of motion function file, I calculate many objects from the state vector, y, to prescribe forces.

After ode45 is finished, I would like access to these objects at every time step so that I can calculate an energy.

Instead of recalculating them over every time step, it would be faster to just pull them from the Runge-Kutta process when they are calculated as intermediate steps anyway.

Is it possible to do this?

There is no guarantee that the ODE function for the right side is even called at the output points as they are usually interpolated from the points computed by the adaptive step size algorithm.

One trick I have often seen but would need to search for references is to have the function return all the values you will need and cut the return list down to the derivative in the ODE45 call. Modulo appropriate syntax

function [ydot, extra] = odefunc(t,y,params)

and then use

sol = ode45(@(t,y): odefunc(t,y,params)(1),...)

and then run odefunc on the points in sol to extract the extra information.

Perhaps that idea of selecting the output only works in python. Then define an explicit wrapper

function ydot = odewrapper(t,y)
    [ydot,~] = odefunc(t,y,params)
end

that you then normally call in ode45 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM