[SciPy-User] problem using optimize.root

Oleksandr Huziy guziy.sasha at gmail.com
Mon Jun 8 13:29:58 EDT 2015


Hi Jon:

Could it be because root feeds the func with a numpy.array whereas the
newton with a float value.

Help outputs:

newton(func, x0, fprime=None, args=(), tol=1.48e-08, maxiter=50,
fprime2=None)
    Find a zero using the Newton-Raphson or secant method.

    Find a zero of the function `func` given a nearby starting point `x0`.
    The Newton-Raphson method is used if the derivative `fprime` of `func`
    is provided, otherwise the secant method is used.  If the second order
    derivate `fprime2` of `func` is provided, parabolic Halley's method
    is used.

    Parameters
    ----------
    func : function
        The function whose zero is wanted. It must be a function of a
        single variable of the form f(x,a,b,c...), where a,b,c... are extra
        arguments that can be passed in the `args` parameter.
    x0 : float
        An initial estimate of the zero that should be somewhere near the
        actual zero.

.....

root(fun, x0, args=(), method='hybr', jac=None, tol=None, callback=None,
options=None)
    Find a root of a vector function.

    Parameters
    ----------
    fun : callable
        A vector function to find a root of.
    x0 : ndarray
        Initial guess.


Cheers




2015-06-08 13:16 GMT-04:00 Slavin, Jonathan <jslavin at cfa.harvard.edu>:

> Well, I still don't know what the problem was with using root the way I
> did, but I found a way around this issue by using newton instead.  This
> seems to work just fine.
>
> Regards,
> Jon
>
> On Mon, Jun 8, 2015 at 12:16 PM, Slavin, Jonathan <jslavin at cfa.harvard.edu
> > wrote:
>
>> Hi,
>>
>> I'm trying to solve a two-point boundary value problem using odeint in
>> conjunction with root.  I've written a function (the objective function)
>> that takes as an argument the eigenvalue for the problem and returns the
>> difference between the endpoint value for one of the variables and the
>> desired value.  So the function calls odeint with extra arguments that give
>> the initial conditions at the left boundary as well as the eigenvalue.  I
>> can call this function perfectly well from the prompt and it behaves as
>> expected but when I run it within the call to scipy.optimize.root I get the
>> following error:
>> ...
>> /export/slavin/python/anaconda/lib/python2.7/site-packages/scipy/integrate/odepack.pyc
>> in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol,
>> atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords,
>> printmessg)
>>     146     output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv,
>> ml, mu,
>>     147                              full_output, rtol, atol, tcrit, h0,
>> hmax, hmin,
>> --> 148                              ixpr, mxstep, mxhnil, mxordn, mxords)
>>     149     if output[-1] < 0:
>>     150         print(_msgs[output[-1]])
>>
>> TypeError: Cannot cast array data from dtype('O') to dtype('float64')
>> according to the rule 'safe'
>> >
>> /export/slavin/python/anaconda/lib/python2.7/site-packages/scipy/integrate/odepack.py(148)odeint()
>>     147                              full_output, rtol, atol, tcrit, h0,
>> hmax, hmin,
>> --> 148                              ixpr, mxstep, mxhnil, mxordn, mxords)
>>     149     if output[-1] < 0:
>>
>> So when root calls the function, which in turn calls odeint, something
>> that was previously of float type somehow gets the dtype of object.  I've
>> used pdb within ipython to try to find what is happening, but it seems that
>> all the inputs to odeint are of the correct dtype.  Any help would be
>> appreciated.
>>
>> Regards,
>> Jon
>>
>> --
>> ________________________________________________________
>> Jonathan D. Slavin                 Harvard-Smithsonian CfA
>> jslavin at cfa.harvard.edu       60 Garden Street, MS 83
>> phone: (617) 496-7981       Cambridge, MA 02138-1516
>> fax: (617) 496-7577            USA
>> ________________________________________________________
>>
>>
>
>
> --
> ________________________________________________________
> Jonathan D. Slavin                 Harvard-Smithsonian CfA
> jslavin at cfa.harvard.edu       60 Garden Street, MS 83
> phone: (617) 496-7981       Cambridge, MA 02138-1516
> fax: (617) 496-7577            USA
> ________________________________________________________
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>


-- 
Sasha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20150608/42f3361e/attachment.html>


More information about the SciPy-User mailing list