[Tutor] newton's method for system of nonlinear equations

Skipper Seabold jsseabold at gmail.com
Fri Jun 26 17:58:06 CEST 2009


On Fri, Jun 26, 2009 at 6:54 AM, <kgotlelelok at galmail.co.za> wrote:
> Hi,
> I am trying to write a program in python that solves a system of nonlinear
> equations using newton's method. I don't know what I am doing wrong.
> Please help
>
> from scipy import*
>
> x = array([0.0,0.0,0.0])
> n=len(x)
> tol= 0.00001
> N=30
>
> k=1
> while k <= N:
>    def f(x):
>        f= zeros((len(x)),float)
>        f[0][k]= x[0][k]**2 + x[1][k]-37
>        f[1][k]=x[0][k]- x[1][k]**2- 5
>        f[2][k]= x[0][k] + x[1][k]+ x[2][k]- 3
>        return f[k]
>        def J(x):
>            J= zeros((n,n),float)
>            for i in range(n):
>                ei=zeros(n,float)
>                ei[i]=1.0
>                J[:i]=(f(x[k]+tol*ei)-f(x[k]))/tol
>                return J
>
>        y[k] = -(J.I)*f[k]
>        x[k+1]=x[k]+y[k]
>
> if sqrt(dot(f0,f0)/len(x)) < tol: print x
> else:
>    k=k+1
>
> print 'Too many iterations'
>
>

You might want to have a look at scipy.optimize
<http://docs.scipy.org/scipy/docs/scipy-docs/optimize.rst/#optimize>

If you have more (somewhat) SciPy related questions, you might want to
ask them on the SciPy mailing list.  It's very active.
<http://scipy.org/Mailing_Lists>

Cheers,

Skipper


More information about the Tutor mailing list