Solve simultaneous equations using the Newton-Raph

From , 2 Years ago, written in Python, viewed 116 times.
URL https://pastebin.vip/view/9b1fc5df
  1. ''' soln = newtonRaphson2(f,x,tol=1.0e-9).
  2.    Solves the simultaneous equations f(x) = 0 by
  3.    the Newton-Raphson method using {x} as the initial
  4.    guess. Note that {f} and {x} are vectors.
  5. '''
  6. from numpy import zeros,dot
  7. from gaussPivot import *
  8. from math import sqrt
  9.  
  10. def newtonRaphson2(f,x,tol=1.0e-9):
  11.  
  12.     def jacobian(f,x):
  13.         h = 1.0e-4
  14.         n = len(x)
  15.         jac = zeros((n,n))
  16.         f0 = f(x)
  17.         for i in range(n):
  18.             temp = x[i]
  19.             x[i] = temp + h
  20.             f1 = f(x)
  21.             x[i] = temp
  22.             jac[:,i] = (f1 - f0)/h
  23.         return jac,f0
  24.  
  25.     for i in range(30):
  26.         jac,f0 = jacobian(f,x)
  27.         if sqrt(dot(f0,f0)/len(x)) < tol: return x
  28.         dx = gaussPivot(jac,-f0)
  29.         x = x + dx
  30.         if sqrt(dot(dx,dx)) < tol*max(max(abs(x)),1.0): return x
  31.     print 'Too many iterations'
  32. #//python/7408

Reply to "Solve simultaneous equations using the Newton-Raph"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website