This question already has an answer here:

I'm trying to plot the friction factor for Darcy-Weisbach formula based on the von Karman-Nikuradse formula for subsonic compressible flow:

$$ \frac{1}{\sqrt{f}}=2 \log \left(\operatorname{Re} \, \sqrt{f} \right)-0.8 \tag{1}$$

So I defined a function using NSolve:

fKN[R_] := NSolve[(1/Sqrt[f] == (2*Log[R*Sqrt[f]] - 0.8)), f]

And tried to plot it using

Plot[fKN[R], {R, 10^5, 10^6}]

But I get a bunch of errors:



I would apreciate if you could help me know what is the problem and how I can solve it?


marked as duplicate by Carl Woll, Coolwater, anderstood, LCarvalho, MarcoB Feb 11 '18 at 2:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ The eq. (1) and that in NSolve do not agree, what is the correct? $\endgroup$ – José Antonio Díaz Navas Feb 7 '18 at 15:02
  • $\begingroup$ the latex one is correct $\endgroup$ – Foad Feb 7 '18 at 15:04


fKN[R_] := NSolve[(1/Sqrt[f] == (2*Log[R*Sqrt[f]] - 0.8)), f][[1, 1, 2]] // Quiet

With this,

LogLinearPlot[fKN[R], {R, 10^5, 10^6}]

enter image description here

Note: in this case, FindRoot is actually faster than NSolve. I leave it to you to implement it, in case speed is important to your needs.

  • $\begingroup$ Thanks a lot. would you please elaborate what [[1, 1, 2]] // Quiet does and maybe giving an example with FindRoot ? $\endgroup$ – Foad Feb 7 '18 at 15:11
  • 1
    $\begingroup$ @Foad the output from NSolve is of the form {{f->0.2}}. To extract the number, we use [[1, 1, 2]]. Finally, NSolve alerts that it is going to ignore possible multi-valued and branches, and it is going to calculate only one solution; Quiet prevents this warning message (which is in fact irrelevant here), $\endgroup$ – AccidentalFourierTransform Feb 7 '18 at 15:14

The exact solution can be obtained using Solve

sol = f /. Assuming[R > 0,
    Solve[(1/Sqrt[f] == (2*Log[R*Sqrt[f]] - 4/5)), f] //
     Simplify] // Quiet

(* {1/(4*ProductLog[-(R/(2*E^(2/5)))]^2), 
   1/(4*ProductLog[R/(2*E^(2/5))]^2)} *)

Using FunctionDomain to determine which solution applies to the region of interest

FunctionDomain[#, R] & /@ sol

(* {R < 0 || 0 < R <= 2/E^(3/5), -(2/E^(3/5)) <= R < 0 || R > 0} *)

Consequently, the second solution is desired


fKN[R_] = sol[[2]];

LogLinearPlot[fKN[R], {R, 10^5, 10^6}]

enter image description here


Not the answer you're looking for? Browse other questions tagged or ask your own question.