[SciPy-Dev] Comments on optimize.newton function

Gökhan Sever gokhansever at gmail.com
Sun May 22 17:02:49 EDT 2011


On Sun, May 22, 2011 at 2:50 PM,  <josef.pktd at gmail.com> wrote:
> try newton with fprime. My initial suggestion was in response to your
> statement that newton with fprime is too slow because the expression
> for the derivative is too complicated and slow.
>

This seems to be not working for my case:

cpdef double myfunc2(double x, double rd, double rh):
    return log(rh) - (kelvin/x) + log(x**3 - rd**3) - log(x**3 -
rd**3*(1.0 - kappa))

cpdef double myfunc2_prime(double x, double rd, double rh):
    -3*x**2/(rd**3 - x**3) - 3*x**2/((kappa - 1.0)*rd**3 + x**3) + kelvin/x**2

rd = 5.75e-08; rh = 0.95

I[4]: newton(myfunc2, rd, args=(rd, rh), fprime=myfunc2_prime)
/usr/lib64/python2.7/site-packages/scipy/optimize/zeros.py:106:
RuntimeWarning: derivative was zero.
  warnings.warn(msg, RuntimeWarning)
O[4]: 5.75e-08

# eliminating the zero derivative.
I[5]: newton(myfunc2, rd, args=(rd, rh), fprime=myfunc2_prime, tol=1.e-10)
O[5]: 5.75e-08

The correct result is:

Setting tol to different accuracies makes a different. In this case
tol=1.e-20 yields exact solution.
I[7]: cnewton(petters_solve_for_rw, rd, args=(rd, rh), tol=1.e-20)
O[7]: 1.4972782377152967e-07


> Trying to get the function in a nicer form might help quite a bit,

Yes, I can confirm this from Charles Harris' suggestion, bit as I said
the speed gain isn't that significant in this case and readability
still counts.
but
> it won't be a solution if you have a large set of functions that might
> show up in different simulations.
>
> It's just an aside for the main topic of the thread, improving the solvers.
>
> Josef





-- 
Gökhan



More information about the SciPy-Dev mailing list