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.

1 vue (au cours des 30 derniers jours)
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

Réponse acceptée

Mathieu NOE
Mathieu NOE le 16 Sep 2021
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

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by