Values not entering IF loop for unknown reason!
Afficher commentaires plus anciens
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 commentaires
the cyclist
le 23 Fév 2014
I can't run your code because you don't indicate what x0 and particleno are.
Mischa Kim
le 23 Fév 2014
Seems to be a duplicate of http://www.mathworks.de/matlabcentral/answers/117142-why-do-i-get-error-undefined-function-or-variable.
Peter Nave
le 23 Fév 2014
time does not seem to change inside the loop.
Phoebe
le 23 Fév 2014
Image Analyst
le 23 Fév 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
le 23 Fév 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’.
Réponses (0)
Catégories
En savoir plus sur Numerical Integration and Differentiation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!