I have created a function that relies on a guess, and now I need to create a for loop for that function that plugs in the answer it gives back in to the "OLD" original guess.

조회 수: 2 (최근 30일)
I have to turn my function into a for loop that plugs "f" back in where "OLD" is, as "OLD" is the original guess for f. I have created this for loop using my professsors advice, but I keep getting errors. Is there a trick for the for loop that I am missing or am I just not doing it correctly?
%% Inputs
E=0.00015; % (m) Surface Roughness
D=0.315; % (m) Hydraulic Diameter
R=125000; % (unitless) Reynolds Number
OLD=0.1; % (unitless) Friction Factor ; Initial Guess for RHS of Equation 1
%% Function Definitions
[f]=colebrook(E,D,R,OLD);
%% Code
function [f] = colebrook(E,D,R,OLD)
f=1/(-2*log10(((E/(3.7*D))+2.51/(R*sqrt(OLD)))))^2;
for i=1:50
f(i)=OLD(i);
end
end

채택된 답변

Mathieu NOE
Mathieu NOE 2021년 9월 16일
hello
yes , your "recursion" is not really working
this works
Once you're satisfied with the code , you can reùove / comment the two lines where you see : % debug only
code :
%% Inputs
E=0.00015; % (m) Surface Roughness
D=0.315; % (m) Hydraulic Diameter
R=125000; % (unitless) Reynolds Number
f_init=0.1; % (unitless) Friction Factor ; Initial Guess for RHS of Equation 1
%% Function Definitions
[f]=colebrook(E,D,R,f_init);
%% Code
function [f] = colebrook(E,D,R,f_init)
f = f_init;
for ci=1:10
f_new = 1/(-2*log10(((E/(3.7*D))+2.51/(R*sqrt(f)))))^2;
f = f_new;
f_save(ci) = f; % debug only
end
plot(f_save);% debug only
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by