ODE - Save variables for every time step
14 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Bjorn Gustavsson
2021-3-8
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 个评论
Bjorn Gustavsson
2021-3-8
What with the second loop-based suggestion? That explicitly calculates yy for every step in the solution you have...
sko
2021-4-22
10 个评论
Bjorn Gustavsson
2021-4-22
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...).
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!