Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This works fine. I've done a few things here. First, I've used a simpler definition of the function using the global variables you've defined anyway. I find this a little nicer than passing the args= to the solvers, it also enables easier use of your own custom solvers if you ever need something like that. I've used the generic <code>root</code> function as an entry point rather than using a particular algorithm - this is nice because you can simply pass a different method later. I've also fixed up your spacing to be as recommended by <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">PEP 8</a> and fixed your erronious rewriting of the equation. I find it more intuitive simply to write LHS - RHS rather than manipulate as you did. Also, notice that I've replaced all the integer literals with 1.0 or whatever to avoid problems with integer division. 0.02 is regarded as a pretty standard starting point for the friction factor.</p> <pre><code>import numpy from scipy.optimize import root w = 10.0 d = 0.22 rho = 1.18 ni = 18.2e-6 Re = w*d*rho/ni k = 0.2e-3 def f(x): return (-2*numpy.log10((2.51/(Re*numpy.sqrt(x))) + (k/(3.71*d))) - 1.0/numpy.sqrt(x)) print root(f, 0.02) </code></pre> <p>I must also mention that fixed point iteration is actually faster than even Newton's method for this problem. You can use the built-in fixed point iteration routine by defining <code>f2</code> as follows:</p> <pre><code>def f2(x): LHS = -2*numpy.log10((2.51/(Re*numpy.sqrt(x))) + (k/(3.71*d))) return 1/LHS**2 </code></pre> <p>Timings (starting further from the root to show speed of convergence):</p> <pre><code>%timeit root(f, 0.2) 1000 loops, best of 3: 428 µs per loop %timeit fixed_point(f2, 0.2) 10000 loops, best of 3: 148 µs per loop </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload