# Plotting von Karman-Nikuradse formula [duplicate]

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:

NSolve::ifun

General::stop

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, MarcoBFeb 11 '18 at 2:37

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

Use

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}]


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.

• Thanks a lot. would you please elaborate what [[1, 1, 2]] // Quiet does and maybe giving an example with FindRoot ? – Foad Feb 7 '18 at 15:11
• @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), – 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

Clear[fKN]

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

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