简体   繁体   English

是否可以从ODE45函数中导出其他变量?

[英]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. 我有一个输入给ode45的运动函数文件方程。 Necessarily, the output variables of the function file is ydot. 功能文件的输出变量必须是ydot。

Within my equation of motion function file, I calculate many objects from the state vector, y, to prescribe forces. 在运动函数文件的方程式中,我根据状态向量y计算了许多对象以规定力。

After ode45 is finished, I would like access to these objects at every time step so that I can calculate an energy. ode45完成后,我想在每个时间步访问这些对象,以便可以计算能量。

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. 相对于在每个时间步长上重新计算它们,将它们从Runge-Kutta流程中拉出,无论如何都是更快的方法,这样反而会更快。

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. 不能保证右侧的ODE函数甚至在输出点被调用,因为它们通常是根据自适应步长算法计算的点进行插值的。

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. 我经常见到的但需要搜索引用的一个技巧是让函数返回您需要的所有值,并将返回列表缩减为ODE45调用中的派生类。 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. 然后在sol的点上运行odefunc以提取额外的信息。

Perhaps that idea of selecting the output only works in python. 也许选择输出的想法仅适用于python。 Then define an explicit wrapper 然后定义一个显式包装器

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

that you then normally call in ode45 . 然后通常调用ode45

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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