Error in the for loop or equality sign

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

Alex Mcaulley
Alex Mcaulley 2019년 5월 21일
What do you mean by "is not working"? What do you expect?
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
Faisal Baig 2019년 5월 21일
I want to check different values of Qa, Qb and Qc for given range of x until the sum Qb+Qc becomes equal to Qa. At that point, I want the code to show the values of Qa, Qb and Qc which satisfies the condition. But when I run the code, only I get the matrix of Qa, Qb, Qc agaisnt the given range of x and my condition (Qa=Qb+Qc) is not met somehow.
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일

1 개 추천

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2019년 5월 21일

답변:

2019년 5월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by