-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewtonsystems3.py
97 lines (74 loc) · 2.49 KB
/
newtonsystems3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import numpy as np
import math
import time
from numpy.linalg import inv
from numpy.linalg import norm
import matplotlib.pyplot as plt;
def driver():
x0 = np.array([0.1, 0.1, -0.1])
Nmax = 100
tol = 1e-6
t = time.time()
for j in range(20):
[xstar,xlist,ier,its] = Newton(x0,tol,Nmax);
elapsed = time.time()-t;
print(xstar);
err = np.sum((xlist-xstar)**2,axis=1);
plt.plot(np.arange(its),np.log10(err[0:its]));
plt.show();
t = time.time()
for j in range(20):
[xstar,xlist,ier,its] = LazyNewton(x0,tol,Nmax);
elapsed = time.time()-t
print(xstar);
err2 = np.sum((xlist-xstar)**2,axis=1);
plt.plot(np.arange(its),np.log10(err2[0:its]));
plt.show();
def evalF(x):
return np.array([x[0]+np.cos(x[0]*x[1]*x[2]) - 1,
(1-x[0])**(1/4)+x[1]+0.05*x[2]**2 - 0.15*x[2] - 1,
-x[0]**2 - 0.1*x[1]**2 + 0.01*x[1] + x[2] - 1]);
def evalJ(x):
return np.array([[-x[1]*x[2]*np.sin(x[0]*x[1]*x[2]) + 1, -x[0]*x[2]*np.sin(x[0]*x[1]*x[2]), -x[0]*x[1]*np.sin(x[0]*x[1]*x[2])],
[-0.25*(1 - x[0])**(-0.75), 1, 0.1*x[2] - 0.15],
[-2*x[0], 0.01 - 0.2*x[1], 1]]);
def Newton(x0,tol,Nmax):
''' inputs: x0 = initial guess, tol = tolerance, Nmax = max its'''
''' Outputs: xstar= approx root, ier = error message, its = num its'''
xlist = np.zeros((Nmax+1,len(x0)));
xlist[0] = x0;
for its in range(Nmax):
J = evalJ(x0);
F = evalF(x0);
x1 = x0 - np.linalg.solve(J,F);
xlist[its+1]=x1;
if (norm(x1-x0) < tol*norm(x0)):
xstar = x1
ier =0
return[xstar, xlist,ier, its];
x0 = x1
xstar = x1
ier = 1
return[xstar,xlist,ier,its];
def LazyNewton(x0,tol,Nmax):
''' Lazy Newton = use only the inverse of the Jacobian for initial guess'''
''' inputs: x0 = initial guess, tol = tolerance, Nmax = max its'''
''' Outputs: xstar= approx root, ier = error message, its = num its'''
xlist = np.zeros((Nmax+1,len(x0)));
xlist[0] = x0;
J = evalJ(x0);
for its in range(Nmax):
F = evalF(x0)
x1 = x0 - np.linalg.solve(J,F);
xlist[its+1]=x1;
if (norm(x1-x0) < tol*norm(x0)):
xstar = x1
ier =0
return[xstar,xlist, ier,its];
x0 = x1
xstar = x1
ier = 1
return[xstar,xlist,ier,its];
if __name__ == '__main__':
# run the drivers only if this is called from the command line
driver();