Error in the for loop or equality sign

4 次查看(过去 30 天)
Hello,
I am using following code to check the discharges in the pipelines connecting three reservoirs. But the Loop is not working I guess. Could anybody tell the error?
Ela=100;
Elb=70;
Elc=30;
ka=1563.085;
kb=38.10;
kc=12729.55;
x=70:0.01:75;
for i=1:x;
Qa=sqrt((Ela-x)/ka);
Qb=sqrt((x-Elb)/kb);
Qc=sqrt((x-Elc)/kc);
if Qb+Qc==Qa
disp(Qa)
disp(Qb)
disp(Qc)
break
else
continue
end
end
  5 个评论
Walter Roberson
Walter Roberson 2019-5-21
Please answer my question about the expected values of i
Faisal Baig
Faisal Baig 2019-5-21
I can use
'for x= 70:0.01:75'
instead of
x= 70:0.01:75;
for i = 1:x;
So basically, I just want to give range of my values to find Qa, Qb and Qc. i doesn't matter

请先登录,再进行评论。

回答(1 个)

Jos (10584)
Jos (10584) 2019-5-21
Welcome to the world of floating point arithmetic, where
if 0.1+0.2 == 0.3
disp('0.1+0.2 equals 0.3')
else
disp('0.1+0.2 does not equal 0.3')
end
More info here:
You want to check with a tolerance:
if abs(Qa+Qb-Qc) < 1e-10
disp('Qa+Qb practically equals Qc')
end

类别

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