Printing all outputs of the function that has been solved via ode45

6 次查看(过去 30 天)
I have defined a function to be solved using ode45. The function is called endoFlightDeriv
function [deriv, m, h] = endoFlightDeriv(t, X, E, L,phaseCode)
Then I solve it using ode45
[time_vert,sol_vert]=ode45(@(t,x)endoFlightDeriv(t,x,E,L,PhaseCode),tspan,x0,options)
My question is, how can I output the values of m and h at each integration step?
This is the first question that I ever post, so I apologize in advance if this is not clearly stated. Obviously I can further explain my problem.
  4 个评论
Sam Chak
Sam Chak 2024-3-26
No worries. I believe your specific technical issue in MATLAB has been resolved and closed. However, it's worth noting that there are at least six approaches available to achieve this, depending on the complexity of the output functions involved.
Niccolo Carioli
Niccolo Carioli 2024-3-26
Your statement is indeed correct. This is a code for the simulation of the endoatmospheric trajectory of a launcher. However this is not written in a state space notation, the function simply defines the vectorial equation of motion to be solved. The mass is a scalar and its change it's computed using the flow rate and the time

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2024-3-26
移动:Steven Lord 2024-3-26
This questions gets asked regularly on this forum: users often imagine that there must be some easy way to get some values out of the function when it is being solved by ODE45 (or whatever solver they are using). In practice it is not that simple: ODE solvers may take arbitrarily small steps or even steps backwards... it is not a trivial task to figure out which function calls produce data which correspond to the values returned by the ODE solver... in fact this task is quite difficult.
Thus in most cases it is much simpler to just call the function after the ODE solver has finished, using the values obtained by the ODE solver. A simple loop and calling the function usually is sufficient.

更多回答(1 个)

Torsten
Torsten 2024-3-26
Assuming m and h are scalars, use
[time_vert,sol_vert]=ode45(@(t,x)endoFlightDeriv(t,x,E,L,PhaseCode),tspan,x0,options)
for i = 1:numel(time_vert)
[~, m(i), h(i)] = endoFlightDeriv(time_vert(i), sol_vert(i,:), E, L,phaseCode)
end
If not, change the loop appropriately.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by