필터 지우기
필터 지우기

Values not entering IF loop for unknown reason!

조회 수: 1 (최근 30일)
Phoebe
Phoebe 2014년 2월 23일
댓글: Star Strider 2014년 2월 23일
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
Image Analyst
Image Analyst 2014년 2월 23일
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
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