# the command after "if" not work after awhile

1 view (last 30 days)
mohammad gudarzi on 23 May 2019
Commented: mohammad gudarzi on 23 May 2019
the "counter"=1 at start, the command under if work until t==8 but after that it's looklike "if" not working beside that "t" continue untill 200.
thanks
if t==(1*counter)
filename = 'y.xlsx';
filename1= 'V.xlsx';
filename2= 'Q.xlsx';
A=double(yp);
C=double(vp);
D=double(Qp);
for i=1:16
maty(counter,i)=A(1,i);
matQ(counter,i)=D(1,i);
matv(counter,i)=C(1,i);
my{counter,i}=[A(1,i)];
mv{counter,i}=[C(1,i)];
mq{counter,i}=[D(1,i)];
end
counter=counter+1;
end

Show 1 older comment
mohammad gudarzi on 23 May 2019
the step of t=0.005.
as i know "counter" never reset
clc;clear all;close all;
%%initial value
%% main program
maty=zeros(200,16);
matQ=zeros(200,16);
matv=zeros(200,16);
for t=0.005:delt:T
for i = 1:16
v(i)=Q0(i)/(B*y(i));
end
for i = 1:15
syms x
[ys(i)]=vpasolve(0==(x-y(i))+(teta*(y(i+1)-y(i))/(1+teta*(v(i+1)-v(i)))*(v(i)-(g*x)^0.5)));
vs(i)=(v(i)-teta*((g*y(i+1))^0.5*v(i)-(g*y(i))^0.5*v(i+1)))/(1-teta*(v(i)-v(i+1)-(g*y(i))^0.5+(g*y(i+1))^0.5));
Qs(i)=B*ys(i)*vs(i);
Sfs(i)= a* vs(i)+ b * vs(i)^2;
[yr(i)]=vpasolve(0==(y(i+1)-x)-teta*(y(i+1)-y(i))/(1+teta*(v(i+1)-v(i)))*(v(i+1)+(g*x)^0.5));
vr(i)=(v(i+1)-teta*((g*y(i))^0.5*v(i+1)-(g*y(i+1))^0.5*v(i)))/(1+teta*(v(i+1)-v(i)+(g*y(i+1))^0.5-(g*y(i))^0.5));
Qr(i)=B*yr(i)*vr(i);
Sfr(i)=a* vr(i)+ b * vr(i)^2;
end
%upstream condition
Qp(1)= 0.0003+0.0075*exp(-0.5*abs((t-50.7261)/24.495)^2.9063);
syms X
[ypp]=vpasolve(0==((vs(1)-2*(g*ys(1))^0.5)+g*(s0-Sfs(1))*delt)+(2*(g*X)^0.5)-(Qp(1)/(B*X)));
for i=1:14
yppp(i) = 0.5*(yr(i)+ys(i+1))+ teta*((Qr(i)-Qs(i+1))/(2*B));
end
%downstream condition
ypppp=0.1201*exp(-0.5*((t-81.7891)/23.2503)^2);
%start velocity
vpp=Qp(1)/(B*ypp);
%end velocity
vpppp= (vr(15)+2*(g*yr(15))^0.5)+(g*(s0-Sfr(15))*delt)-2*(g*ypppp)^0.5;
for i = 1:14
vppp(i)=0.5*(vr(i)+vs(i+1))+0.5*teta*((vr(i)^2-vs(i+1)^2)+g*(yr(i)-ys(i+1)))+g*delt*(s0-0.5*(Sfr(i)+Sfs(i+1)));
end
%combine yp & vp
yp=horzcat(ypp,yppp,ypppp);
vp=horzcat(vpp,vppp,vpppp);
if t==0:1:200
plot(X0,yp)
end
for i=1:15
Qp(i+1)=B*vp(i+1)*yp(i+1);
end
%output
if t==(1*counter)
filename = 'y.xlsx';
filename1= 'V.xlsx';
filename2= 'Q.xlsx';
A=double(yp);
C=double(vp);
D=double(Qp);
for i=1:16
maty(counter,i)=A(1,i);
matQ(counter,i)=D(1,i);
matv(counter,i)=C(1,i);
my{counter,i}=[A(1,i)];
mv{counter,i}=[C(1,i)];
mq{counter,i}=[D(1,i)];
end
counter=counter+1;
end
Q0=Qp;
y=yp;
aaaa=aaaa+1
end
xlswrite(filename,my)
xlswrite(filename1,mv)
xlswrite(filename2,mq)
Geoff Hayes on 23 May 2019
What are you expecting to happen with this code?
if t==0:1:200
plot(X0,yp)
end
Do you really mean to compare t to an array of values from 0-200?
mohammad gudarzi on 23 May 2019
that's not important, i can delete that but the problem is with next if

the cyclist on 23 May 2019
My best guess here is that you are running up against the limitations of floating point computation. For example, try putting this expression into MATLAB:
6*0.01 == (0.01+0.01+0.01+0.01+0.01+0.01)
You will find that the result is false (logical 0). This is probably unexpected by you.
Then try
6*0.01 - (0.01+0.01+0.01+0.01+0.01+0.01)
and you will see that the result is a number of order 1.e-18 (very tiny but not zero).
I suggest that you read this documentation page about floating point representation. There are also many, many answers here that talk about this issue. Be aware that this is not some kind of MATLAB error.
I also happened to notice that you have this line in your code:
if t==0:1:200
...
end
I'm not sure what you are expecting to happen there, but you will only enter that loop is t is equal to the vector [0 1 2 ... 199 200].

#### 1 Comment

mohammad gudarzi on 23 May 2019
thank you about answer, i test that and it's true, because of that tiny number the "if" not runnig.
thanks for help