I don't know how to fix this error within my code.

1 次查看(过去 30 天)
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 个评论
Monique
Monique 2023-11-5
I'm not sure how to call the function since i thought the values were already defined in the code.
Stephen23
Stephen23 2023-11-5
编辑:Stephen23 2023-11-5
"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?

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2023-11-5
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];

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by