[Tutor] Hi,
Kgotlelelo Legodi
klegodi at csir.co.za
Fri Jun 26 12:47:29 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 message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.
This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean. MailScanner thanks Transtec Computers for their support.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090626/eac66464/attachment.htm>
More information about the Tutor
mailing list