[Tutor] newton's method for system of nonlinear equations
kgotlelelok at galmail.co.za
kgotlelelok at galmail.co.za
Fri Jun 26 12:54:21 CEST 2009
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'
-----------------------------------------
This email was sent using SquirrelMail.
"Webmail for nuts!"
http://squirrelmail.org/
More information about the Tutor
mailing list