How to code a function for solving equation by iteration?
3 views (last 30 days)
Show older comments
I'd like to write a function which calculates the pressure drop given by:
P = (1/2)*rho*omega^2*(L/D)*lambda
where rho - fluid's density, omega - fluid's velocity, L - pipe's length, D - pipe's diameter, lambda - function of the Reynolds number.
- For Reynolds<2320, lambda = 64/Reynolds
- For 2320<Reynolds<10^5, lambda = 0.3164/(Reynolds^0.25)
- For 10^5<Reynolds<10^6, 1/sqrt(lambda) = 2*log(Reynolds*sqrt(lambda)) - 0.8, to be solved via iteration according to this algorithm: letting tol = 0.01 and lambda0 = 1, the starting value x0 should be x0 = 1/sqrt(lambda0) whereas the next value is x = 2lg(Reynolds/x0) - 0.8. The "guessing" process is to continue until the absolute value of (x - x0) is less than tol.
- For Reynolds>10^6, calculations are halted and the program generates a message about that using the error() function.
May I code it thus:
function PDROP = ex(rho, omega, length, diameter, Reynolds)
if (Reynolds<2320)
lambda = 64/Reynolds;
elseif (Reynolds>2320 && Reynolds<10^5)
lambda = 0.3164/(Reynolds^0.25);
elseif (Reynolds>10^5 && Reynolds<10^6)
tol = 0.01;
x0 = 1;
x = 2*log10(Reynolds) - 0.8;
while (abs(x - x0) > tol)
x0 = x;
x = 2*log10(Reynolds/x0) - 0.8;
end
lambda = x^(-2);
elseif (Reynolds>10^6)
error('Reynolds number must not be larger than 10^6');
end
PDROP = 0.5*rho*(omega^2)*(length/diameter)*lambda;?
0 Comments
Answers (0)
See Also
Categories
Find more on Fluid Dynamics 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!