How to load intermediate variable into workspace while using ode45 ?
2 views (last 30 days)
Stephen23 on 17 Jun 2022
Edited: Stephen23 on 17 Jun 2022
Here is the neat, easy, robust approach which returns exactly the a values at the exact t and x output values:
tspan = 1:100;
x0 = 0;
[t,x] = ode45(@system, tspan,x0);
[~,a] = arrayfun(@system,t,x)
function [dx,a] = system(t,x)
dx = -x(1);
a = 1;
Note that trying to obtain the value generated during the ODE-solver routine is much more complex (although this is often what beginners think of, it is practically unsolvable because you have no easy way to distinguish between points which are kept vs. discarded, e.g. when the solver goes backwards a few steps. Not every call of the objective function corresponds to output values, and you have no way to know which... consider the implications of that).