Values not entering IF loop for unknown reason!
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Image Analyst
el 23 de Feb. de 2014
Phoebe: Please read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup. Then delete all that in your prior comment and attach your m-file with the paper clip icon. Then read this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
Star Strider
el 23 de Feb. de 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’.
Respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!