-
Notifications
You must be signed in to change notification settings - Fork 1
/
mynewtonsystems.py
92 lines (48 loc) · 1.53 KB
/
mynewtonsystems.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
import numpy as np
import math
import time
from numpy.linalg import inv
from numpy.linalg import norm
import matplotlib.pyplot as plt
import sympy
def F(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 J(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]]);
# J_broyden = lambda x,y:
for init in [[0.1,0.1,-0.1]]:
print('\nINITIAL CONDIITONS', init)
x = init
# Newton's method
print('\nVANILLA NEWTON\n', x)
try:
for i in range(20):
x = x - np.matmul(inv(J(x)), F(x))
print(x)
except:
print('Error')
# Lazy Newton
# x = init[0]
# y = init[1]
# print('\nLAZY\n', x, y)
# # calc J once
# J_ = J(x,y)
# try:
# for i in range(50):
# vec = [x,y] - np.matmul(inv(J_), [f(x,y), g(x,y)])
# x = vec[0]
# y = vec[1]
# print(x,y)
# except:
# print('Error')
# x,y = init
# print('\nBROYDEN\n', x, y)
# for i in range(50):
# vec = [x,y] - np.matmul(inv(J_broyden(x,y)), [f(x,y), g(x,y)])
# x = vec[0]
# y = vec[1]
# print(x,y)