ODE - Save variables for every time step
23 views (last 30 days)
Show older comments
Hello Guys,
I am stuck with saving time dependent variables inside a model function ... I was browing some already answered questions but none seems to help.
I have the following:
[t, f_val] = ode15s(@model,tspan,yeq);
That yields my f_val as a function of time.
But inside my model function I have also yy variable (time dependent) I would like to trace
function [f, yy] = model(t,yeq)
f= ... %some expression
yy= ... %some expression
How can I extract yy as a function of time?
Cordially
0 Comments
Answers (2)
Bjorn Gustavsson
on 8 Mar 2021
This is something that is (simplest?) done by calculating the second output once after you've integrated the ODE-system. Either in one fell swoop if your ODE-function can handle arrays for input of t and f:
[f_second_time_around_or_tilde, yy] = model(t,f_val);
Or you can quickly loop over t:
for i_t = numel(t):-1:1,
yy(i_t,:) = model(t(i_t),f_val(i_t,:));
end
Since the odeNN-functions take cunningly adaptive steps in time we cannot trivially save all the values of yy along the steps...
HTH
3 Comments
Bjorn Gustavsson
on 8 Mar 2021
What with the second loop-based suggestion? That explicitly calculates yy for every step in the solution you have...
sko
on 22 Apr 2021
10 Comments
Bjorn Gustavsson
on 22 Apr 2021
Yes, the in solution I provided I couldn't now what the dimensions of your additional outputs were expected to be, therefore I opted to provide something that you could easily adjust to match the dimensions of your outputs (one or the other parameter might even have different dimensions at different time-steps as far as I could see...).
See Also
Categories
Find more on Equation Solving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!