[SciPy-user] fsolve help

John Hassler hasslerjc at adelphia.net
Fri Jul 14 21:24:40 EDT 2006


Put a decimal point in each quotient to force floating division ... 
otherwise, you get integers
 (ie., 1/2 = 0, but 1./2 = 0.5). 
If I change your function to:

def func2(x):
        out = [x[0]+2.*x[1]+2.*x[2]-1.]
        out.append(x[0]+x[2] - 2.*x[1])
        out.append(x[3]+x[4]-1.)
        out.append(1./5-2./5*x[2]+2./5*x[0]*x[3]-x[0])
        out.append(1./5-x[1]/5.-x[2]/5.+2/5.*x[1]*x[4]+x[1]/5.-x[1])
        #out.append(1/5-2/5*x[1]+2*x[2]/5+x[2]*x[3]/5)
        return out

I get:
"D:\Python\test.py"
[ 0.125   0.1875  0.25    0.5     0.5   ]
which is correct.


john

demarchi at duke.edu wrote:
> Folks:
>
> I'm trying to use fsolve and my program crashes(!) when I execute this:
>
> #!/usr/bin/env python
> import math
> from scipy.optimize import fsolve
>
> def func2(x):
>         out = [x[0]+2*x[1]+2*x[2]-1]
>         out.append(x[0]+x[2] - 2*x[1])
>         out.append(x[3]+x[4]-1)
>         out.append(1/5-2/5*x[2]+2/5*x[0]*x[3]-x[0])
>         out.append(1/5-x[1]/5-x[2]/5+2/5*x[1]*x[4]+x[1]/5-x[1])
>         out.append(1/5-2/5*x[1]+2*x[2]/5+x[2]*x[3]/5)
>         return out
>
> x02 = fsolve(func2, [0.25,0.25,0.5,0.5,0.5])
> print x02
>
> I just did it in maple, and the roots are
> x0=1/8
> x1=3/16
> x2=1/4
> x3=.5
> x4=.5
>
> Moroever, the final equation isn't really needed (and I tried rerunning
> the program without it).  If I do this, the program doesn't crash, but the
> answer doesn't converge (and isn't close -- "The iteration is not making
> good progress..." message shows up).
>
> Am I doing something wrong in python, or is the solver weak, or...?  IF it
> is the latter, is there another library that I could be using?
>
> Thanks for any help.
> Scott
>   



More information about the SciPy-User mailing list