Output from while-loop at fixed values

1 vue (au cours des 30 derniers jours)
Arjan Marelis
Arjan Marelis le 16 Mar 2020
Commenté : Arjan Marelis le 16 Mar 2020
I am running time-dependent simulations with variable time-step size dt in seconds. I have implemented a while-loop that updates the elapsed time t_elapsed as t_elapsed + dt and stops when t_elapsed > t_total where t_total is a predefined number in seconds. Now I want to output the solution (a vector) to a text file at every x seconds, i.e. every 3600 seconds. This problem throws me of my game a bit as dt is now variable. Any help is much appreciated.
Arjan

Réponse acceptée

dpb
dpb le 16 Mar 2020
Either compute the number of timesteps corresponding to your dt and desired frequency if dt is constant and output every N timesteps:
N=Tout/dt;
if mod(iterNo,N)==0
fprintf(fid,data)
end
or, alternatively, test the time variable modulo Tout is within dt. ismembertol might be of interest here for floating point comparisons.
  1 commentaire
Arjan Marelis
Arjan Marelis le 16 Mar 2020
dpb,
thank you. The ismembertol function solves the problem!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by