Contact Us!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

| Download
Project: MAT 3770
Views: 188
1
##########################################
2
# Newton's Method Scripts
3
##########################################
4
#
5
# Johann Thiel
6
# ver 11.25.17
7
# Functions to implement Newton's
8
# method.
9
#
10
##########################################
11
12
##########################################
13
# Generic Newton's method for one
14
# 1-variable functions
15
##########################################
16
# g = function
17
# x0 = initial value
18
# n = number of iterations
19
##########################################
20
def newton(g,x0,n):
21
x = var('x')
22
f(x) = g(x)
23
N = x0
24
for i in range(n):
25
N = N - f(N)/f.diff(x)(N)
26
return N
27
##########################################
28
29
##########################################
30
# Generic Newton's method for two
31
# 2-variable functions
32
##########################################
33
# F,G = functions
34
# x0 = initial x-value
35
# y0 = initial y-value
36
# n = number of iterations
37
##########################################
38
def newton2(F,G,x0,y0,n):
39
x = var('x')
40
y = var('y')
41
f(x,y) = F(x,y)
42
g(x,y) = G(x,y)
43
N = vector([x0,y0])
44
for i in range(n):
45
A = jacobian((f,g),(x,y))(N[0],N[1])
46
N = N - A^(-1)*vector([f(N[0],N[1]),g(N[0],N[1])])
47
return N
48
##########################################
49