How do i fix this error?

Réponses (1)

dpb
dpb le 24 Sep 2025
Modifié(e) : dpb le 26 Sep 2025

0 votes

Paste the code as text and the complete error message; can't do anything with an image.
The statement
f=zeros(num_pipes,1);
creates a vector of three elements so when you write
for i=1:num_pipes
...
f(i)=32*f*pipe_length(i)*Q(i)^2 ...
then the RHS is going to also be a 3-vector of zeros that you're trying to store into one location. Can't do that.
One presumes the second, unsubscripted f is supposed to be a friction factor based on some other correlation. Fix that to be some other scalar variable.

3 commentaires

% Constants
e = 0.26e-3; % Roughness coefficient in meters
rho = 1000; % Density of fluid in kg/m^3
mu = 1e-3; % Viscosity of fluid in Pa.s
gc = 9.81; % Acceleration due to gravity in m/s^2
% Pipe properties (You need to define your network)
pipe_length = [100, 150, 200]; % Length of each pipe in meters
pipe_diameter = [0.15, 0.2, 0.1]; % Diameter of each pipe in meters
% Initial flow rates (You need to provide initial guesses)
Q = [0.01, 0.02, 0.03]; % Initial flow rates in m^3/s
% Hardy Cross Method Parameters
max_iterations = 100; % Maximum number of iterations
tolerance = 1e-6; % Convergence tolerance
% Initialize variables
num_pipes = length(pipe_length);
H = zeros(num_pipes, 1);
iteration = 0;
converged = false;
while iteration < max_iterations
% Calculate friction losses for each pipe
f = zeros(num_pipes, 1);
for i = 1:num_pipes
A = pi * (pipe_diameter(i) / 2)^2; % Cross-sectional area of the pipe
hydraulic_radius = A / (pi * pipe_diameter(i)); % Hydraulic radius
Reynolds_number = rho * Q(i) / (mu * hydraulic_radius); % Reynolds number
% Calculate Darcy-Weisbach friction factor using the given equation
f(i) = (32 * f * pipe_length(i) * Q(i)^2) / (pi * gc * pipe_diameter(i)^5);
% Calculate head loss in the pipe
H(i) = 4 * f(i) * pipe_length(i) * (Q(i)^2) / (2 * gc * pipe_diameter(i));
end
% Calculate flow corrections
delta_Q = zeros(num_pipes, 1);
for i = 1:num_pipes
sum_dQ = 0;
for j = 1:num_pipes
if i ~= j
sum_dQ = sum_dQ + (H(i) - H(j)) / (f(i) - f(j));
end
end
delta_Q(i) = -sum_dQ;
end
% Update flow rates
Q = Q + delta_Q;
% Check for convergence
if max(abs(delta_Q)) < tolerance
converged = true;
break;
end
iteration = iteration + 1;
end
Unable to perform assignment because the left and right sides have a different number of elements.
if converged
disp('Converged to a solution:');
for i = 1:num_pipes
fprintf('Pipe %d: Flow rate = %f m^3/s\n', i, Q(i));
end
else
disp('Did not converge to a solution within the maximum number of iterations.');
end
Daniel
Daniel le 28 Sep 2025
Here is the full code with the error message.
Torsten
Torsten le 28 Sep 2025
Modifié(e) : Torsten le 28 Sep 2025
% Calculate Darcy-Weisbach friction factor using the given equation
f(i) = (32 * f * pipe_length(i) * Q(i)^2) / (pi * gc * pipe_diameter(i)^5);
This f (bold)
f(i) = (32 * f * pipe_length(i) * Q(i)^2) / (pi * gc * pipe_diameter(i)^5);
is a vector while this f(i) (bold)
f(i) = (32 * f * pipe_length(i) * Q(i)^2) / (pi * gc * pipe_diameter(i)^5);
is a scalar.
So you try to assign a vector
(32 * f * pipe_length(i) * Q(i)^2) / (pi * gc * pipe_diameter(i)^5);
to a scalar
f(i)
- consequently the error message.
@dpb suspected that f is a friction factor you forgot to specify in your code - thus that f is different from f(i). We don't know.
Shouldn't the headloss be prescribed externally if you want to compute volumetric flowrates ? I get the impression that both quantities are treated as unknowns in your code.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Fluid Dynamics dans Centre d'aide et File Exchange

Produits

Question posée :

le 24 Sep 2025

Modifié(e) :

le 28 Sep 2025

Community Treasure Hunt

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

Start Hunting!

Translated by