Creating for loop for a matrix of data

Hi, I'm trying to create a plot that shows system head vs flowrate.
The flowrate (Q1x) goes from 0 to 0.05 in 100 intervals. But I'm struggling to use fzero to calculate the friction factors corresponding to the flowrates.
A1 = 0.0254;
rho = 1024;
D1 = 0.23;
mu = 0.0025;
e = 4.60e-05;
L1 = 77.6;
V1 = 0.5254;
g = 9.81;
h_J = 16.3347;
h1 = 31.9276;
%Colebrook-White equation
ceq = @(f,edD,Re) 1/sqrt(f)+2*log10(edD/3.7+2.51/(Re*sqrt(f)));
Q1x = linspace(0,0.05,100);
n = length(Q1x);
for i = 1:n
V1x = Q1x/A1;
Re_1x = (rho*V1x*D1)/mu;
%Friction factor:
format shortE; %Display 5 s.f.
edD_1x(1:1,1:100) = e/D1; %e/D
f_i1x(1:1,1:100) = 1e-1;
ceq_set1x = @(f) ceq(f,edD_1x,Re_1x);
options1x(1:1,1:100) = optimset('Display','iter');
options = '';
fD_1x(i) = fzero(ceq_set1x,f_i1x,options1x);
f1x = fD_1x/4;
h1_frictionx = (2*f_i1x*L1*V1^2)/(D1*g);
h_system(i) = abs(h1+h1_frictionx-h_J);
end
Error using fzero
Second argument must be a scalar or vector of length 2.
%Plotting system head vs flowrate
figure(1)
plot(Q1x,h_system(i),'b-')
xlable('Flowrate, Q(m^3/s)')
ylable('System head, hS (m)')
title('Graph of system head (m) vs flowrate (m^3/s)')
Everytime when I run the script, there's an error for the fD_1x(i) = fzero(ceq_set1x,f_i1x,options1x) part that says 'Second argument must be a scalar or vector of length 2'.
How can I fix it?

回答(1 个)

f_i1x(1:1,1:100) = 1e-1;
That is a vector of length 100. And it is constant, not changed anywhere in the loop, so it is not clear why it was not set before the loop if it is going to be used as a vector.
options1x(1:1,1:100) = optimset('Display','iter');
A vector of options structures is... unexpected.
fD_1x(i) = fzero(ceq_set1x,f_i1x,options1x);
You are passing in a vector of length 100 for the second parameter, and a vector of options of length 100 for the third parameter.
I get the impression that you are thinking that you can pass in a vector of problems to work on, and that fzero() will handle them all. But that is not the case: fzero() can only handle one problem per call.

1 个评论

Starting Q1x below 1e-4 can result in NaN or in fzero attempting to use complex locations.
I had to modify the h1_frictionx calculation. You were using f_1x which is independent of the results calculated by fzero, so you were getting constant output. I modified it to use f1x that was just calculated.
format shortE; %Display 5 s.f.
A1 = 0.0254;
rho = 1024;
D1 = 0.23;
mu = 0.0025;
e = 4.60e-05;
L1 = 77.6;
V1 = 0.5254;
g = 9.81;
h_J = 16.3347;
h1 = 31.9276;
%Colebrook-White equation
ceq = @(f,edD,Re) 1/sqrt(f)+2*log10(edD/3.7+2.51/(Re*sqrt(f)));
Q1x = linspace(1e-4,0.05,100);
n = length(Q1x);
edD_1x = e/D1; %e/D
f_i1x = 1e-1;
options1x = optimset('Display','final');
for i = 1:n
V1x = Q1x(i)/A1;
Re_1x = (rho*V1x*D1)/mu;
%Friction factor:
ceq_set1x = @(f) ceq(f,edD_1x,Re_1x);
ceq_set1x(f_i1x)
fD_1x(i) = fzero(ceq_set1x,f_i1x,options1x);
f1x = fD_1x(i)/4;
h1_frictionx = (2*f1x*L1*V1^2)/(D1*g);
h_system(i) = abs(h1+h1_frictionx-h_J);
end
ans =
-1.7469e-01
Zero found in the interval [0.0886863, 0.108]
ans =
-1.7259e+00
Zero found in the interval [0.036, 0.145255]
ans =
-2.2420e+00
Zero found in the interval [0.036, 0.145255]
ans =
-2.5570e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-2.7828e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-2.9579e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.1004e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.2201e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.3230e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.4130e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.4928e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.5644e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.6290e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.6879e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.7420e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.7918e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.8379e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.8808e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.9209e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.9584e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-3.9937e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.0269e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.0583e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.0880e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.1161e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.1429e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.1684e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.1926e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.2158e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.2380e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.2592e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.2796e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.2991e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.3178e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.3359e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.3532e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.3700e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.3861e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4017e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4167e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4313e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4453e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4589e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4721e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4849e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.4973e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5093e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5209e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5323e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5433e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5540e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5644e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5745e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5844e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.5940e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6034e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6125e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6214e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6301e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6385e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6468e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6549e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6628e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6705e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6780e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6854e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6926e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.6996e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7065e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7132e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7198e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7263e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7326e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7388e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7449e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7509e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7567e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7625e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7681e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7736e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7790e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7843e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7895e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7946e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.7997e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8046e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8094e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8142e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8189e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8235e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8280e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8324e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8368e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8411e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8453e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8495e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8536e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8576e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8616e+00
Zero found in the interval [0.00949033, 0.164]
ans =
-4.8655e+00
Zero found in the interval [0.00949033, 0.164]
%Plotting system head vs flowrate
figure(1)
plot(Q1x,h_system,'b-')
xlabel('Flowrate, Q(m^3/s)')
ylabel('System head, hS (m)')
title('Graph of system head (m) vs flowrate (m^3/s)')

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by