Values not entering IF loop for unknown reason!

2 visualizzazioni (ultimi 30 giorni)
Phoebe
Phoebe il 23 Feb 2014
Commentato: Star Strider il 23 Feb 2014
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 Commenti
Star Strider
Star Strider il 23 Feb 2014
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’.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by