I don't know how to fix this error within my code.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
function f_sj = swamee_jain_friction_factor(Re_values, D, epsilon)
% Given hydraulic diameter and absolute roughness
D = 0.1; % Replace with your hydraulic diameter
epsilon = 0.01; % Replace with your absolute roughness
% Reynolds numbers to compare
Re_values = [1e4, 1e5, 1e6, 1e7];
term1 = (Re_values.^0.9) + (6.26 / abs(D / epsilon));
term2 = (Re_values.^0.25) .* (log10(term1).^0.25);
f_sj = 0.079 ./ term2;
% Initialize arrays to store calculated and Moody chart friction factors
f_sj_values = zeros(size(Re_values));
% Calculate friction factors using Swamee and Jain formula and Moody chart
for i = 1:length(Re_values)
Re_values = Re_values(i);
% Calculate friction factor using Swamee and Jain formula
f_sj_values(i) = swamee_jain_friction_factor(Re_values, D, epsilon);
end
% Display the results
for i = 1:length(Re_values)
fprintf('Re = %.2e, Swamee and Jain f = %.6f\n', Re_values(i), f_sj_values(i));
end
^^that's the code and it says that when i calulate the friction values it runs out of memory due to infinite recursion.
5 Kommentare
Stephen23
am 5 Nov. 2023
Bearbeitet: Stephen23
am 5 Nov. 2023
"I'm not sure how to call the function since i thought the values were already defined in the code."
They are, but that is not the problem. Read the error message, apparently you are getting "...it runs out of memory due to infinite recursion." Now look at your code again. This is what your code does when you click RUN (or call it any other way):
- you RUN swamee_jain_friction_factor (with or without input arguments makes absolutely no difference)
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- Inside that function call on the first loop iteration you ...
- call swamee_jain_friction_factor(Re_values, D, epsilon); on line 17.
- etc. etc.
You wrote your function to keep calling itself, so that is exactly what it is doing. You wrote nothing to tell it to stop calling itself, so it will keep calling itself recursively until the end of the universe or MATLAB reaches its recursion limit.
How you call this function makes absolutely no difference to this. What inputs you provide makes absolutely no difference to this (the input arguments get ignored anyway).
The first question is: did you intend to write a recursive function?
Antworten (1)
Image Analyst
am 5 Nov. 2023
You said "I just hit the "run" button. " Well, that's the problem. You can't do that because it will have no idea what to use for Re_values, D, and epsilon. Plus, you let them be sent in but then you simply overwrite them in the first few lines of the function, essentially ignoring what you're sending in.
function f_sj = swamee_jain_friction_factor(Re_values, D, epsilon)
% Given hydraulic diameter and absolute roughness
D = 0.1; % Replace with your hydraulic diameter
epsilon = 0.01; % Replace with your absolute roughness
% Reynolds numbers to compare
Re_values = [1e4, 1e5, 1e6, 1e7];
You can either get rid of the arguments, and then clicking the Run button will work, OR delete the function line entirely,
OR (better) you can have a little test script to assign values and call the function. Like in test.m have this:
% Assign some values.
% Given hydraulic diameter and absolute roughness
D = 0.1; % Replace with your hydraulic diameter
epsilon = 0.01; % Replace with your absolute roughness
% Reynolds numbers to compare
Re_values = [1e4, 1e5, 1e6, 1e7];
% Now call the function:
f_sj = swamee_jain_friction_factor(Re_values, D, epsilon)
and make sure you don't overwrite the incoming argument values. So delete these lines from the file swamee_jain_friction_factor.m
% Given hydraulic diameter and absolute roughness
D = 0.1; % Replace with your hydraulic diameter
epsilon = 0.01; % Replace with your absolute roughness
% Reynolds numbers to compare
Re_values = [1e4, 1e5, 1e6, 1e7];
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!