##########################################1# Newton's Method Scripts2##########################################3#4# Johann Thiel5# ver 11.25.176# Functions to implement Newton's7# method.8#9##########################################1011##########################################12# Generic Newton's method for one13# 1-variable functions14##########################################15# g = function16# x0 = initial value17# n = number of iterations18##########################################19def newton(g,x0,n):20x = var('x')21f(x) = g(x)22N = x023for i in range(n):24N = N - f(N)/f.diff(x)(N)25return N26##########################################2728##########################################29# Generic Newton's method for two30# 2-variable functions31##########################################32# F,G = functions33# x0 = initial x-value34# y0 = initial y-value35# n = number of iterations36##########################################37def newton2(F,G,x0,y0,n):38x = var('x')39y = var('y')40f(x,y) = F(x,y)41g(x,y) = G(x,y)42N = vector([x0,y0])43for i in range(n):44A = jacobian((f,g),(x,y))(N[0],N[1])45N = N - A^(-1)*vector([f(N[0],N[1]),g(N[0],N[1])])46return N47##########################################4849