Output from while-loop at fixed values

조회 수: 1 (최근 30일)
Arjan Marelis
Arjan Marelis 2020년 3월 16일
댓글: Arjan Marelis 2020년 3월 16일
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

채택된 답변

dpb
dpb 2020년 3월 16일
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
Arjan Marelis
Arjan Marelis 2020년 3월 16일
dpb,
thank you. The ismembertol function solves the problem!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by