Plot does not display. Using for loop and function.
4 次查看(过去 30 天)
显示 更早的评论
% Hello. I am using a for loop and a function to calculate head loss (hf) for various flow rates (Qo). When I try to plot(Qo, hf), nothing comes up. I have included the code I am using, along with a picture of the blank plot. Please help. Thank you!

clear variable
close all
clc
%% using Swamee-Jain & Colebrook equations to find (f)
for Qo = 0:2:20; %flowrate [GPM]
Q = Qo/15850.32314; %flowrate [m3/s]
Do = 2; %diameter [in]
D = Do*2.54./100; %diameter [m]
Lo = 10; %length [ft]
L = Lo * 0.3048; %length [m]
rho = 1025; %density [kg/m3]
mu = 0.00109; %dynamic viscosity [Pa.S]
g = 9.81; %gravity [m/s2]
A = (pi*D^2)/4; %area [m2]
v = Q/A; %velocity [m/s]
Re = (rho * v * D) / mu; %Reynolds number
releps = 0.00; %relative roughness
f = Swamee_Jain_Colebrook(releps, Re); %Darcy-Weisbach friction factor
hfo= f * (L/D) * ((v^2) / 2) / g; %head loss [m]
hf= hfo/0.3048 %head loss [ft]
end
hold on;
box on;
grid on;
plot(Qo,hf,'-')
axis([0 20 0 0.1])
function f=Swamee_Jain_Colebrook(releps, Re)
if Re < 2300
%Darcy-Weisbach friction factor for laminar flow
f = 64/Re;
elseif Re > 2300
%Darcy-Weisbach friction factor for turbulent flow
%Swamee-Jain equation
f0 = 0.25/((log10((releps/3.7)+(5.74/(Re^0.9))))^2);
%Colebrook equation
f = (1/(-2.0*log10((releps/(3.7))+(2.51/(Re*((f0)^(1/2)))))))^2;
end
end
0 个评论
回答(1 个)
Walter Roberson
2019-11-3
After the for Q0 loop, Q0 will be a scalar holding the last thing that was assigned to it, namely 20.
After that same loop, hf is going to be the last thing that was assigned to it. Your calculations in the loop are going to be based on the current scalar Q0 value, and you overwrite all of the hf variable each iteration, so after the loop hf will hold the value corresponding to Q0=20.
You then plot one scalar against another scalar, and you do not ask for markers. MATLAB only draws lines if you plot with a vector or array that has at least two adjacent finite values.
You need to store all of the hf results, not just the current one. You need a record of Q0 values used to plot against.
You should consider vectorizing your code using logical indices to select the various conditions.
Mask = Re<2300;
Output(Mask) = vector calculation involving Re(Mask)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!