Can i make loop from this equation?
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Chaya N
2016-10-26
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 个评论
Chaya N
2016-10-27
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!