2

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?

Foad
  • 605
  • 4
  • 13

2 Answers2

7

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

enter image description here

Bob Hanlon
  • 157,611
  • 7
  • 77
  • 198
3

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

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.

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