I'm trying to make a loop using 'for' or 'while' for this situation but i am lost:
Sa = 0.0233 %ft^2 Sb = 0.0884 %ft^2 Va = 9.5600 %ft/s Vb = 2.5200 %ft/s
if Sa > Sb hf = 0.4*(1-(Sb/Sa))*(((Vb)^2)*(0.5)) class = 'friction loss from sudden compression of cross section' elseif Sa < Sb hf = ((1-(Sa/Sb))^2)*(((Va)^2)*(0.5)) class = 'friction loss from sudden expansion of cross section' end

 채택된 답변

Chaya N
Chaya N 2016년 10월 26일

0 개 추천

I don't see why you would need any loops here. Your if-else statements seem to be doing just fine.
But, if your Sa and Sb variables are vectors, and if they are of the same length, then you would do:
for idx = 1:numel(Sa)
if Sa(idx) > Sb(idx)
hf(idx) = 0.4*(1-(Sb(idx)/Sa(idx)))*(((Vb)^2)*(0.5)); % class = 'friction loss from sudden compression of cross section'
elseif Sa(idx) < Sb(idx)
hf(idx) = ((1-(Sa(idx)/Sb(idx)))^2)*(((Va)^2)*(0.5)); % class = 'friction loss from sudden expansion of cross section'
end
end

댓글 수: 4

Little miss sunshine
Little miss sunshine 2016년 10월 27일
Hi, I would like to ask why when same length need to do like what you showed?
Chaya N
Chaya N 2016년 10월 27일
To get each of your equation values from hf, you need values of both Sa and Sb. If you did not have them in corresponding pairs, you would at the very least need to define how they are related.
The code above uses a single for loop to index both Sa and Sb. If they were different length vectors you would only get as many hf values as the smaller vector (Example: If lengths are Sa = 5 and Sb = 6, then hf has length 5). But, if you wanted to compare responses of all combinations of Sa and Sb values, then you would require either two loops or use some other tactic to generate these combinations (For Sa = 5 and Sb = 6, total combinations = 5*6 = 30) beforehand, in which case you would still end up with equal length vectors!
Your question does not address either of these cases. So, it is up to you to decide which approach you need.
Little miss sunshine
Little miss sunshine 2016년 10월 27일
Can I do like this?
Sa = 0.0233 %ft^2 Sb = 0.0233 %ft^2 Va = 9.5600 %ft/s Vb = 2.5200 %ft/s
for idx = 1:numel(Sa) if Sa(idx) > Sb(idx) hf(idx) = 0.4*(1-(Sb(idx)/Sa(idx)))*(((Vb)^2)*(0.5)); class = 'friction loss from sudden compression of cross section' elseif Sa(idx) < Sb(idx) hf(idx) = ((1-(Sa(idx)/Sb(idx)))^2)*(((Va)^2)*(0.5)); class = 'friction loss from sudden expansion of cross section' elseif Sa(idx) == Sb(idx) hf(idx) = 0 class = 'no friction loss from sudden expansion or compression of cross section' end end
You can certainly do that, but it still doesn't change the equal lengths condition for Sa and Sb. It does not calculate hf for all possible combinations of Sa and Sb either.
Notice the line below:
for idx = 1:numel(Sa)
This line is setting the counter for the loop. If Sa is longer than Sb, you would get an error message. If Sb is longer than Sa, then this program will run but hf will have the same length as Sa. Would you just discard the remaining elements in Sb then?
If you have unequal number of elements in Sa and Sb, and are willing to ignore the extra elements in one of them, then change that loop counter to:
for idx = 1:min(numel(Sa),numel(Sb))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

태그

질문:

2016년 10월 26일

댓글:

2016년 10월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by