MATLAB: Can i make loop from this equation

for loop

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

Best Answer

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