Fzero for Colebrook Equation
조회 수: 6(최근 30일)
I have looked here: https://uk.mathworks.com/help/matlab/ref/fzero.html
I am having no success in using fzero for
f = -1/sqrt(x)-2.*log10(((e./D)./3.7)+2.51/(Re*sqrt(x)))
Can someone please help with this? I want to know the iterations, root, time elapsed, and error.
I would ideally like to use the version that uses a function file, f.m.
John D'Errico 2021년 3월 14일
What you do not understand is that function have their own workspaces. If you create thse variables outside the function, then call it without passing in these variables, the function will fail, because it cannot "see" those variables. They don't exist in the function workspace. (Nested functions can see variables above them.)
If you insist on using an m-file function, then this should work:
p = 0.9*1000;
mu = 8*0.001;
e = 0.01;
D = 4*0.0254;
Q = (2000*42*3.785*10^-3)/(24*60*60);
A = (pi*(D.^2))/4;
Re = (p*D*Q)/(A*mu);
x0 = 0.01;
I've plotted the function to see if a root does exist. Only now do I want to use fzero.
Also, note the use of fzero where I provide a bracket around the root. When you give it only ONE value, then it must do a search. And negative values for x will cause problems with your function.
options = optimset('Display','iter');
[xval,fval,exitflag] = fzero(@(x) myfun(x,e,D,Re),[eps,10],options)
Save this function as an m-file. Note my use of ./ operator. It seems you know about .* but the ./ operator has escaped your attention.
function y = myfun(x,e,D,Re)
y = -1./sqrt(x)-2.*log10(((e./D)./3.7)+2.51./(Re*sqrt(x)));
As far as being told the time elapsed, you can use tic and toc, I suppose. fzero does not return that. I have no clue what you mean by the "error".