# [SciPy-User] Simple root solving issues

Warren Weckesser warren.weckesser@enthought....
Sun Oct 24 16:29:26 CDT 2010

```On Sun, Oct 24, 2010 at 4:14 PM, Charles R Harris <charlesr.harris@gmail.com
> wrote:

>
>
> On Sun, Oct 24, 2010 at 2:43 PM, Gökhan Sever <gokhansever@gmail.com>wrote:
>
>> Hello,
>>
>> I have a simple test case below:
>>
>> #!/usr/bin/env python
>>
>> import numpy as np
>> from scipy.optimize import leastsq, fsolve
>>
>> def fitfunc(gsd):
>>    #return dH_dlogDP1 -
>>
>> (h1.sum()/((2*np.pi)**0.5*np.log(gsd)))*np.exp(-(np.log(Dp1)-np.log(Dp1))**2./(2.*np.log(gsd)**2.))
>>    return dH_dlogDP1 - (h1.sum()/((2*np.pi)**0.5*np.log(gsd)))
>>
>> dH_dlogDP1 = np.array([ 869.11014589])
>> Dp1 = np.array([ 0.02994996])
>> h1 = np.array([ 1906.7283])
>>
>> res = leastsq(fitfunc, x0=1.5)
>> res2 = fsolve(fitfunc, x0=1.0)
>>
>>
>> return terms in fitfunc are equivalent since np.exp(-0) = 1
>> I hand-solve this equation and find gsd as ~2.4. Both leastsq and
>> fsolve requires initial estimates to work properly. Now the problem is
>> they are very sensitive
>> to the selection of initial parameter. x0=1.5 works (gives ~2.39)
>> whereas x0=1.0 situation can't estimate a solution.
>>
>

Your function is basically c1 - c2/log(gsd), so yeah, it will have a problem
if the initial guess is 1.0.  It works fine if the initial guess is, say,
1.01.

>> Is there a way to improve this sensitive init estimate issue with an
>> alternative approach?
>>
>>

Solve c1*log(gsd) - c2 = 0 instead (but don't then change your initial guess
to 0!).

Warren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/scipy-user/attachments/20101024/ed6f4898/attachment.html
```