function newton
x0 = input('Enter the initial guess value, x: ');
tolerance = input('Enter the tolerance value, tol: ');
max_i = input('Enter the maximum no. of iteration, max_i: ');
r = input('Enter the value of relative roughness, e/D: ');
RN = input('Enter the Reynolds number, RN: ');
f = input('Enter the Friction factor, f: ');
c = colebrook/colebrook_deriv;
x=x0;
for i=1:max_i
y = x;
x = y - (c);
if abs(x-y)<= tolerance
disp(['root = ', num2str(x)])
break
end
end
fprintf('The number of iterations: %d\n', i)
My functions I'm calling are
function colebrook
%%Formula for C(fd)
CFd =(1/sqrt(f)) + 2*log10(r/(3.7)+ 2.51/(RN*sqrt(f)));
end
function colebrook_deriv
CFd1 = 1/(2*f^(3/2)) + 2.51/RN*f^(3/2)log(10)(r/3.7) + 2.51/(RN*f^(1/2));
end
Please do help with answers

2 Kommentare

Stephen23
Stephen23 am 26 Nov. 2020
Bearbeitet: Stephen23 am 26 Nov. 2020
None of your functions have input or output arguments, which are required if you want to pass data to/from those functions.
How to add input/output arguments to a function is explained here:
How to call functions with input/output arguments is explained here:
Thank you for you interest. It was very helpful

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephan
Stephan am 26 Nov. 2020
Bearbeitet: Stephan am 26 Nov. 2020

0 Stimmen

Give your functions outputs:
newton
function newton
x0 = input('Enter the initial guess value, x: ');
tolerance = input('Enter the tolerance value, tol: ');
max_i = input('Enter the maximum no. of iteration, max_i: ');
r = input('Enter the value of relative roughness, e/D: ');
RN = input('Enter the Reynolds number, RN: ');
f = input('Enter the Friction factor, f: ');
c = colebrook/colebrook_deriv;
x=x0;
for i=1:max_i
y = x;
x = y - (c);
if abs(x-y)<= tolerance
disp(['root = ', num2str(x)])
break
end
end
fprintf('The number of iterations: %d\n', i)
function CFd = colebrook
%%Formula for C(fd)
CFd =(1/sqrt(f)) + 2*log10(r/(3.7)+ 2.51/(RN*sqrt(f)));
end
function CFd1 = colebrook_deriv
CFd1 = 1/(2*f^(3/2)) + 2.51/RN*f^(3/2)*log(10)*(r/3.7) + 2.51/(RN*f^(1/2));
end
end
Also check the function for colebrook_deriv i had to make a change (missing '*' i guess) tha you should correct if needed. Problems here:
CFd1 = 1/(2*f^(3/2)) + 2.51/RN*f^(3/2)log(10)(r/3.7) + 2.51/(RN*f^(1/2));
% ^ ^
% | |
% ----------- Check here
Also i added the final end, because i suspect that you wanted to have nested functions.

Weitere Antworten (0)

Kategorien

Mehr zu Computational Fluid Dynamics (CFD) finden Sie in Hilfe-Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by