Error in the for loop or equality sign
Show older comments
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 Comments
Alex Mcaulley
on 21 May 2019
What do you mean by "is not working"? What do you expect?
Walter Roberson
on 21 May 2019
x=70:0.01:75;
for i=1:x;
When you execute that for loop what do you expect the first value of i to be? What do you expect the second value of i to be? What do you expect the last value to be?
Faisal Baig
on 21 May 2019
Walter Roberson
on 21 May 2019
Please answer my question about the expected values of i
Faisal Baig
on 21 May 2019
Answers (1)
Jos (10584)
on 21 May 2019
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
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!