Values not entering IF loop for unknown reason!

3 次查看(过去 30 天)
Hi I am really struggling with the following, I have code that loops over a time interval, within this I have an if statement from which a function file is ran. From my values I know this should work but it seems nothing is entering the if statement? IF anyone could help on the following it would be much appreciated!!
CODE
time=0; %Initialise time
tfinal=40;
diagstep=10;
diagcounter=0;
while time<tfinal
if ((round(time/diagstep) * diagstep) == time)
[xcounter] = ParticleCounterFun(x0, particleno)
diagcounter=diagcounter+1
end
%Where the function file ParticleCounterFun is;
function [ xcounter ] = ParticleCounterFun( x0, particleno )
%Initialise counters used in E field diagnostics
counter01=0;
counter12=0;
counter23=0;
counter34=0;
counter45=0;
for np=1:particleno
if (0<=x0(np)) & (x0(np)<1)
counter01=counter01+1;
end
if (1<=x0(np)) & (x0(np)<2)
counter12=counter12+1;
end
if (2<=x0(np)) & (x0(np)<3)
counter23=counter23+1;
end
if (3<=x0(np)) & (x0(np)<4)
counter34=counter34+1;
end
if (4<=x0(np)) & (x0(np)<5)
counter45=counter45+1;
end
end
xcounter=[counter01 counter12 counter23 counter34 counter45]
end
I really need this solved as I am unable to continue with work on my project so any help would be greatly appreciated! Thanks in advance
  6 个评论
Star Strider
Star Strider 2014-2-23
My guess is that you’re yet another victim of rounding error.
Just after
while time < tfinal
insert:
test = (round(time/diagstep) * diagstep) - time
My guess is that it will be non-zero, so:
(round(time/diagstep) * diagstep) == time
will never evaluate to ‘true’.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by