How to run a for loop with two functions (nested for loop?)
Show older comments
I am trying to perform a series of iterative calculations given two inputs. First I want to calculate for three pressures. Second, I want to calculate over a range of values from 0:0.01:1.0. I am using two functions: one from the XSteam.m package and one user-defined function. I am getting an error with the second function but I don't really know how to resolve this. First is the input in the script.
P = [50.0, 100.0, 150.0]; % Pressure (bars)
alpha = 0.0:0.01:1.0; % Void fraction from 0 to 1 in increments of 0.01
dh = 0.01; % Pipe diameter in meters (1 cm = 0.01 m)
for i = 1:numel(P);
sigma(i) = XSteam('st_p',P(i));
rhof(i) = XSteam('rhoL_P',P(i));
rhov(i) = XSteam('rhoV_P',P(i));
for n = 1:numel(alpha);
a(n) = calc_a( alpha, sigma(i), rhof(i), rhov(i), dh );
end
end
The values with XSteam are correctly generated if I put them in their own loop but I am unable to get the second function working when running through a for loop. Here is the calc_a function
function [ a ] = calc_a( alpha, sigma, rhof, rhov, dh )
g = 9.81;
db = sqrt(sigma /(g*(rhof - rhov)));
if alpha < 0.5
a = (6*alpha)/ db
elseif alpha >0.8
a = (4*sqrt(alpha))/ dh
else
omega = (alpha - 0.5)/(0.8 - 0.5)
a = ((6*alpha)/ db)*(1-omega) + ((4*alpha)/ dh)*omega
end
end
Is there a better way to do this or what am I missing? Thanks for the help.
1 Comment
SALAH ALRABEEI
on 5 Jun 2021
I think u might have a mistake in alpha in the 2nd loop. If Iam correct you should send one value of apha at foe each iteration. So it is alpha(i) not alpha
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!