[Tutor] Hi,
bob gailer
bgailer at gmail.com
Fri Jun 26 13:57:52 CEST 2009
Kgotlelelo Legodi 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
Mostly what you are "doing wrong" is failing to tell us why you think
there is a problem! What results are you expecting and what are you getting?
>
> 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'
>
>
--
Bob Gailer
Chapel Hill NC
919-636-4239
More information about the Tutor
mailing list